// File: /workspace/github_c_makefile_star2_O1/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 0xc862
movq %r13, %rsi
callq 0xc7dc
jmp 0xcac1
movl %edi, %ebp
leaq 0x30b1(%rip), %rax      # 0xf91c
movq %rax, 0x20(%rsp)
leaq 0x30a9(%rip), %rax      # 0xf920
movq %rax, 0x8(%rsp)
leaq 0x30a3(%rip), %rax      # 0xf926
movq %rax, 0x10(%rsp)
movq $-0x1, (%rsp)
leaq 0x30b4(%rip), %rbx      # 0xf94b
leaq 0x6832(%rip), %r15      # 0x130d0
leaq 0x1c(%rsp), %r14
movq 0x6736(%rip), %r12      # 0x12fe0
movl $0x0, 0x1c(%rsp)
movl %ebp, %edi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
movq %r14, %r8
callq 0x10b0
cmpl $0x6c, %eax
jg 0xc8e8
cmpl $-0x1, %eax
je 0xc91c
cmpl $0x3f, %eax
je 0xc929
cmpl $0x63, %eax
jne 0xcb0c
movq (%r12), %rax
movq %rax, 0x10(%rsp)
jmp 0xc929
cmpl $0x6d, %eax
je 0xc920
cmpl $0x74, %eax
je 0xc906
cmpl $0x6f, %eax
jne 0xcb0c
movq (%r12), %rax
movq %rax, 0x20(%rsp)
jmp 0xc929
movq (%r12), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1110
movq %rax, (%rsp)
jmp 0xc929
xorl %eax, %eax
jmp 0xc92b
movq (%r12), %rax
movq %rax, 0x8(%rsp)
movb $0x1, %al
testb %al, %al
jne 0xc8aa
movl %ebp, %eax
movq -0x10(%r13,%rax,8), %rdi
movq -0x8(%r13,%rax,8), %r15
callq 0xcb14
movq %rax, %r13
movl $0x18, %edi
callq 0x1130
movq %rax, %rbp
leaq 0x2fc1(%rip), %rdi      # 0xf91c
movq 0x20(%rsp), %r12
movq %r12, %rsi
callq 0x10f0
testl %eax, %eax
je 0xc9a8
leaq 0x2fdd(%rip), %rdi      # 0xf950
movq %r12, %rsi
callq 0x10f0
testl %eax, %eax
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbx
je 0xc9b9
leaq 0x2fc7(%rip), %rdi      # 0xf957
movq %r12, %rsi
callq 0x10f0
testl %eax, %eax
je 0xc9bd
leaq 0x2fb9(%rip), %rdi      # 0xf95c
jmp 0xcaba
movl $0x1, %eax
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbx
jmp 0xc9c2
xorl %eax, %eax
jmp 0xc9c2
movl $0x2, %eax
movl %eax, (%rbp)
leaq 0x2fd9(%rip), %rdi      # 0xf9a5
movq %rbx, %rsi
callq 0x10f0
testl %eax, %eax
je 0xca0a
leaq 0x2f41(%rip), %rdi      # 0xf920
movq %rbx, %rsi
callq 0x10f0
testl %eax, %eax
je 0xca25
leaq 0x26f5(%rip), %rdi      # 0xf0e7
movq %rbx, %rsi
callq 0x10f0
testl %eax, %eax
je 0xca3c
leaq 0x2fa5(%rip), %rdi      # 0xf9aa
jmp 0xcaba
movl $0x1, 0x8(%rbp)
movq (%rsp), %rax
cmpq $-0x1, %rax
jne 0xca36
movq $0x2d, 0x10(%rbp)
jmp 0xca4d
movl $0x2, 0x8(%rbp)
movq (%rsp), %rax
cmpq $-0x1, %rax
je 0xca45
movq %rax, 0x10(%rbp)
jmp 0xca4d
movl $0x0, 0x8(%rbp)
jmp 0xca4d
movq $0xd2, 0x10(%rbp)
leaq 0x2f99(%rip), %rdi      # 0xf9ed
movq %r14, %rsi
callq 0x10f0
xorl %ebx, %ebx
movl $0x0, %ecx
testl %eax, %eax
je 0xcae2
leaq 0x2f83(%rip), %rdi      # 0xf9f1
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0xcac8
leaq 0x2f74(%rip), %rdi      # 0xf9f5
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0xcacf
leaq 0x2e92(%rip), %rdi      # 0xf926
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0xcad6
leaq 0x2f52(%rip), %rdi      # 0xf9f9
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0xcadd
leaq 0x2f43(%rip), %rdi      # 0xf9fd
xorl %eax, %eax
callq 0x10c0
movl $0x1, %ebx
jmp 0xcafb
movl $0x1, %ecx
jmp 0xcae2
movl $0x2, %ecx
jmp 0xcae2
movl $0x3, %ecx
jmp 0xcae2
movl $0x4, %ecx
movl %ecx, 0x4(%rbp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xcc08
movq %r13, %rdi
movq %r15, %rsi
callq 0xcb97
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1050
nopl (%rax)