// File: /workspace/github_c_makefile_star2_O1/dolphinking[P]slimox/src/slimox-main.c
// Function: main
int main(int argc, char** argv) {
    enum {
        OPERATION_INVALID,
        OPERATION_COMP,
        OPERATION_DECOMP,
        OPERATION_PACK,
        OPERATION_UNPACK,
    } operation = OPERATION_INVALID;

    char option;
    uint32_t block_size = 16777216; /* 16MB by default */
    ppm_model_t* ppm = ppm_model_new();
    buf_t* ib = buf_new();
    buf_t* ob = buf_new();
    struct timeval time_start;
    struct timeval time_end;
    double cost_time;

    /* timer start */
    gettimeofday(&time_start, NULL);

#if defined(_WIN32) || defined(_WIN64)
    /* we need to set stdin/stdout to binary mode under windows */
    setmode(fileno(stdin), O_BINARY);
    setmode(fileno(stdout), O_BINARY);
#endif

    /* process args */
    while(argc > 1 && sscanf(argv[1], "-%c", &option) > 0) {
        if(option == 'q') { /* quiet mode */
            fclose(stderr);
            memmove(&argv[1], &argv[2], (--argc) * sizeof(*argv));
            continue;
        }
        if(option == 'b') { /* set blocksize */
            if(sscanf(argv[1] + 2, "%d", &block_size) > 0) {
                block_size *= 1048576;
            } else {
                fprintf(stderr, "error: Invalid blocksize\n");
                exit(-1);
            }
            memmove(&argv[1], &argv[2], (--argc) * sizeof(*argv));
            continue;
        }
        fprintf(stderr, "error: Invalid argument\n");
        exit(-1);
    }
    buf_resize(ib, block_size);

    /* start! */
    fprintf(stderr, msg_start);
    if(argc == 4 && strcmp(argv[1], "e") == 0) { /* pack directory */
        operation = OPERATION_PACK;
        if(pack(argv[2], argv[3], ppm, block_size) == -1) {
            abort();
        }

        /* timer end */
        gettimeofday(&time_end, NULL);
        cost_time = (time_end.tv_sec - time_start.tv_sec) + (time_end.tv_usec - time_start.tv_usec) / 1000000.0;
        fprintf(stderr, "encode done in %.2lfsec\n", cost_time);
    }
    if(argc == 4 && strcmp(argv[1], "d") == 0) { /* unpack directory */
        operation = OPERATION_PACK;
        if(unpack(argv[2], argv[3], ppm, block_size) == -1) {
            abort();
        }

        /* timer end */
        gettimeofday(&time_end, NULL);
        cost_time = (time_end.tv_sec - time_start.tv_sec) + (time_end.tv_usec - time_start.tv_usec) / 1000000.0;
        fprintf(stderr, "encode done in %.2lfsec\n", cost_time);
    }

    if(operation == OPERATION_INVALID) { /* bad operation */
        fprintf(stderr, msg_help);
    }
    ppm_model_del(ppm);
    buf_del(ib);
    buf_del(ob);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, 0x8(%rsp)
movl %edi, %r13d
movl $0x1000000, 0x4(%rsp)   # imm = 0x1000000
movl $0x320838, %edi         # imm = 0x320838
callq 0x1190
movq %rax, %r12
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
leaq 0x10(%rax), %rbx
movl $0x10, %edi
callq 0x1190
movq %rax, %r14
xorl %r15d, %r15d
movl $0x2a0806, %edx         # imm = 0x2A0806
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10e0
xorpd %xmm0, %xmm0
movupd %xmm0, (%r14)
movq %r14, 0x2a0820(%r12)
movl $0x10, %edi
callq 0x1190
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x2a0818(%r12)
movb $0x0, 0x2a0828(%r12)
movq %r12, %rax
addq $0x2a082c, %rax         # imm = 0x2A082C
leaq 0x21fc(%rip), %rcx      # 0xb280
movzwl (%rcx,%r15,8), %edx
movzwl 0x4(%rcx,%r15,8), %esi
xorl %edi, %edi
movw %dx, -0x2(%rax,%rdi,4)
movw %si, (%rax,%rdi,4)
incq %rdi
cmpq $0x400, %rdi            # imm = 0x400
jne 0x9091
incq %r15
addq $0x1000, %rax           # imm = 0x1000
cmpq $0x80, %r15
jne 0x9084
movabsq $0x100010000, %rax      # imm = 0x100010000
movq %rax, 0x32082a(%r12)
movl $0x10, %edi
callq 0x1190
movq %rax, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movl $0x10, %edi
callq 0x1190
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %ebx, %ebx
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x10d0
cmpl $0x2, %r13d
jl 0x91d8
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rbp
leaq 0x10(%rax), %r12
leal -0x1(%r13), %r14d
shlq $0x3, %r14
leaq 0x3(%rsp), %rbx
movq (%rbp), %rdi
leaq 0x20e2(%rip), %rsi      # 0xb222
movq %rbx, %rdx
xorl %eax, %eax
callq 0x11a0
testl %eax, %eax
jle 0x91bf
movzbl 0x3(%rsp), %eax
cmpl $0x62, %eax
je 0x9172
cmpl $0x71, %eax
jne 0x9371
movq 0x4e90(%rip), %rax      # 0xdff8
movq (%rax), %rdi
callq 0x1090
jmp 0x919a
movq (%rbp), %rdi
addq $0x2, %rdi
leaq 0x20a5(%rip), %rsi      # 0xb226
leaq 0x4(%rsp), %rdx
xorl %eax, %eax
callq 0x11a0
testl %eax, %eax
jle 0x936c
shll $0x14, 0x4(%rsp)
leal -0x1(%r13), %r15d
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x11f0
addq $-0x8, %r14
cmpl $0x2, %r13d
movl %r15d, %r13d
jg 0x9135
jmp 0x91c2
movl %r13d, %r15d
cmpl $0x4, %r15d
sete %bl
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r15
movl 0x4(%rsp), %ebp
movl 0xc(%r15), %eax
cmpl %ebp, %eax
setae %cl
shrl %eax
cmpl %ebp, %eax
setbe %al
testb %al, %cl
jne 0x9218
movl %ebp, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x1e06(%rip), %xmm0     # 0xb008
cvttsd2si %xmm0, %rax
movl %eax, 0xc(%r15)
movq (%r15), %rdi
movl %eax, %esi
callq 0x11c0
movq %rax, (%r15)
movl %ebp, 0x8(%r15)
movq 0x4dd5(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0x4f0b(%rip), %rsi      # 0xe138
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x1150
testb %bl, %bl
movq 0x8(%rsp), %r13
je 0x9257
movq 0x8(%r13), %rdi
leaq 0x2012(%rip), %rsi      # 0xb25c
callq 0x1140
testl %eax, %eax
je 0x9308
testb %bl, %bl
je 0x9273
movq 0x8(%r13), %rdi
leaq 0x1fc1(%rip), %rsi      # 0xb227
callq 0x1140
testl %eax, %eax
je 0x933a
testl %ebp, %ebp
je 0x92fe
movq 0x2a0820(%r12), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x9298
movq (%rdi), %rax
movq %rax, (%rbx)
callq 0x1030
jmp 0x9283
movq %rbx, %rdi
callq 0x1030
movq 0x2a0818(%r12), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x92bd
movq (%rdi), %rax
movq %rax, (%rbx)
callq 0x1030
jmp 0x92a8
movq %rbx, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq (%r15), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq (%r14), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x132f
jmp 0x927b
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rsi
movl 0x4(%rsp), %ecx
movq %r12, %rdx
callq 0x937c
cmpl $-0x1, %eax
je 0x9376
leaq 0x38(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1285
movl $0x3, %ebp
jmp 0x9257
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rsi
movl 0x4(%rsp), %ecx
movq %r12, %rdx
callq 0xa167
cmpl $-0x1, %eax
je 0x9376
leaq 0x38(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x12da
movl $0x3, %ebp
jmp 0x9273
callq 0x1260
callq 0x134d
callq 0x1040
nop