// File: /workspace/github_cpp_makefile_star2_O1/silverneko[P]Lambda-calculus-interpreter/src/main.cpp
// Function: weakNormalForm(Expression const&, Context const&)
Object weakNormalForm(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 = weakNormalForm( res.expr(), res.env() );
      }else{
        return makeNormalForm( expr );
      }
      break;
    case Expression::Lambda:
      return Object(expr, 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 weakNormalForm(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 << "[Weak normal form] Unexpected expression type: " << expr.type << endl;
      exit(1);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movl (%rsi), %eax
leaq 0xa96a(%rip), %rcx      # 0xf004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x8(%r15), %r12
movq (%r14), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x778c
testb %al, %al
je 0x474d
movq %r14, %rdi
movq %r12, %rsi
callq 0x449e
movq %rax, %r14
movl (%rax), %eax
testl %eax, %eax
je 0x496e
cmpl $0x2, %eax
jne 0x49de
movl %eax, (%rbx)
movl 0x8(%r14), %eax
movl %eax, 0x8(%rbx)
movl 0xc(%r14), %eax
movl %eax, 0xc(%rbx)
leaq 0x10(%rbx), %r15
leaq 0x20(%rbx), %r12
movq %r12, 0x10(%rbx)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0xa0a0
movq 0x30(%r14), %rax
movq %rax, 0x30(%rbx)
movq 0x38(%r14), %rax
movq %rax, 0x38(%rbx)
movq 0x40(%r14), %rax
movq %rax, 0x40(%rbx)
movq 0x48(%r14), %rax
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x4c2c
movq 0x10884(%rip), %rcx     # 0x14fc0
cmpb $0x0, (%rcx)
je 0x4c28
incl 0x8(%rax)
jmp 0x4c2c
movq %rbx, %rdi
movl $0x2, %esi
movq %r15, %rdx
callq 0x9bd4
jmp 0x4c13
movq 0x28(%r15), %rsi
movq 0x30(%r15), %r15
leaq 0x70(%rsp), %r12
movq %r12, %rdi
movq %r14, %rdx
callq 0x4677
cmpl $0x0, (%r12)
sete %al
movl 0x8(%r12), %ebp
cmpl $0x3, %ebp
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x4801
movq %rsp, %rdi
leaq 0x70(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x4518
movl (%rsp), %eax
testl %eax, %eax
je 0x4ac1
cmpl $0x2, %eax
jne 0x4b29
movl %eax, (%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0xc(%rbx)
leaq 0x10(%rbx), %rdi
leaq 0x20(%rbx), %rax
movq %rax, 0x10(%rbx)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
callq 0xa0a0
jmp 0x4aef
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x9b02
jmp 0x4c13
leaq 0xf8(%rsp), %r13
movl $0x4, -0x18(%r13)
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r13)
movl $0x38, %edi
callq 0x31c0
movq %rax, %r12
movl %ebp, (%rax)
movl 0x7c(%rsp), %eax
movl %eax, 0x4(%r12)
leaq 0x8(%r12), %rdi
movq %r12, %rax
addq $0x18, %rax
movq %rax, 0x8(%r12)
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
addq %rsi, %rdx
callq 0xa0a0
movq 0xa0(%rsp), %rax
movq %rax, 0x28(%r12)
movq 0xa8(%rsp), %rax
movq %rax, 0x30(%r12)
movq %r12, 0x108(%rsp)
movl $0x38, %edi
callq 0x31c0
movq %rax, %r12
movq %rsp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4d7f
movl 0x8(%rsp), %eax
movl %eax, (%r12)
movl 0xc(%rsp), %eax
movl %eax, 0x4(%r12)
leaq 0x8(%r12), %rdi
movq %r12, %rax
addq $0x18, %rax
movq %rax, 0x8(%r12)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
callq 0xa0a0
movq 0x30(%rsp), %rax
movq %rax, 0x28(%r12)
movq 0x38(%rsp), %rax
movq %rax, 0x30(%r12)
movq %r12, 0x110(%rsp)
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x4911
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x4920
callq 0x9f9e
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x493b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x31d0
leaq 0xe0(%rsp), %rdx
movq %rbx, %rdi
movl $0x2, %esi
callq 0x9bd4
movq 0xe8(%rsp), %rdi
cmpq %r13, %rdi
je 0x4bc1
movq 0xf8(%rsp), %rsi
jmp 0x4bb9
movl %eax, (%rbx)
movl 0x8(%r14), %eax
movl %eax, 0x8(%rbx)
movl 0xc(%r14), %eax
movl %eax, 0xc(%rbx)
leaq 0x10(%rbx), %r15
leaq 0x20(%rbx), %r12
movq %r12, 0x10(%rbx)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0xa0a0
movq 0x30(%r14), %rax
movq %rax, 0x30(%rbx)
movq 0x38(%r14), %rax
movq %rax, 0x38(%rbx)
movq 0x40(%r14), %rax
movq %rax, 0x40(%rbx)
movq 0x48(%r14), %rax
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x4c42
movq 0x105f3(%rip), %rcx     # 0x14fc0
cmpb $0x0, (%rcx)
je 0x4c3e
incl 0x8(%rax)
jmp 0x4c42
leaq 0x8(%r14), %rsi
leaq 0x40(%r14), %rdx
leaq 0x70(%rsp), %r15
movq %r15, %rdi
callq 0x4677
movl (%r15), %eax
movl %eax, (%r14)
movq 0x8(%r15), %rax
movq %rax, 0x8(%r14)
leaq 0x10(%r14), %rdi
leaq 0x80(%rsp), %rsi
callq 0x30f0
leaq 0xb8(%rsp), %rsi
movups -0x18(%rsi), %xmm0
movups %xmm0, 0x30(%r14)
movq -0x8(%rsi), %rax
movq %rax, 0x40(%r14)
leaq 0x48(%r14), %rdi
callq 0x9f58
movq %r14, %r13
addq $0x50, %r13
leaq 0xc0(%rsp), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0xa00e
movl (%r14), %eax
movl %eax, (%rbx)
movl 0x8(%r14), %eax
movl %eax, 0x8(%rbx)
movl 0xc(%r14), %eax
movl %eax, 0xc(%rbx)
leaq 0x10(%rbx), %r12
leaq 0x20(%rbx), %rbp
movq %rbp, 0x10(%rbx)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0xa0a0
movq 0x30(%r14), %rax
movq %rax, 0x30(%rbx)
movq 0x38(%r14), %rax
movq %rax, 0x38(%rbx)
movq 0x40(%r14), %rax
movq %rax, 0x40(%rbx)
movq 0x48(%r14), %rax
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x4c58
movq 0x10510(%rip), %rcx     # 0x14fc0
cmpb $0x0, (%rcx)
je 0x4c54
incl 0x8(%rax)
jmp 0x4c58
movl %eax, (%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0xc(%rbx)
leaq 0x10(%rbx), %rdi
leaq 0x20(%rbx), %rax
movq %rax, 0x10(%rbx)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
callq 0xa0a0
movq 0x30(%rsp), %rax
movq %rax, 0x30(%rbx)
movq 0x38(%rsp), %rax
movq %rax, 0x38(%rbx)
movq 0x40(%rsp), %rax
movq %rax, 0x40(%rbx)
movq 0x48(%rsp), %rax
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x4b41
movq 0x104a1(%rip), %rcx     # 0x14fc0
cmpb $0x0, (%rcx)
je 0x4b3d
incl 0x8(%rax)
jmp 0x4b41
leaq 0x8(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movq %rbx, %rdi
callq 0x4677
jmp 0x4b7e
lock
incl 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%rbx)
movq $0x0, 0x60(%rbx)
movq 0x68(%rsp), %rax
movq %rax, 0x68(%rbx)
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x4b7e
leaq 0x50(%rbx), %rdx
leaq 0x60(%rsp), %rcx
movups 0x50(%rsp), %xmm0
movups %xmm0, (%rdx)
movq %rax, 0x60(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rcx)
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x4b97
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x4ba6
callq 0x9f9e
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4bc1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x31d0
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x4be0
leaq 0xc0(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x4bf2
callq 0x9f9e
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c13
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x31d0
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
lock
incl 0x8(%rax)
leaq 0x50(%rbx), %rdi
addq $0x50, %r14
movq %r14, %rsi
callq 0x9ef2
jmp 0x4c13
lock
incl 0x8(%rax)
leaq 0x50(%rbx), %rdi
addq $0x50, %r14
movq %r14, %rsi
callq 0x9ef2
jmp 0x4c13
lock
incl 0x8(%rax)
leaq 0x50(%rbx), %rdi
movq %r13, %rsi
callq 0x9ef2
movq 0xd0(%rsp), %rax
testq %rax, %rax
je 0x4be0
movq %r15, %rdi
movq %r15, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x4be0
movq 0x10362(%rip), %rdi     # 0x14ff0
leaq 0xa760(%rip), %rsi      # 0xf3f5
callq 0x31b0
movl (%r15), %esi
movq %rax, %rdi
callq 0x32c0
movq %rax, %rdi
callq 0x30d0
movl $0x1, %edi
callq 0x3200
jmp 0x4d45
movq %rax, %r14
movq 0x48(%rbx), %rdi
testq %rdi, %rdi
je 0x4ccd
callq 0x9f9e
movq (%r12), %rdi
cmpq %rbp, %rdi
je 0x4d6d
movq (%rbp), %rsi
jmp 0x4d65
jmp 0x4ce5
movq %rax, %r14
movq 0x48(%rbx), %rdi
testq %rdi, %rdi
je 0x4cf6
callq 0x9f9e
movq (%r15), %rdi
cmpq %r12, %rdi
je 0x4d77
movq (%r12), %rsi
incq %rsi
callq 0x31d0
jmp 0x4d77
jmp 0x4d45
jmp 0x4d45
jmp 0x4d40
movq %rax, %r14
movq %rsp, %rdi
callq 0x9b88
jmp 0x4d31
jmp 0x4d2e
movq %rax, %r14
movq %rsp, %rdi
callq 0x9b88
jmp 0x4d6d
movq %rax, %r14
movl $0x38, %esi
movq %r12, %rdi
callq 0x31d0
jmp 0x4d50
movq %rax, %r14
jmp 0x4d6d
movq %rax, %rdi
callq 0x9d49
movq %rax, %r14
movq 0xe8(%rsp), %rdi
cmpq %r13, %rdi
je 0x4d6d
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x31d0
leaq 0x70(%rsp), %rdi
callq 0x9b88
movq %r14, %rdi
callq 0x32d0