diff --git a/src/main/scala/leon/codegen/CodeGeneration.scala b/src/main/scala/leon/codegen/CodeGeneration.scala
index 0afef05bfd3f0918825d337578f87bc098c68649..c436893e209e2b7e3d3a2cff2c57fbb78bfe9896 100644
--- a/src/main/scala/leon/codegen/CodeGeneration.scala
+++ b/src/main/scala/leon/codegen/CodeGeneration.scala
@@ -56,7 +56,21 @@ object CodeGeneration {
   def compileFunDef(funDef : FunDef, ch : CodeHandler)(implicit env : CompilationEnvironment) {
     val newMapping = funDef.args.map(_.id).zipWithIndex.toMap
 
-    val exprToCompile = purescala.TreeOps.matchToIfThenElse(funDef.getBody)
+    val bodyWithPre = if(funDef.hasPrecondition) {
+      IfExpr(funDef.precondition.get, funDef.getBody, Error("Precondition failed"))
+    } else {
+      funDef.getBody
+    }
+
+    val bodyWithPost = if(funDef.hasPostcondition) {
+      val freshResID = FreshIdentifier("result").setType(funDef.returnType)
+      val post = purescala.TreeOps.replace(Map(ResultVariable() -> Variable(freshResID)), funDef.postcondition.get)
+      Let(freshResID, bodyWithPre, IfExpr(post, Variable(freshResID), Error("Postcondition failed")) )
+    } else {
+      bodyWithPre
+    }
+
+    val exprToCompile = purescala.TreeOps.matchToIfThenElse(bodyWithPost)
 
     mkExpr(exprToCompile, ch)(env.withVars(newMapping))