// File: /workspace/github_c_makefile_star2_O3/pje[P]pixelsort/main.c
// Function: main
int main (int argc, char **argv) {
    char* orientation_arg_value = ARG_ROW;
    char* method_arg_value = ARG_LIGHT;
    char* comparator_arg_value = ARG_MIN;
    long threshold_arg_value = -1;

    if (argc < 3) {
        print_usage(argc, argv);
        return 1;
    }

    int c;
    while (1) {
        static struct option long_options[] = {
            { "orientation",  required_argument, 0, 'o'},
            { "method",       required_argument, 0, 'm'},
            { "comparator",   required_argument, 0, 'c'},
            { "threshold",    required_argument, 0, 't'},
            { 0, 0, 0, 0 }
        };

        int option_index = 0;

        c = getopt_long(argc, argv, "a:f:", long_options, &option_index);

        if (c == -1) break;

        switch (c) {
        case 'o':
            orientation_arg_value = optarg;
            break;
        case 'm':
            method_arg_value = optarg;
            break;
        case 'c':
            comparator_arg_value = optarg;
            break;
        case 't':
            threshold_arg_value = strtol(optarg, NULL, 10);
            break;
        case '?':
            break;
        default:
            abort();
        }
    }

    char *source = argv[argc - 2];
    char *destination = argv[argc - 1];

    struct image *img = read_image(source);

    pixel_sorting_context_t *ctx = (pixel_sorting_context_t*)malloc(sizeof(pixel_sorting_context_t));

    if (0 == strcmp(ARG_ROW, orientation_arg_value)) {
        ctx->orientation = ROW;
    } else if (0 == strcmp(ARG_COLUMN, orientation_arg_value)) {
        ctx->orientation = COLUMN;
    } else if (0 == strcmp(ARG_BOTH, orientation_arg_value)) {
        ctx->orientation = BOTH;
    } else {
        printf("invalid value for orientation argument; must be one of [row|column|both]");
        return 1;
    }

    if (0 == strcmp(ARG_DARK, method_arg_value)) {
        ctx->run_type = DARK;
        if (threshold_arg_value == -1) {
            ctx->threshold = 45;
        } else {
            ctx->threshold = threshold_arg_value;
        }
    } else if (0 == strcmp(ARG_LIGHT, method_arg_value)) {
        ctx->run_type = LIGHT;
        if (threshold_arg_value == -1) {
            ctx->threshold = 210;
        } else {
            ctx->threshold = threshold_arg_value;
        }
    } else if (0 == strcmp(ARG_ALL, method_arg_value)) {
        ctx->run_type = ALL;
    } else {
        printf("invalid value for method argument; must be one of [all|dark|light]");
        return 1;
    }

    if (0 == strcmp(ARG_AVG, comparator_arg_value)) {
        ctx->comparison = AVG;
    } else if (0 == strcmp(ARG_MUL, comparator_arg_value)) {
        ctx->comparison = MUL;
    } else if (0 == strcmp(ARG_MAX, comparator_arg_value)) {
        ctx->comparison = MAX;
    } else if (0 == strcmp(ARG_MIN, comparator_arg_value)) {
        ctx->comparison = MIN;
    } else if (0 == strcmp(ARG_XOR, comparator_arg_value)) {
        ctx->comparison = XOR;
    } else {
        printf("invalid value for comparator argument; must be one of [avg|mul|max|min|xor]");
        return 1;
    }

    sort(img, ctx);
    write_image(img, destination);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r13
cmpl $0x3, %edi
jge 0xc9da
movq %r13, %rsi
callq 0xc954
jmp 0xcc29
movl %edi, %ebp
leaq 0x2ff9(%rip), %rax      # 0xf9dc
movq %rax, 0x20(%rsp)
leaq 0x2ff1(%rip), %rax      # 0xf9e0
movq %rax, 0x8(%rsp)
leaq 0x2feb(%rip), %rax      # 0xf9e6
movq %rax, 0x10(%rsp)
movq $-0x1, (%rsp)
leaq 0x2ffc(%rip), %rbx      # 0xfa0b
leaq 0x66ca(%rip), %r12      # 0x130e0
leaq 0x1c(%rsp), %r15
movq 0x65be(%rip), %r14      # 0x12fe0
movl $0x0, 0x1c(%rsp)
movl %ebp, %edi
movq %r13, %rsi
movq %rbx, %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x10b0
cmpl $0x6c, %eax
jle 0xca5f
cmpl $0x6d, %eax
je 0xca88
cmpl $0x74, %eax
je 0xca73
cmpl $0x6f, %eax
jne 0xcc74
movq (%r14), %rax
movq %rax, 0x20(%rsp)
jmp 0xca22
cmpl $0x3f, %eax
je 0xca22
cmpl $0x63, %eax
jne 0xca92
movq (%r14), %rax
movq %rax, 0x10(%rsp)
jmp 0xca22
movq (%r14), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1120
movq %rax, (%rsp)
jmp 0xca22
movq (%r14), %rax
movq %rax, 0x8(%rsp)
jmp 0xca22
cmpl $-0x1, %eax
jne 0xcc74
movl %ebp, %eax
movq -0x10(%r13,%rax,8), %rdi
movq -0x8(%r13,%rax,8), %r15
callq 0xcc7c
movq %rax, %r13
movl $0x18, %edi
callq 0x1140
movq %rax, %rbp
leaq 0x2f19(%rip), %rdi      # 0xf9dc
movq 0x20(%rsp), %r12
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
je 0xcb10
leaq 0x2f35(%rip), %rdi      # 0xfa10
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbx
je 0xcb21
leaq 0x2f1f(%rip), %rdi      # 0xfa17
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
je 0xcb25
leaq 0x2f11(%rip), %rdi      # 0xfa1c
jmp 0xcc22
movl $0x1, %eax
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbx
jmp 0xcb2a
xorl %eax, %eax
jmp 0xcb2a
movl $0x2, %eax
movl %eax, (%rbp)
leaq 0x2f31(%rip), %rdi      # 0xfa65
movq %rbx, %rsi
callq 0x1100
testl %eax, %eax
je 0xcb72
leaq 0x2e99(%rip), %rdi      # 0xf9e0
movq %rbx, %rsi
callq 0x1100
testl %eax, %eax
je 0xcb8d
leaq 0x264d(%rip), %rdi      # 0xf1a7
movq %rbx, %rsi
callq 0x1100
testl %eax, %eax
je 0xcba4
leaq 0x2efd(%rip), %rdi      # 0xfa6a
jmp 0xcc22
movl $0x1, 0x8(%rbp)
movq (%rsp), %rax
cmpq $-0x1, %rax
jne 0xcb9e
movq $0x2d, 0x10(%rbp)
jmp 0xcbb5
movl $0x2, 0x8(%rbp)
movq (%rsp), %rax
cmpq $-0x1, %rax
je 0xcbad
movq %rax, 0x10(%rbp)
jmp 0xcbb5
movl $0x0, 0x8(%rbp)
jmp 0xcbb5
movq $0xd2, 0x10(%rbp)
leaq 0x2ef1(%rip), %rdi      # 0xfaad
movq %r14, %rsi
callq 0x1100
xorl %ebx, %ebx
movl $0x0, %ecx
testl %eax, %eax
je 0xcc4a
leaq 0x2edb(%rip), %rdi      # 0xfab1
movq %r14, %rsi
callq 0x1100
testl %eax, %eax
je 0xcc30
leaq 0x2ecc(%rip), %rdi      # 0xfab5
movq %r14, %rsi
callq 0x1100
testl %eax, %eax
je 0xcc37
leaq 0x2dea(%rip), %rdi      # 0xf9e6
movq %r14, %rsi
callq 0x1100
testl %eax, %eax
je 0xcc3e
leaq 0x2eaa(%rip), %rdi      # 0xfab9
movq %r14, %rsi
callq 0x1100
testl %eax, %eax
je 0xcc45
leaq 0x2e9b(%rip), %rdi      # 0xfabd
xorl %eax, %eax
callq 0x10c0
movl $0x1, %ebx
jmp 0xcc63
movl $0x1, %ecx
jmp 0xcc4a
movl $0x2, %ecx
jmp 0xcc4a
movl $0x3, %ecx
jmp 0xcc4a
movl $0x4, %ecx
movl %ecx, 0x4(%rbp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xcd70
movq %r13, %rdi
movq %r15, %rsi
callq 0xccff
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1050
nopl (%rax)