// File: /workspace/github_c_makefile_star2_O2/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, %r15
cmpl $0x3, %edi
jge 0x99cb
movq %r15, %rsi
callq 0x9948
jmp 0x9bf7
movl %edi, %ebp
leaq 0x2f48(%rip), %rax      # 0xc91c
movq %rax, 0x10(%rsp)
leaq 0x2f40(%rip), %rax      # 0xc920
movq %rax, 0x18(%rsp)
leaq 0x2f3a(%rip), %rax      # 0xc926
movq %rax, 0x20(%rsp)
pushq $-0x1
popq %rax
movq %rax, (%rsp)
leaq 0x2f4c(%rip), %rbx      # 0xc94b
leaq 0x76ca(%rip), %r13      # 0x110d0
leaq 0xc(%rsp), %r12
movq 0x75ce(%rip), %r14      # 0x10fe0
andl $0x0, 0xc(%rsp)
movl %ebp, %edi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movq %r12, %r8
callq 0x10b0
cmpl $0x3f, %eax
je 0x9a12
cmpl $0x63, %eax
je 0x9a6a
cmpl $0x6d, %eax
je 0x9a56
cmpl $0x6f, %eax
je 0x9a60
cmpl $0x74, %eax
jne 0x9a74
movq (%r14), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x1110
movq %rax, (%rsp)
jmp 0x9a12
movq (%r14), %rax
movq %rax, 0x18(%rsp)
jmp 0x9a12
movq (%r14), %rax
movq %rax, 0x10(%rsp)
jmp 0x9a12
movq (%r14), %rax
movq %rax, 0x20(%rsp)
jmp 0x9a12
cmpl $-0x1, %eax
jne 0x9c35
movl %ebp, %eax
movq -0x10(%r15,%rax,8), %rdi
movq -0x8(%r15,%rax,8), %r12
callq 0x9c3c
movq %rax, %r15
pushq $0x18
popq %rdi
callq 0x1130
movq %rax, %rbp
leaq 0x2e79(%rip), %rdi      # 0xc91c
movq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0x10f0
testl %eax, %eax
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %rbx
je 0x9af0
leaq 0x2e8b(%rip), %rdi      # 0xc950
movq %r13, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9af4
leaq 0x2e7f(%rip), %rdi      # 0xc957
movq %r13, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9af8
leaq 0x2e71(%rip), %rdi      # 0xc95c
jmp 0x9bf0
pushq $0x1
jmp 0x9afa
xorl %eax, %eax
jmp 0x9afb
pushq $0x2
popq %rax
movl %eax, (%rbp)
leaq 0x2ea0(%rip), %rdi      # 0xc9a5
movq %rbx, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9b43
leaq 0x2e08(%rip), %rdi      # 0xc920
movq %rbx, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9b5e
leaq 0x25bc(%rip), %rdi      # 0xc0e7
movq %rbx, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9b75
leaq 0x2e6c(%rip), %rdi      # 0xc9aa
jmp 0x9bf0
movl $0x1, 0x8(%rbp)
movq (%rsp), %rax
cmpq $-0x1, %rax
jne 0x9b6f
movq $0x2d, 0x10(%rbp)
jmp 0x9b83
movl $0x2, 0x8(%rbp)
movq (%rsp), %rax
cmpq $-0x1, %rax
je 0x9b7b
movq %rax, 0x10(%rbp)
jmp 0x9b83
andl $0x0, 0x8(%rbp)
jmp 0x9b83
movq $0xd2, 0x10(%rbp)
leaq 0x2e63(%rip), %rdi      # 0xc9ed
movq %r14, %rsi
callq 0x10f0
xorl %ebx, %ebx
movl $0x0, %ecx
testl %eax, %eax
je 0x9c0b
leaq 0x2e4d(%rip), %rdi      # 0xc9f1
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9bfc
leaq 0x2e3e(%rip), %rdi      # 0xc9f5
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9c00
leaq 0x2d5c(%rip), %rdi      # 0xc926
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9c04
leaq 0x2e1c(%rip), %rdi      # 0xc9f9
movq %r14, %rsi
callq 0x10f0
testl %eax, %eax
je 0x9c08
leaq 0x2e0d(%rip), %rdi      # 0xc9fd
xorl %eax, %eax
callq 0x10c0
pushq $0x1
popq %rbx
jmp 0x9c24
pushq $0x1
jmp 0x9c0a
pushq $0x2
jmp 0x9c0a
pushq $0x3
jmp 0x9c0a
pushq $0x4
popq %rcx
movl %ecx, 0x4(%rbp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x9d2c
movq %r15, %rdi
movq %r12, %rsi
callq 0x9cba
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1050
nop