// File: baziotis[P]minijava-cpp/typecheck.cpp
// Function: MainTypeCheckVisitor::visit(IfStatement*)
void MainTypeCheckVisitor::visit(IfStatement *if_stmt) {
    LOG_SCOPE;
    debug_print("MainTypeCheck::IfStatement:\n");
    Type *condty = if_stmt->cond->accept(this);
    poison_stmt(condty);
    if (condty != this->type_table.bool_type) {
        typecheck_error(if_stmt->loc, "In if statement, the ",
                        "condition expression must have `boolean` type but has: `",
                        condty->name(), "`");
    }
    /// Codegen ///
    llvalue_t cond = __expr_context.llval;
    // Handle constant condition. Eliminate the branch
    // and elide the never-executed body.
    if (cond.kind == LLVALUE::CONST) {
        // Elide either the `if` body or the `else` body
        // depending on the condition constant value.
        // Assume that the user does not want to type-
        // check the elided body (IIRC if(0) in Java
        // works like #ifdef 0 in C).
        if (cond.val) {
            assert(if_stmt->then);
            if_stmt->then->accept(this);
        } else {
            assert(if_stmt->else_);
            if_stmt->else_->accept(this);
        }
        return;
    }
    
    // Generate the 3 labels used. According to the condition,
    // we either go to if_lbl or else_lbl. Both bodies at the end
    // should jump to end_lbl.
    llvm_label_t if_lbl, else_lbl, end_lbl;
    long lbl = gen_lbl();
    if_lbl.construct("if", lbl);
    else_lbl.construct("else", lbl);
    end_lbl.construct("if_end", lbl);
    
    // Emit a comparison and branch
    /*
    llvalue_t cmp_res;
    cmp_res.kind = LLVALUE::REG;
    cmp_res.reg = gen_reg();
    emit("    %%%ld = icmp eq i1 %%%ld, 1\n", cmp_res.reg, cond.reg);
    */
    llvm_branch_cond(cond, if_lbl, else_lbl);

    /*
    //// Algorithm for emission of phi nodes ////
    
    -- A couple of important info --
    Note that this compiler does not use the classic load / store model.
    Clang / LDC and generally most of the LLVM front-ends, whenever we
    use a variable, they load it from memory. And when they assign to
    a variable, they store to. Implementing this model is simple - just
    `alloca` every local variable and use its memory. For parameters, because
    they come in registers, we just `alloca` a copy of them as well.
    This is simple and effective for all kinds of reasons:
    1) First and foremost, because of its simplicity, bugs are less likely to happen.
    2) Debuggers have an easier time when they know that the memory for a variable
       is in a specific place in memory.
    3) LLVM will remove them anyway in optimized builds.

    But I tried to do something better, for locals (variables and params), I tried
    to use registers only by wrapping them in llvalue_t. That scheme makes
    constant-folding and constant-propagation easy.
    An example:
    a = 2;  // underneath, an llvalue is created, which is of kind CONST
    b = a;  // now, `llval` for the local `b` is the previous CONST llvalue and so on.

    However, with this scheme, problems are introduced with conditional code.
    Let's say we have this:
    if (cond) {
        a = 2;
    } else {
        a = 3;
    }

    What llvalue should `a` get? 2 or 3 ? We can't know at compile-time (assuming
    that `cond` is not known at compile-time).
    What we have to do is emit a phi node, i.e. this code above should translate to:
    
        icmp cond, 1
        br i1 cond, if1, else1
    if1:
        br if_end1
    else1:
        br if_end1
    if_end1:
        %1 = phi [ 2, if1 ], [ 3, else1 ]

    Now, the `llval` for the local `a` is the llvalue of kind REG, where its `reg`
    is 1 (i.e. %1).
    So, now the question is how do we generate these phi nodes.
    
    -- The algorithm --
    We have 2 places where we need this scheme: ifs and whiles. Let's explain
    `if` and you'll get the point.
    Note that an `if` is always followed by an `else`. Thus, we always will go either
    to the `if` body or the `else` body. As such, let's say we want to correctly
    emit code for local `a`. There are 4 cases:
    1) Neither of the 2 bodies assignes to `a`. This is the simplest case, keep
       its current `llval`.
    2) The `if` body _only_ assigns to it. Then, keep the (last) value that it
       assigns and then emit a phi node between this assigned value and the
       previous / initial value. For example:
       a = 2;
       if (cond) {
           a = 3;
       } else {
           // No assignment to `a`
       }
       emit a phi node between 2 and 3 for `a`.

    3) The `else` body _only_ assigns to it. Just as case 2)
    4) Both assign to it. Then, emit a phi node between the 2 assigned values.

    So, there are a couple of things we have to do. First of all, track for each
    `if` statement the assignments in its bodies. We keep a TrackAssignment
    for each local of the function, so a TrackAssignmentBuf.
    We use a TrackAssignmentBuf for each nesting level of the function, _not_ for
    every if. For example:
    if () {
        if () {
        }
        ...
        if () {  <-- We can reuse the same buffer for this if as we're finished with
                     with the above. However we can't use it for the outer if because
                     that's still on the run.
        }
    }

    Actually, we keep 2 buffers for each nesting level. One for the if body and
    one for the else. Because we want to save them separately so we can then
    apply the 4 cases above.
    
    Assuming we have those buffers, on the _return_ of processing the
    `if` and `else` bodies, we apply the 4 cases below.
    */

    assert(if_stmt->then);
    assert(if_stmt->else_);

    __expr_context.nesting_level += 1;
    debug_print("IfStatement nesting level: %d\n", __expr_context.nesting_level);
    
    int nesting_level = __expr_context.nesting_level;
    size_t locals_len = __expr_context.method->locals.len;
    // If we surpassed max nesting level, allocate 2 new buffers.
    if (nesting_level > __expr_context.max_nesting_level) {
        __expr_context.max_nesting_level = nesting_level;
        TrackAssignmentBuf if_buf, else_buf;
        if_buf.reserve(locals_len);
        else_buf.reserve(locals_len);
        __expr_context.if_bufs.push(if_buf);
        __expr_context.else_bufs.push(else_buf);
    }
    Method *method = __expr_context.method;
      
    // Note: You'll see the `nesting_level - 1` a lot below. The reason
    // for the -1 is that if you think about, we don't need to keep
    // assignment info for level 0. So, the 0th buffers are for nesting
    // level 1 etc.

    // Clear the 2 buffers in the current nesting level
    for (size_t i = 0; i != locals_len; ++i) {
        TrackAssignment tasgn;
        tasgn.assigned = false;
        // TODO: There might be a better way to decide about this.
        // Essentially, if we're in the first nesting level, we can just get
        // the local value. But if not, then we have to take from the previous level
        // as the `llval` of the local might have changed but we don't want this new value:
        /*
        if (cond) {
            a = new A();  // let's say %5
        } else {
            // Now, `llval` of `a` is %5 but we don't want that.
            if (cond) {
            } else {
                a = new A();
            }
        }
        */
        if (nesting_level > 1) {
            if (__expr_context.in_if) {
                tasgn.llval = __expr_context.if_bufs[nesting_level - 2][i].llval;
            } else {
                tasgn.llval = __expr_context.else_bufs[nesting_level - 2][i].llval;
            }
        } else {
            tasgn.llval = method->locals[i]->llval;
        }
        Local *local = method->locals[i];
        __expr_context.if_bufs[nesting_level - 1][i] = tasgn;
        __expr_context.else_bufs[nesting_level - 1][i] = tasgn;
    }
    
    // Save if we're currently inside an if or an else.
    bool save_in_if = __expr_context.in_if;
    llvm_gen_lbl(if_lbl);
    __expr_context.in_if = true;
    if_stmt->then->accept(this);

    // 2 bufs that track assignments for the 2 bodies we just processed (if and else)
    TrackAssignmentBuf if_buf = __expr_context.if_bufs[nesting_level - 1];
    TrackAssignmentBuf else_buf = __expr_context.else_bufs[nesting_level - 1];
    
    // Before moving to `else`, if `if` assigned, we want to _revert_ the choice,
    // because the `else` may use a variable before assign it. Consider this:
    // a = 0;
    // if (...) {
    //   a = 2;  // This was visited first and now `a->llval` is 2.
    // } else {
    //   b = a;  // Here, we use `a` before we assign to it, so `b` will get `2`,
    //           // which is wrong.
    // }
    // TODO: That was a hacky choice as I found the bug. This bug might mean
    // that we want to revisit a bigger part of the algorithm.
    for (size_t i = 0; i < method->locals.len; ++i) {
        bool assigned_if = if_buf[i].assigned;
        if (assigned_if) {
            Local *local = method->locals[i];
            local->llval = else_buf[i].llval;
        }
    }

    // Save the last label in if, because this is the label / basic block
    // from which we jump to the `end_lbl`. And we need this info
    // for the emission of the phi node.
    llvm_label_t save_last_lbl_in_if = __expr_context.curr_lbl;
    llvm_branch(end_lbl);
    llvm_gen_lbl(else_lbl);
    __expr_context.in_if = false;
    if_stmt->else_->accept(this);
    // Save the last label in `else` for the same reason as above.
    llvm_label_t save_last_lbl_in_else = __expr_context.curr_lbl;
    llvm_branch(end_lbl);
    llvm_gen_lbl(end_lbl);
    __expr_context.in_if = save_in_if;
    
    // Go through all the locals and apply the 4 cases above.
    for (size_t i = 0; i < method->locals.len; ++i) {
        bool assigned_if = if_buf[i].assigned;
        bool assigned_else = else_buf[i].assigned;
        if (assigned_if || assigned_else) {
            Local *local = method->locals[i];
            // If only one of them assigned to it, then the initial
            // value is on the _other_ buffer. If both bodies
            // assigned to it, then just emit the nodes between the 2.
            // So, in any case, emit a phi node between the 2.
            // Note: Because there's no `if` without `else`, we will
            // always go to either one of the 2.
            llvalue_t if_value = if_buf[i].llval;
            llvalue_t else_value = else_buf[i].llval;
            llvalue_t new_val = llvm_phi_node(local->type, if_value, else_value,
                                              save_last_lbl_in_if,
                                              save_last_lbl_in_else);
            // Save the new val for the local
            local->llval = new_val;
            // Pass on the previous nesting level. If we have an assignment
            // in an enclosed level, then we have in its outer as well.
            if (nesting_level > 1) {
                if (__expr_context.in_if) {
                    __expr_context.if_bufs[nesting_level - 2][i].assigned = true;
                    __expr_context.if_bufs[nesting_level - 2][i].llval = new_val;
                } else {
                    __expr_context.else_bufs[nesting_level - 2][i].assigned = true;
                    __expr_context.else_bufs[nesting_level - 2][i].llval = new_val;
                }
            }
        } /* else {
            // Do nothing, no assignment was involved.
        } */
    }
    __expr_context.nesting_level -= 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x410, %rsp            # imm = 0x410
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2d8(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x3da0
leaq 0xa1da(%rip), %rdi      # 0x1c738
xorl %eax, %eax
callq 0x3e60
jmp 0x12567
movq -0x2d8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x16490
movq %rax, -0x2e0(%rbp)
jmp 0x12584
movq -0x2e0(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x125bf
movl $0x1, -0x34(%rbp)
jmp 0x13274
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x3dd0
jmp 0x13286
movq -0x2d8(%rbp), %rcx
movq -0x30(%rbp), %rax
cmpq 0x8(%rcx), %rax
je 0x12614
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0xc180
movq %rax, -0x2e8(%rbp)
jmp 0x125ec
movq -0x2e8(%rbp), %rcx
movl -0x38(%rbp), %edi
leaq 0xa158(%rip), %rsi      # 0x1c755
leaq 0xa167(%rip), %rdx      # 0x1c76b
leaq 0x8722(%rip), %r8       # 0x1ad2d
callq 0xcca0
jmp 0x12612
jmp 0x12614
leaq 0x123ad(%rip), %rax     # 0x249c8
movq 0x8(%rax), %rcx
movq %rcx, -0x58(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x50(%rbp)
movq 0x18(%rax), %rcx
movq %rcx, -0x48(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0x40(%rbp)
cmpl $0x1, -0x58(%rbp)
jne 0x126e7
cmpl $0x0, -0x40(%rbp)
je 0x12693
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x12658
jmp 0x12677
leaq 0xa145(%rip), %rdi      # 0x1c7a4
leaq 0x90f8(%rip), %rsi      # 0x1b75e
movl $0x648, %edx            # imm = 0x648
leaq 0xa140(%rip), %rcx      # 0x1c7b2
callq 0x20d0
movq -0x2d8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x12691
jmp 0x126db
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x126a0
jmp 0x126bf
leaq 0xa13b(%rip), %rdi      # 0x1c7e2
leaq 0x90b0(%rip), %rsi      # 0x1b75e
movl $0x64b, %edx            # imm = 0x64B
leaq 0xa0f8(%rip), %rcx      # 0x1c7b2
callq 0x20d0
movq -0x2d8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x126d9
jmp 0x126db
movl $0x1, -0x34(%rbp)
jmp 0x13274
leaq -0x60(%rbp), %rdi
callq 0x79b0
jmp 0x126f2
leaq -0x68(%rbp), %rdi
callq 0x79b0
jmp 0x126fd
leaq -0x70(%rbp), %rdi
callq 0x79b0
jmp 0x12708
callq 0x5ab0
movq %rax, -0x2f0(%rbp)
jmp 0x12716
movq -0x2f0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdx
leaq 0x8f01(%rip), %rsi      # 0x1b62d
leaq -0x60(%rbp), %rdi
callq 0x167d0
jmp 0x12737
movq -0x78(%rbp), %rdx
leaq 0x8ee6(%rip), %rsi      # 0x1b628
leaq -0x68(%rbp), %rdi
callq 0x167d0
jmp 0x1274d
movq -0x78(%rbp), %rdx
leaq 0xa099(%rip), %rsi      # 0x1c7f1
leaq -0x70(%rbp), %rdi
callq 0x167d0
jmp 0x12763
movups -0x58(%rbp), %xmm0
movups -0x48(%rbp), %xmm1
movaps %xmm1, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movaps -0xa0(%rbp), %xmm0
movaps -0x90(%rbp), %xmm1
movq %rsp, %rax
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x6c50
jmp 0x127bc
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x127c9
jmp 0x127e8
leaq 0x9fd4(%rip), %rdi      # 0x1c7a4
leaq 0x8f87(%rip), %rsi      # 0x1b75e
movl $0x6c0, %edx            # imm = 0x6C0
leaq 0x9fcf(%rip), %rcx      # 0x1c7b2
callq 0x20d0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x127f5
jmp 0x12814
leaq 0x9fe6(%rip), %rdi      # 0x1c7e2
leaq 0x8f5b(%rip), %rsi      # 0x1b75e
movl $0x6c1, %edx            # imm = 0x6C1
leaq 0x9fa3(%rip), %rcx      # 0x1c7b2
callq 0x20d0
leaq 0x121ad(%rip), %rax     # 0x249c8
movl 0x30(%rax), %ecx
incl %ecx
movl %ecx, 0x30(%rax)
movl 0x30(%rax), %esi
leaq 0x9fcb(%rip), %rdi      # 0x1c7f8
xorl %eax, %eax
callq 0x3e60
jmp 0x12836
leaq 0x1218b(%rip), %rax     # 0x249c8
movl 0x30(%rax), %eax
movl %eax, -0xb4(%rbp)
leaq 0x1217b(%rip), %rax     # 0x249c8
movq 0x28(%rax), %rax
movq 0x38(%rax), %rax
movq %rax, -0xc0(%rbp)
movl -0xb4(%rbp), %eax
leaq 0x1215f(%rip), %rcx     # 0x249c8
cmpl 0x34(%rcx), %eax
jle 0x12956
movl -0xb4(%rbp), %ecx
leaq 0x12149(%rip), %rax     # 0x249c8
movl %ecx, 0x34(%rax)
leaq -0xd8(%rbp), %rdi
callq 0x168b0
jmp 0x12890
leaq -0xf0(%rbp), %rdi
callq 0x168b0
jmp 0x1289e
movq -0xc0(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x168e0
jmp 0x128b3
movq -0xc0(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0x168e0
jmp 0x128c8
movq -0xc8(%rbp), %rax
movq %rax, -0x100(%rbp)
movups -0xd8(%rbp), %xmm0
movaps %xmm0, -0x110(%rbp)
movq -0x100(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movaps -0x110(%rbp), %xmm0
movups %xmm0, (%rax)
leaq 0x120c5(%rip), %rdi     # 0x249c8
addq $0x40, %rdi
callq 0x16960
jmp 0x1290e
movq -0xe0(%rbp), %rax
movq %rax, -0x120(%rbp)
movups -0xf0(%rbp), %xmm0
movaps %xmm0, -0x130(%rbp)
movq -0x120(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movaps -0x130(%rbp), %xmm0
movups %xmm0, (%rax)
leaq 0x1207f(%rip), %rdi     # 0x249c8
addq $0x58, %rdi
callq 0x16960
jmp 0x12954
jmp 0x12956
leaq 0x1206b(%rip), %rax     # 0x249c8
movq 0x28(%rax), %rax
movq %rax, -0x138(%rbp)
movq $0x0, -0x140(%rbp)
movq -0x140(%rbp), %rax
cmpq -0xc0(%rbp), %rax
je 0x12bf9
leaq -0x168(%rbp), %rdi
callq 0x167b0
movb $0x0, -0x148(%rbp)
cmpl $0x1, -0xb4(%rbp)
jle 0x12a9e
leaq 0x1201a(%rip), %rax     # 0x249c8
testb $0x1, 0x38(%rax)
je 0x12a29
movl -0xb4(%rbp), %eax
addl $-0x2, %eax
movslq %eax, %rsi
leaq 0x12001(%rip), %rdi     # 0x249c8
addq $0x40, %rdi
callq 0x16410
movq %rax, -0x2f8(%rbp)
jmp 0x129d9
movq -0x2f8(%rbp), %rdi
movq -0x140(%rbp), %rsi
callq 0x16430
movq %rax, -0x300(%rbp)
jmp 0x129f5
movq -0x300(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x168(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x160(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x158(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x150(%rbp)
jmp 0x12a9c
movl -0xb4(%rbp), %eax
addl $-0x2, %eax
movslq %eax, %rsi
leaq 0x11f8c(%rip), %rdi     # 0x249c8
addq $0x58, %rdi
callq 0x16410
movq %rax, -0x308(%rbp)
jmp 0x12a4e
movq -0x308(%rbp), %rdi
movq -0x140(%rbp), %rsi
callq 0x16430
movq %rax, -0x310(%rbp)
jmp 0x12a6a
movq -0x310(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x168(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x160(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x158(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x150(%rbp)
jmp 0x12af4
movq -0x138(%rbp), %rdi
addq $0x18, %rdi
movq -0x140(%rbp), %rsi
callq 0x7870
movq %rax, -0x318(%rbp)
jmp 0x12abe
movq -0x318(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x168(%rbp)
movq 0x18(%rax), %rcx
movq %rcx, -0x160(%rbp)
movq 0x20(%rax), %rcx
movq %rcx, -0x158(%rbp)
movq 0x28(%rax), %rax
movq %rax, -0x150(%rbp)
movq -0x138(%rbp), %rdi
addq $0x18, %rdi
movq -0x140(%rbp), %rsi
callq 0x7870
movq %rax, -0x320(%rbp)
jmp 0x12b14
movq -0x320(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x170(%rbp)
movl -0xb4(%rbp), %eax
decl %eax
movslq %eax, %rsi
leaq 0x11e91(%rip), %rdi     # 0x249c8
addq $0x40, %rdi
callq 0x16410
movq %rax, -0x328(%rbp)
jmp 0x12b49
movq -0x328(%rbp), %rdi
movq -0x140(%rbp), %rsi
callq 0x16430
movq %rax, -0x330(%rbp)
jmp 0x12b65
movq -0x330(%rbp), %rax
movb -0x148(%rbp), %cl
movb %cl, 0x20(%rax)
movups -0x168(%rbp), %xmm0
movups -0x158(%rbp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movl -0xb4(%rbp), %eax
decl %eax
movslq %eax, %rsi
leaq 0x11e2c(%rip), %rdi     # 0x249c8
addq $0x58, %rdi
callq 0x16410
movq %rax, -0x338(%rbp)
jmp 0x12bae
movq -0x338(%rbp), %rdi
movq -0x140(%rbp), %rsi
callq 0x16430
movq %rax, -0x340(%rbp)
jmp 0x12bca
movq -0x340(%rbp), %rdi
leaq -0x168(%rbp), %rsi
movl $0x21, %edx
callq 0x2100
movq -0x140(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x140(%rbp)
jmp 0x12973
leaq 0x11dc8(%rip), %rax     # 0x249c8
movb 0x38(%rax), %al
andb $0x1, %al
movb %al, -0x171(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rdi
callq 0x6c20
jmp 0x12c24
movq -0x2d8(%rbp), %rsi
leaq 0x11d96(%rip), %rax     # 0x249c8
movb $0x1, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x12c49
movl -0xb4(%rbp), %eax
decl %eax
movslq %eax, %rsi
leaq 0x11d6d(%rip), %rdi     # 0x249c8
addq $0x40, %rdi
callq 0x16410
movq %rax, -0x348(%rbp)
jmp 0x12c6d
movq -0x348(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x190(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x1a0(%rbp)
movl -0xb4(%rbp), %eax
decl %eax
movslq %eax, %rsi
leaq 0x11d2d(%rip), %rdi     # 0x249c8
addq $0x58, %rdi
callq 0x16410
movq %rax, -0x350(%rbp)
jmp 0x12cad
movq -0x350(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x1b8(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x1b0(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x1a8(%rbp)
movq $0x0, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rax
movq -0x138(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x12dc1
movq -0x1c0(%rbp), %rsi
leaq -0x1a0(%rbp), %rdi
callq 0x16430
movq %rax, -0x358(%rbp)
jmp 0x12d13
movq -0x358(%rbp), %rax
movb 0x20(%rax), %al
andb $0x1, %al
movb %al, -0x1c1(%rbp)
testb $0x1, -0x1c1(%rbp)
je 0x12da8
movq -0x138(%rbp), %rdi
addq $0x18, %rdi
movq -0x1c0(%rbp), %rsi
callq 0x7870
movq %rax, -0x360(%rbp)
jmp 0x12d4e
movq -0x360(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1d0(%rbp)
movq -0x1c0(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x16430
movq %rax, -0x368(%rbp)
jmp 0x12d7b
movq -0x368(%rbp), %rcx
movq -0x1d0(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x18(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x20(%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x28(%rax)
jmp 0x12daa
movq -0x1c0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1c0(%rbp)
jmp 0x12cdf
leaq 0x11c00(%rip), %rax     # 0x249c8
movq (%rax), %rax
movq %rax, -0x1d8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0x1e0(%rbp), %rdi
callq 0x6cf0
jmp 0x12deb
movq -0x68(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movq -0x1e8(%rbp), %rdi
callq 0x6c20
jmp 0x12e04
movq -0x2d8(%rbp), %rsi
leaq 0x11bb6(%rip), %rax     # 0x249c8
movb $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x12e29
leaq 0x11b98(%rip), %rax     # 0x249c8
movq (%rax), %rax
movq %rax, -0x1f0(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movq -0x1f8(%rbp), %rdi
callq 0x6cf0
jmp 0x12e53
movq -0x70(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x200(%rbp), %rdi
callq 0x6c20
jmp 0x12e6c
movb -0x171(%rbp), %cl
andb $0x1, %cl
leaq 0x11b4c(%rip), %rax     # 0x249c8
movb %cl, 0x38(%rax)
movq $0x0, -0x208(%rbp)
movq -0x208(%rbp), %rax
movq -0x138(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x13256
movq -0x208(%rbp), %rsi
leaq -0x1a0(%rbp), %rdi
callq 0x16430
movq %rax, -0x370(%rbp)
jmp 0x12ebe
movq -0x370(%rbp), %rax
movb 0x20(%rax), %al
andb $0x1, %al
movb %al, -0x209(%rbp)
movq -0x208(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x16430
movq %rax, -0x378(%rbp)
jmp 0x12eec
movq -0x378(%rbp), %rax
movb 0x20(%rax), %al
andb $0x1, %al
movb %al, -0x20a(%rbp)
testb $0x1, -0x209(%rbp)
jne 0x12f14
testb $0x1, -0x20a(%rbp)
je 0x1323d
movq -0x138(%rbp), %rdi
addq $0x18, %rdi
movq -0x208(%rbp), %rsi
callq 0x7870
movq %rax, -0x380(%rbp)
jmp 0x12f34
movq -0x380(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x218(%rbp)
movq -0x208(%rbp), %rsi
leaq -0x1a0(%rbp), %rdi
callq 0x16430
movq %rax, -0x388(%rbp)
jmp 0x12f61
movq -0x388(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movaps %xmm1, -0x230(%rbp)
movaps %xmm0, -0x240(%rbp)
movq -0x208(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x16430
movq %rax, -0x390(%rbp)
jmp 0x12f99
movq -0x390(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movaps %xmm1, -0x250(%rbp)
movaps %xmm0, -0x260(%rbp)
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rsi
movaps -0x240(%rbp), %xmm0
movaps -0x230(%rbp), %xmm1
movaps %xmm1, -0x290(%rbp)
movaps %xmm0, -0x2a0(%rbp)
movaps -0x260(%rbp), %xmm0
movaps -0x250(%rbp), %xmm1
movaps %xmm1, -0x2b0(%rbp)
movaps %xmm0, -0x2c0(%rbp)
movq -0x1d8(%rbp), %rax
movq %rax, -0x2c8(%rbp)
movq -0x1f0(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movq -0x2c8(%rbp), %rdx
movq -0x2d0(%rbp), %rcx
movaps -0x2c0(%rbp), %xmm0
movaps -0x2b0(%rbp), %xmm1
movq %rsp, %rax
movups %xmm1, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movaps -0x2a0(%rbp), %xmm0
movaps -0x290(%rbp), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x280(%rbp), %rdi
movq $-0x1, %r8
callq 0x6d20
jmp 0x13065
movq -0x218(%rbp), %rax
movq -0x280(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x278(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x270(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq -0x268(%rbp), %rcx
movq %rcx, 0x28(%rax)
cmpl $0x1, -0xb4(%rbp)
jle 0x1323b
leaq 0x1191c(%rip), %rax     # 0x249c8
testb $0x1, 0x38(%rax)
je 0x1317a
movl -0xb4(%rbp), %eax
addl $-0x2, %eax
movslq %eax, %rsi
leaq 0x118ff(%rip), %rdi     # 0x249c8
addq $0x40, %rdi
callq 0x16410
movq %rax, -0x398(%rbp)
jmp 0x130db
movq -0x398(%rbp), %rdi
movq -0x208(%rbp), %rsi
callq 0x16430
movq %rax, -0x3a0(%rbp)
jmp 0x130f7
movq -0x3a0(%rbp), %rax
movb $0x1, 0x20(%rax)
movl -0xb4(%rbp), %eax
addl $-0x2, %eax
movslq %eax, %rsi
leaq 0x118b3(%rip), %rdi     # 0x249c8
addq $0x40, %rdi
callq 0x16410
movq %rax, -0x3a8(%rbp)
jmp 0x13127
movq -0x3a8(%rbp), %rdi
movq -0x208(%rbp), %rsi
callq 0x16430
movq %rax, -0x3b0(%rbp)
jmp 0x13143
movq -0x3b0(%rbp), %rax
movq -0x280(%rbp), %rcx
movq %rcx, (%rax)
movq -0x278(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x270(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x268(%rbp), %rcx
movq %rcx, 0x18(%rax)
jmp 0x13239
movl -0xb4(%rbp), %eax
addl $-0x2, %eax
movslq %eax, %rsi
leaq 0x1183b(%rip), %rdi     # 0x249c8
addq $0x58, %rdi
callq 0x16410
movq %rax, -0x3b8(%rbp)
jmp 0x1319f
movq -0x3b8(%rbp), %rdi
movq -0x208(%rbp), %rsi
callq 0x16430
movq %rax, -0x3c0(%rbp)
jmp 0x131bb
movq -0x3c0(%rbp), %rax
movb $0x1, 0x20(%rax)
movl -0xb4(%rbp), %eax
addl $-0x2, %eax
movslq %eax, %rsi
leaq 0x117ef(%rip), %rdi     # 0x249c8
addq $0x58, %rdi
callq 0x16410
movq %rax, -0x3c8(%rbp)
jmp 0x131eb
movq -0x3c8(%rbp), %rdi
movq -0x208(%rbp), %rsi
callq 0x16430
movq %rax, -0x3d0(%rbp)
jmp 0x13207
movq -0x3d0(%rbp), %rax
movq -0x280(%rbp), %rcx
movq %rcx, (%rax)
movq -0x278(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x270(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x268(%rbp), %rcx
movq %rcx, 0x18(%rax)
jmp 0x1323b
jmp 0x1323d
jmp 0x1323f
movq -0x208(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x208(%rbp)
jmp 0x12e8a
leaq 0x1176b(%rip), %rax     # 0x249c8
movl 0x30(%rax), %ecx
subl $0x1, %ecx
leaq 0x1175e(%rip), %rax     # 0x249c8
movl %ecx, 0x30(%rax)
movl $0x0, -0x34(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x3dd0
addq $0x410, %rsp            # imm = 0x410
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x21c0
nop