// File: hzeller[P]gcode-cli/main.cc
// Function: main
int main(int argc, char *argv[]) {
    // -- Command line options.
    bool is_dry_run = false;                // Don't send anything if enabled.
    bool use_ok_flow_control = true;        // wait for 'ok' response
    int block_buffer_count = 1;             // Number of blocks sent at once.
    bool remove_semicolon_comments = true;  // Not all machines understand them
    int initial_squash_chatter_ms = 2500;   // Start after start machine prompt.

    bool print_communication = true;     // print line+block to $log_gcode
    bool print_unusual_messages = true;  // messages outside handshake

    // No cli options for the following yet. Make configurable ?
    const size_t buffer_size = (1 << 20);  // Input buffer in bytes
    FILE *const log_gcode = stderr;        // Log gcode communication here.
    FILE *log_info = stderr;               // info log, switched off with -q

    const char *EXTRA_MESSAGE_ON = "\033[7m";
    const char *EXTRA_MESSAGE_OFF = "\033[0m";
    if (!isatty(STDERR_FILENO)) {
        EXTRA_MESSAGE_ON = EXTRA_MESSAGE_OFF = "";
    }

    int opt;
    while ((opt = getopt(argc, argv, "b:cFhnqs:")) != -1) {
        switch (opt) {
        case 'n': is_dry_run = true; break;
        case 'q':
            log_info = nullptr;
            // unusual messages squashed if -q multiple times
            print_unusual_messages = print_communication;
            print_communication = false;  // Make separate option ?
            break;
        case 'F': use_ok_flow_control = false; break;
        case 'b':
            block_buffer_count = atoi(optarg);
            if (block_buffer_count < 1)
                return usage(argv[0], "Invalid block buffer\n");
            break;
        case 'c': remove_semicolon_comments = false; break;
        case 's':
            initial_squash_chatter_ms = atoi(optarg);
            if (initial_squash_chatter_ms < 0) {
                return usage(argv[0], "Invalid startup squash timeout\n");
            }
            break;
        case 'h': return usage(argv[0], "");
        default: return usage(argv[0], "Invalid option\n");
        }
    }

    if (optind >= argc) {
        return usage(argv[0], "Expected filename\n");
    }

    // Input: Open GCode file
    const char *const filename = argv[optind];
    const int input_fd = (filename == std::string("-"))
                             ? STDIN_FILENO
                             : open(filename, O_RDONLY);
    if (input_fd < 0) {
        fprintf(stderr, "Can't open input %s: %s\n", filename, strerror(errno));
        return 1;
    }

    // Output: open machine connection.
    const char *const connect_str = (optind < argc - 1)  // destination as arg
                                        ? argv[optind + 1]
                                        : "/dev/ttyACM0,b115200";
    is_dry_run |= (strcmp(connect_str, "/dev/null") == 0);

    std::unique_ptr<MachineConnection> machine;
    if (!is_dry_run) {
        machine.reset(MachineConnection::Open(connect_str));
        if (!machine) {
            fprintf(stderr, "Failed to connect to machine %s\n", connect_str);
            return 1;
        }
    }

    // In a dry-run, we also will not read anything.
    use_ok_flow_control &= !is_dry_run;

    // If there is some initial chatter, ignore it, until there is some time
    // silence on the wire.
    // That way, we only get OK responses to our requests.
    // Even without OK flow control, we need to wait as machine might
    // just reset on connect.
    if (machine) {
        machine->DiscardPendingInput(initial_squash_chatter_ms,
                                     print_communication ? log_gcode : nullptr);
    }

    if (log_info) {
        fprintf(log_info, "\n---- Sending file '%s' to '%s'%s -----\n",
                filename, connect_str, is_dry_run ? " (Dry-run)" : "");
    }

    BufferedLineReader gcode_reader(input_fd, buffer_size,
                                    remove_semicolon_comments);
    char *scratch_buffer = new char[buffer_size];
    int line_no = 0;
    const int64_t start_time = get_time_ms();
    while (!gcode_reader.is_eof()) {
        const auto lines = gcode_reader.ReadNextLines(block_buffer_count);

        // Send all block_buffer_count blocks at once.
        if (!is_dry_run && !machine->WriteBlocks(scratch_buffer, lines)) {
            fprintf(stderr, "Couldn't write!\n");
            return 1;
        }

        // We've sent all the lines above at once, now looking at the
        // expected responses for each block to confirm success.
        // Respons to a gcode-block can be multiple lines and are expected
        // to finish with either "ok" or "error".
        // If communication printing requested, print the lines together with
        // their corresponding response.
        for (const auto request : lines) {
            line_no++;
            bool request_line_already_printed = false;
            AckResponse response;
            do {
                std::string_view print_msg;
                response = ReadResponseLine(use_ok_flow_control, machine.get(),
                                            &print_msg);

                // Now we know enough if we should print the original
                // request. Whenever there is some unusual stuff going on, we
                // want to print the original message first before the response.
                const bool needs_printing =
                    (print_communication ||              // regular chatter
                     response == AckResponse::kError ||  // always print error
                     (print_unusual_messages && response != AckResponse::kOk));

                if (needs_printing) {
                    if (!request_line_already_printed) {
                        fprintf(log_gcode, "%6d\t%.*s ", line_no,
                                (int)request.size() - 1, request.data());
                        request_line_already_printed = true;
                    }
                    if (response == AckResponse::kOk) {
                        fprintf(log_gcode,
                                use_ok_flow_control ? "<< OK\n" : "\n");
                    } else {
                        while (!print_msg.empty() &&
                               isspace(*(print_msg.end() - 1))) {
                            print_msg.remove_suffix(1);
                        }
                        fprintf(log_gcode, "\n%s%.*s%s", EXTRA_MESSAGE_ON,
                                (int)print_msg.size(), print_msg.data(),
                                EXTRA_MESSAGE_OFF);
                    }
                    fflush(log_gcode);
                }

                if (response == AckResponse::kError) {
                    handle_error_or_exit();
                }
            } while (response == AckResponse::kMessage);  // more to come
        }
    }

    const int64_t duration = get_time_ms() - start_time;

    if (log_info) {
        fprintf(log_info, "---- Finished file '%s' -----\n", filename);
    }

    // We don't really expect anything coming afterwards from the machine, but
    // if there is an imbalance of sent commands vs. acknowledge flow control
    // tokens, we'd see it now.
    if (!is_dry_run) {
        if (log_info)
            fprintf(log_info, "Discarding remaining machine responses.\n");
        machine->DiscardPendingInput(
            initial_squash_chatter_ms,
            print_unusual_messages ? log_gcode : nullptr);
    }

    close(input_fd);

    if (log_info) {
        fprintf(log_info,
                "Sent total of %d non-empty lines in "
                "%" PRId64 ".%03" PRId64 "s\n",
                line_no, duration / 1000, duration % 1000);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movb $0x1, -0x12(%rbp)
movl $0x1, -0x18(%rbp)
movb $0x1, -0x19(%rbp)
movl $0x9c4, -0x20(%rbp)     # imm = 0x9C4
movb $0x1, -0x21(%rbp)
movb $0x1, -0x22(%rbp)
movq $0x100000, -0x30(%rbp)  # imm = 0x100000
movq 0x8a1e(%rip), %rax      # 0xafd8
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq 0x8a10(%rip), %rax      # 0xafd8
movq (%rax), %rax
movq %rax, -0x40(%rbp)
leaq 0x4ae6(%rip), %rax      # 0x70bc
movq %rax, -0x48(%rbp)
leaq 0x4ae0(%rip), %rax      # 0x70c1
movq %rax, -0x50(%rbp)
movl $0x2, %edi
callq 0x23f0
cmpl $0x0, %eax
jne 0x260a
leaq 0x4bcd(%rip), %rax      # 0x71c8
movq %rax, -0x50(%rbp)
leaq 0x4bc2(%rip), %rax      # 0x71c8
movq %rax, -0x48(%rbp)
jmp 0x260c
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x4aac(%rip), %rdx      # 0x70c6
callq 0x2080
movl %eax, -0x54(%rbp)
cmpl $-0x1, %eax
je 0x2730
movl -0x54(%rbp), %eax
addl $-0x46, %eax
movl %eax, %ecx
movq %rcx, -0x180(%rbp)
subl $0x2d, %eax
ja 0x2710
movq -0x180(%rbp), %rax
leaq 0x49b3(%rip), %rcx      # 0x7004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, -0x11(%rbp)
jmp 0x272b
movq $0x0, -0x40(%rbp)
movb -0x21(%rbp), %al
andb $0x1, %al
movb %al, -0x22(%rbp)
movb $0x0, -0x21(%rbp)
jmp 0x272b
movb $0x0, -0x12(%rbp)
jmp 0x272b
movq 0x893c(%rip), %rax      # 0xafc8
movq (%rax), %rdi
callq 0x23b0
movl %eax, -0x18(%rbp)
cmpl $0x1, -0x18(%rbp)
jge 0x26b8
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x4a25(%rip), %rsi      # 0x70d0
callq 0x3070
movl %eax, -0x4(%rbp)
jmp 0x3033
jmp 0x272b
movb $0x0, -0x19(%rbp)
jmp 0x272b
movq 0x8901(%rip), %rax      # 0xafc8
movq (%rax), %rdi
callq 0x23b0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x26f3
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x4a00(%rip), %rsi      # 0x70e6
callq 0x3070
movl %eax, -0x4(%rbp)
jmp 0x3033
jmp 0x272b
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x4ac5(%rip), %rsi      # 0x71c8
callq 0x3070
movl %eax, -0x4(%rbp)
jmp 0x3033
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x49e8(%rip), %rsi      # 0x7106
callq 0x3070
movl %eax, -0x4(%rbp)
jmp 0x3033
jmp 0x260c
movq 0x88a9(%rip), %rax      # 0xafe0
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jl 0x2759
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x49ca(%rip), %rsi      # 0x7116
callq 0x3070
movl %eax, -0x4(%rbp)
jmp 0x3033
movq -0x10(%rbp), %rax
movq 0x887c(%rip), %rcx      # 0xafe0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x2410
movq -0x188(%rbp), %rdx
leaq 0x498e(%rip), %rsi      # 0x7129
leaq -0x88(%rbp), %rdi
callq 0x33c0
jmp 0x27a9
movq -0x190(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x3390
movb %al, -0x191(%rbp)
jmp 0x27c4
movb -0x191(%rbp), %al
testb $0x1, %al
jne 0x27d0
jmp 0x27da
xorl %eax, %eax
movl %eax, -0x198(%rbp)
jmp 0x27fe
movq -0x60(%rbp), %rdi
xorl %esi, %esi
movb %sil, %al
callq 0x21b0
movl %eax, -0x19c(%rbp)
jmp 0x27f0
movl -0x19c(%rbp), %eax
movl %eax, -0x198(%rbp)
jmp 0x27fe
movl -0x198(%rbp), %eax
movl %eax, -0x1a0(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x2180
leaq -0x89(%rbp), %rdi
callq 0x2260
movl -0x1a0(%rbp), %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jge 0x28cb
movq 0x879c(%rip), %rax      # 0xafd8
movq (%rax), %rax
movq %rax, -0x1b0(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x1a8(%rbp)
callq 0x2040
movl (%rax), %edi
callq 0x20c0
movq -0x1b0(%rbp), %rdi
movq -0x1a8(%rbp), %rdx
movq %rax, %rcx
leaq 0x48b6(%rip), %rsi      # 0x712b
movb $0x0, %al
callq 0x2360
movl $0x1, -0x4(%rbp)
jmp 0x3033
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x28ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x2180
leaq -0x89(%rbp), %rdi
callq 0x2260
jmp 0x3057
movq 0x870e(%rip), %rax      # 0xafe0
movl (%rax), %eax
movl -0x8(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x28fe
movq -0x10(%rbp), %rax
movq 0x86f7(%rip), %rcx      # 0xafe0
movl (%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x1b8(%rbp)
jmp 0x290e
leaq 0x483f(%rip), %rax      # 0x7144
movq %rax, -0x1b8(%rbp)
jmp 0x290e
movq -0x1b8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
leaq 0x482f(%rip), %rsi      # 0x7159
callq 0x2340
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %ecx
movb -0x11(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
orl %ecx, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x11(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3470
testb $0x1, -0x11(%rbp)
jne 0x29eb
movq -0xa8(%rbp), %rdi
callq 0x4290
movq %rax, -0x1c0(%rbp)
jmp 0x297a
movq -0x1c0(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x34a0
leaq -0xb0(%rbp), %rdi
callq 0x34d0
testb $0x1, %al
jne 0x29e9
movq 0x8634(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movq -0xa8(%rbp), %rdx
leaq 0x47ae(%rip), %rsi      # 0x7163
movb $0x0, %al
callq 0x2360
movl $0x1, -0x4(%rbp)
movl $0x1, -0xb4(%rbp)
jmp 0x3027
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x304b
jmp 0x29eb
movb -0x11(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %ecx
movb -0x12(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
andl %ecx, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x12(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x34d0
testb $0x1, %al
jne 0x2a1c
jmp 0x2a73
leaq -0xb0(%rbp), %rdi
callq 0x3500
movq %rax, -0x1d0(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x1c4(%rbp)
testb $0x1, -0x21(%rbp)
je 0x2a4b
movq -0x38(%rbp), %rax
movq %rax, -0x1d8(%rbp)
jmp 0x2a56
xorl %eax, %eax
movq %rax, -0x1d8(%rbp)
jmp 0x2a56
movl -0x1c4(%rbp), %esi
movq -0x1d0(%rbp), %rdi
movq -0x1d8(%rbp), %rdx
callq 0x45b0
jmp 0x2a71
jmp 0x2a73
cmpq $0x0, -0x40(%rbp)
je 0x2ab1
movq -0x40(%rbp), %rdi
movq -0x60(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movb -0x11(%rbp), %sil
leaq 0x4734(%rip), %r8       # 0x71c8
leaq 0x4712(%rip), %rax      # 0x71ad
testb $0x1, %sil
cmovneq %rax, %r8
leaq 0x46da(%rip), %rsi      # 0x7184
movb $0x0, %al
callq 0x2360
movl -0x64(%rbp), %esi
movzbl -0x19(%rbp), %ecx
andl $0x1, %ecx
leaq -0xf8(%rbp), %rdi
movl $0x100000, %edx         # imm = 0x100000
callq 0x5890
jmp 0x2ace
movl $0x100000, %edi         # imm = 0x100000
callq 0x2030
movq %rax, -0x1e0(%rbp)
jmp 0x2ae1
movq -0x1e0(%rbp), %rax
movq %rax, -0x100(%rbp)
movl $0x0, -0x104(%rbp)
callq 0x30d0
movq %rax, -0x1e8(%rbp)
jmp 0x2b07
movq -0x1e8(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3520
movb %al, -0x1e9(%rbp)
jmp 0x2b29
movb -0x1e9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2b3a
jmp 0x2efe
movslq -0x18(%rbp), %rdx
leaq -0x128(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x5aa0
jmp 0x2b53
testb $0x1, -0x11(%rbp)
jne 0x2bfb
leaq -0xb0(%rbp), %rdi
callq 0x3500
movq %rax, %rdi
movq -0x100(%rbp), %rsi
leaq -0x128(%rbp), %rdx
callq 0x47f0
movb %al, -0x1ea(%rbp)
jmp 0x2b87
movb -0x1ea(%rbp), %al
testb $0x1, %al
jne 0x2bfb
jmp 0x2b93
movq 0x843e(%rip), %rax      # 0xafd8
movq (%rax), %rdi
leaq 0x4614(%rip), %rsi      # 0x71b8
movb $0x0, %al
callq 0x2360
movl $0x1, -0x4(%rbp)
movl $0x1, -0xb4(%rbp)
jmp 0x2edd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x303f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x3710
jmp 0x303f
leaq -0x128(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rdi
callq 0x3540
movq %rax, -0x138(%rbp)
movq -0x130(%rbp), %rdi
callq 0x3570
movq %rax, -0x140(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x35a0
testb $0x1, %al
jne 0x2c4b
jmp 0x2ed3
leaq -0x138(%rbp), %rdi
callq 0x35e0
movq (%rax), %rcx
movq %rcx, -0x150(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x148(%rbp)
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
movb $0x0, -0x151(%rbp)
leaq -0x168(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x3600
movzbl -0x12(%rbp), %eax
movl %eax, -0x1fc(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3620
movl -0x1fc(%rbp), %edi
movq -0x1f8(%rbp), %rdx
movq %rax, %rsi
andl $0x1, %edi
callq 0x3110
movl %eax, -0x1f0(%rbp)
jmp 0x2ccb
movl -0x1f0(%rbp), %eax
movl %eax, -0x158(%rbp)
movb $0x1, %al
testb $0x1, -0x21(%rbp)
movb %al, -0x1fd(%rbp)
jne 0x2d20
movb $0x1, %al
cmpl $0x1, -0x158(%rbp)
movb %al, -0x1fd(%rbp)
je 0x2d20
xorl %eax, %eax
testb $0x1, -0x22(%rbp)
movb %al, -0x1fe(%rbp)
je 0x2d14
cmpl $0x0, -0x158(%rbp)
setne %al
movb %al, -0x1fe(%rbp)
movb -0x1fe(%rbp), %al
movb %al, -0x1fd(%rbp)
movb -0x1fd(%rbp), %al
andb $0x1, %al
movb %al, -0x169(%rbp)
testb $0x1, -0x169(%rbp)
je 0x2e9f
testb $0x1, -0x151(%rbp)
jne 0x2da7
movq -0x38(%rbp), %rax
movq %rax, -0x210(%rbp)
movl -0x104(%rbp), %eax
movl %eax, -0x208(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x3650
subl $0x1, %eax
movl %eax, -0x204(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x3670
movq -0x210(%rbp), %rdi
movl -0x208(%rbp), %edx
movl -0x204(%rbp), %ecx
movq %rax, %r8
leaq 0x4430(%rip), %rsi      # 0x71c9
movb $0x0, %al
callq 0x2360
movb $0x1, -0x151(%rbp)
cmpl $0x0, -0x158(%rbp)
jne 0x2dd8
movq -0x38(%rbp), %rdi
movb -0x12(%rbp), %cl
leaq 0x4409(%rip), %rsi      # 0x71c7
leaq 0x440e(%rip), %rax      # 0x71d3
testb $0x1, %cl
cmovneq %rax, %rsi
movb $0x0, %al
callq 0x2360
jmp 0x2e92
jmp 0x2dda
leaq -0x168(%rbp), %rdi
callq 0x3690
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x211(%rbp)
jne 0x2e16
leaq -0x168(%rbp), %rdi
callq 0x36b0
movsbl -0x1(%rax), %edi
callq 0x2060
cmpl $0x0, %eax
setne %al
movb %al, -0x211(%rbp)
movb -0x211(%rbp), %al
testb $0x1, %al
jne 0x2e22
jmp 0x2e35
leaq -0x168(%rbp), %rdi
movl $0x1, %esi
callq 0x36d0
jmp 0x2dda
movq -0x38(%rbp), %rax
movq %rax, -0x228(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x220(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3650
movl %eax, -0x218(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3670
movq -0x228(%rbp), %rdi
movq -0x220(%rbp), %rdx
movl -0x218(%rbp), %ecx
movq %rax, %r8
movq -0x50(%rbp), %r9
leaq 0x434f(%rip), %rsi      # 0x71da
movb $0x0, %al
callq 0x2360
movq -0x38(%rbp), %rdi
callq 0x22a0
jmp 0x2e9d
jmp 0x2e9f
cmpl $0x1, -0x158(%rbp)
jne 0x2eb1
callq 0x32a0
jmp 0x2eaf
jmp 0x2eb1
jmp 0x2eb3
cmpl $0x2, -0x158(%rbp)
je 0x2c82
jmp 0x2ec2
leaq -0x138(%rbp), %rdi
callq 0x36f0
jmp 0x2c2f
movl $0x0, -0xb4(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x3710
movl -0xb4(%rbp), %eax
testl %eax, %eax
jne 0x301b
jmp 0x2ef9
jmp 0x2b15
callq 0x30d0
movq %rax, -0x230(%rbp)
jmp 0x2f0c
movq -0x230(%rbp), %rax
subq -0x110(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x2f3e
movq -0x40(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0x42ad(%rip), %rsi      # 0x71e4
movb $0x0, %al
callq 0x2360
testb $0x1, -0x11(%rbp)
jne 0x2fb4
cmpq $0x0, -0x40(%rbp)
je 0x2f5d
movq -0x40(%rbp), %rdi
leaq 0x42ad(%rip), %rsi      # 0x7203
movb $0x0, %al
callq 0x2360
leaq -0xb0(%rbp), %rdi
callq 0x3500
movq %rax, -0x240(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x234(%rbp)
testb $0x1, -0x22(%rbp)
je 0x2f8c
movq -0x38(%rbp), %rax
movq %rax, -0x248(%rbp)
jmp 0x2f97
xorl %eax, %eax
movq %rax, -0x248(%rbp)
jmp 0x2f97
movl -0x234(%rbp), %esi
movq -0x240(%rbp), %rdi
movq -0x248(%rbp), %rdx
callq 0x45b0
jmp 0x2fb2
jmp 0x2fb4
movl -0x64(%rbp), %edi
callq 0x2450
jmp 0x2fbe
cmpq $0x0, -0x40(%rbp)
je 0x3011
movq -0x40(%rbp), %rdi
movl -0x104(%rbp), %eax
movl %eax, -0x24c(%rbp)
movq -0x178(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rcx
movq -0x178(%rbp), %rax
movl $0x3e8, %esi            # imm = 0x3E8
cqto
idivq %rsi
movq %rdx, %r8
movl -0x24c(%rbp), %edx
leaq 0x4222(%rip), %rsi      # 0x722c
movb $0x0, %al
callq 0x2360
movl $0x0, -0xb4(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x5900
leaq -0xb0(%rbp), %rdi
callq 0x3770
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
leaq -0xf8(%rbp), %rdi
callq 0x5900
leaq -0xb0(%rbp), %rdi
callq 0x3770
movq -0x98(%rbp), %rdi
callq 0x2400
nopw %cs:(%rax,%rax)