// File: silverneko[P]Lambda-calculus-interpreter/src/main.cpp
// Function: normalForm(Expression const&, Context const&)
Object normalForm(const Expression& expr, const Context& env){
  switch( expr.type ){
    case Expression::Constant:
      return makeNormalForm( expr );
      break;
    case Expression::Var:
      if( env.exist(expr.name) ){
        Object &res = env[expr.name];
        if( res.isNormalForm() )
          return res;
        if( res.isPrimitive() )
          return res;
        return res = normalForm( res.expr(), res.env() );
      }else{
        return makeNormalForm( expr );
      }
      break;
    case Expression::Lambda:
      {
        Expression expr2(expr);
        expr2.body = new Expression(normalForm(*expr.body, env.erase( expr.name )).expr());
        return makeNormalForm( expr2 );
      // return Object(expr2, env);
      }
      break;
    case Expression::Ap:
      {
        const Expression& body = *expr.body;
        const Expression& arg = *expr.arg;
        Object callee(weakNormalForm(body, env));
        if( callee.callable() ){
          Object res = callee.call(arg, env);
          if( res.isNormalForm() )
            return res;
          if( res.isPrimitive() )
            return res;
          return normalForm(res.expr(), res.env());
        }else{
          Expression expr2(Expression::Ap);
          expr2.body = new Expression(callee.expr());
          expr2.arg = new Expression(normalForm(arg, env).expr());
          return makeNormalForm( expr2 );
        }
      }
      break;
    case Expression::Nothing:
      cerr << "[Normal form] Unexpected expression type: " << expr.type << endl;
      exit(1);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3a0, %rsp            # imm = 0x3A0
movq %rdi, -0x320(%rbp)
movq %rdi, %rax
movq %rax, -0x318(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x310(%rbp)
subq $0x4, %rax
ja 0x5514
movq -0x310(%rbp), %rax
leaq 0x1a15b(%rip), %rcx     # 0x1f018
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x320(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x48e0
jmp 0x5516
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x46b0
testb $0x1, %al
jne 0x4ef5
jmp 0x4fea
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x45d0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x114c0
testb $0x1, %al
jne 0x4f19
jmp 0x4f2e
movq -0x320(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x11230
jmp 0x5516
movq -0x20(%rbp), %rdi
callq 0x114e0
testb $0x1, %al
jne 0x4f3d
jmp 0x4f52
movq -0x320(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x11230
jmp 0x5516
movq -0x20(%rbp), %rdi
callq 0x11500
movq %rax, -0x338(%rbp)
movq -0x20(%rbp), %rdi
callq 0x11520
movq -0x338(%rbp), %rsi
movq %rax, %rdx
leaq -0x90(%rbp), %rdi
movq %rdi, -0x330(%rbp)
callq 0x4e70
movq -0x330(%rbp), %rsi
movq -0x20(%rbp), %rdi
callq 0x11540
movq %rax, -0x328(%rbp)
jmp 0x4fa1
movq -0x328(%rbp), %rsi
movq -0x320(%rbp), %rdi
callq 0x11230
jmp 0x4fb6
leaq -0x90(%rbp), %rdi
callq 0x111d0
jmp 0x5516
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x111d0
jmp 0x5526
movq -0x320(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x48e0
jmp 0x5516
movq -0x10(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x11680
movl $0x38, %edi
callq 0x3230
movq %rax, -0x340(%rbp)
jmp 0x5022
movb $0x1, -0x159(%rbp)
movq -0x10(%rbp), %rdx
movq 0x28(%rdx), %rax
movq %rax, -0x348(%rbp)
movq -0x18(%rbp), %rsi
addq $0x8, %rdx
leaq -0x158(%rbp), %rdi
callq 0x44a0
jmp 0x504e
movq -0x348(%rbp), %rsi
leaq -0x148(%rbp), %rdi
leaq -0x158(%rbp), %rdx
callq 0x4e70
jmp 0x506a
leaq -0x148(%rbp), %rdi
callq 0x11500
movq %rax, -0x350(%rbp)
jmp 0x507f
movq -0x350(%rbp), %rsi
movq -0x340(%rbp), %rdi
callq 0x11680
jmp 0x5094
movq -0x340(%rbp), %rax
movb $0x0, -0x159(%rbp)
movq %rax, -0xb0(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x111d0
leaq -0x158(%rbp), %rdi
callq 0x11340
movq -0x320(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x48e0
jmp 0x50d6
leaq -0xd8(%rbp), %rdi
callq 0x116e0
jmp 0x5516
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x516b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x514d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x5141
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x111d0
leaq -0x158(%rbp), %rdi
callq 0x11340
testb $0x1, -0x159(%rbp)
jne 0x5158
jmp 0x5169
movq -0x340(%rbp), %rdi
movl $0x38, %esi
callq 0x3240
jmp 0x516b
leaq -0xd8(%rbp), %rdi
callq 0x116e0
jmp 0x5526
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x168(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x170(%rbp)
movq -0x168(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x1e0(%rbp), %rdi
movq %rdi, -0x360(%rbp)
callq 0x4910
movq -0x360(%rbp), %rdi
callq 0x46e0
movb %al, -0x351(%rbp)
jmp 0x51cc
movb -0x351(%rbp), %al
testb $0x1, %al
jne 0x51db
jmp 0x531b
movq -0x170(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq -0x250(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x4730
jmp 0x51fb
leaq -0x250(%rbp), %rdi
callq 0x114c0
movb %al, -0x361(%rbp)
jmp 0x520f
movb -0x361(%rbp), %al
testb $0x1, %al
jne 0x521b
jmp 0x5279
movq -0x320(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x115b0
jmp 0x5230
movl $0x1, -0x254(%rbp)
jmp 0x530a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x54cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x111d0
jmp 0x54cc
leaq -0x250(%rbp), %rdi
callq 0x114e0
movb %al, -0x362(%rbp)
jmp 0x528d
movb -0x362(%rbp), %al
testb $0x1, %al
jne 0x5299
jmp 0x52ba
movq -0x320(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x115b0
jmp 0x52ae
movl $0x1, -0x254(%rbp)
jmp 0x530a
leaq -0x250(%rbp), %rdi
callq 0x11500
movq %rax, -0x370(%rbp)
jmp 0x52cf
leaq -0x250(%rbp), %rdi
callq 0x11520
movq %rax, -0x378(%rbp)
jmp 0x52e4
movq -0x378(%rbp), %rdx
movq -0x370(%rbp), %rsi
movq -0x320(%rbp), %rdi
callq 0x4e70
jmp 0x5300
movl $0x1, -0x254(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x111d0
jmp 0x54be
leaq -0x290(%rbp), %rdi
movl $0x4, %esi
callq 0x11640
jmp 0x532e
movl $0x38, %edi
callq 0x3230
movq %rax, -0x380(%rbp)
jmp 0x5341
leaq -0x1e0(%rbp), %rdi
callq 0x11500
movq %rax, -0x388(%rbp)
jmp 0x5356
movq -0x388(%rbp), %rsi
movq -0x380(%rbp), %rdi
callq 0x11680
jmp 0x536b
movq -0x380(%rbp), %rax
movq %rax, -0x268(%rbp)
movl $0x38, %edi
callq 0x3230
movq %rax, -0x390(%rbp)
jmp 0x538c
movb $0x1, -0x301(%rbp)
movq -0x170(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x300(%rbp), %rdi
callq 0x4e70
jmp 0x53ac
leaq -0x300(%rbp), %rdi
callq 0x11500
movq %rax, -0x398(%rbp)
jmp 0x53c1
movq -0x398(%rbp), %rsi
movq -0x390(%rbp), %rdi
callq 0x11680
jmp 0x53d6
movq -0x390(%rbp), %rax
movb $0x0, -0x301(%rbp)
movq %rax, -0x260(%rbp)
leaq -0x300(%rbp), %rdi
callq 0x111d0
movq -0x320(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x48e0
jmp 0x540c
movl $0x1, -0x254(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x116e0
jmp 0x54be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x54b0
movq -0x380(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
movl $0x38, %esi
callq 0x3240
jmp 0x54b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x5492
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x300(%rbp), %rdi
callq 0x111d0
testb $0x1, -0x301(%rbp)
jne 0x549d
jmp 0x54ae
movq -0x390(%rbp), %rdi
movl $0x38, %esi
callq 0x3240
jmp 0x54b0
leaq -0x290(%rbp), %rdi
callq 0x116e0
jmp 0x54cc
leaq -0x1e0(%rbp), %rdi
callq 0x111d0
jmp 0x5516
leaq -0x1e0(%rbp), %rdi
callq 0x111d0
jmp 0x5526
movq 0x2ab0f(%rip), %rdi     # 0x2fff0
leaq 0x1a061(%rip), %rsi     # 0x1f549
callq 0x3220
movq %rax, %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
callq 0x33b0
movq %rax, %rdi
movq 0x2aaa3(%rip), %rsi     # 0x2ffa8
callq 0x3260
movl $0x1, %edi
callq 0x32c0
ud2
movq -0x318(%rbp), %rax
addq $0x3a0, %rsp            # imm = 0x3A0
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x33c0
nopw %cs:(%rax,%rax)