// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x120, %rsp            # imm = 0x120
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl (%rsi), %eax
leaq 0x7006(%rip), %rcx      # 0xb004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x8(%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3ec0
testb %al, %al
je 0x4047
movq %r15, %rdi
movq %r12, %rsi
callq 0x3e1a
movq %rax, %r14
movl (%rax), %eax
testl %eax, %eax
je 0x4037
cmpl $0x2, %eax
jne 0x416c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6ffa
jmp 0x41c2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3fc8
jmp 0x41c2
movq 0x28(%r14), %rsi
movq 0x30(%r14), %r12
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x3fdc
cmpl $0x0, (%r14)
je 0x413e
cmpl $0x3, 0x80(%rsp)
je 0x413e
leaq 0xf0(%rsp), %r14
movl $0x4, -0x8(%r14)
leaq 0x100(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
pushq $0x38
popq %rdi
callq 0x31b0
movq %rax, %r13
leaq 0x80(%rsp), %rsi
movq %rax, %rdi
callq 0x715c
movq %r13, 0x110(%rsp)
pushq $0x38
popq %rdi
callq 0x31b0
movq %rax, %r13
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x4258
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x715c
leaq 0xe8(%rsp), %r15
movq %r13, 0x30(%r15)
leaq 0x8(%rsp), %rdi
callq 0x6fd6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3fc8
movq %r14, %rdi
callq 0x30c0
jmp 0x41b8
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6f9c
jmp 0x41c2
leaq 0x8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x3edc
testl $0xfffffffd, 0x8(%rsp)  # imm = 0xFFFFFFFD
jne 0x419c
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x711a
jmp 0x41ae
leaq 0x8(%r14), %rsi
movq %r14, %rdx
addq $0x40, %rdx
leaq 0x78(%rsp), %r15
movq %r15, %rdi
callq 0x3fdc
movq %r14, %rdi
movq %r15, %rsi
callq 0x70d6
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6ffa
jmp 0x41b8
leaq 0x10(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %rbx, %rdi
callq 0x3fdc
leaq 0x8(%rsp), %rdi
callq 0x6fd6
leaq 0x78(%rsp), %rdi
callq 0x6fd6
movq %rbx, %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0xce13(%rip), %rdi      # 0x10ff0
leaq 0x7211(%rip), %rsi      # 0xb3f5
callq 0x31a0
movl (%r14), %esi
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x30e0
pushq $0x1
popq %rdi
callq 0x31e0
jmp 0x4236
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x6fd6
jmp 0x421a
jmp 0x4217
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r13, %rdi
callq 0x31c0
jmp 0x423e
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x6fd6
jmp 0x4246
movq %rax, %rbx
jmp 0x4246
movq %rax, %rbx
movq %r14, %rdi
callq 0x30c0
leaq 0x78(%rsp), %rdi
callq 0x6fd6
movq %rbx, %rdi
callq 0x32b0