// File: /workspace/github_c_makefile_star2_O2/erik[P]gamut/flip.c
// Function: main
int main(int argc, char** argv)
{
    uint32_t width = 0,
             height = 0;
    bool flip_v = false,
         flip_h = false;
    pixel_t* image = NULL, temp;

    if (argc < 2) {
        fprintf(stderr, USAGE, argv[0]);
        return 1;
    }

    ARGBEGIN {
    case 'v':
        flip_v = true; break;
    case 'h':
        flip_h = true; break;
    default:
        fprintf(stderr, USAGE, argv[0]);
        return 1;
        ARGEND;
    }

    if (!flip_v && !flip_h) {
        fprintf(stderr, "need to specify a flip direction\n");
        return 1;
    }

    read_header(&width, &height);

    if (!(image = calloc(width * height, sizeof(pixel_t)))) {
        perror("couldn't allocate memory");
        return 1;
    }

    read_image(image, width, height);
    write_header(width, height);

    if (flip_h) {
        for (uint32_t y = 0; y < height; y++) {
            for (uint32_t x = 0; x < width / 2; x++) {
                uint32_t swap = (width - 1) - x;

                memcpy(&temp, PIXEL(&image, x, y), sizeof(pixel_t));
                memcpy(PIXEL(&image, x, y), PIXEL(&image, swap, y), sizeof(pixel_t));
                memcpy(PIXEL(&image, swap, y), &temp, sizeof(pixel_t));
            }
        }
    }

    if (flip_v) {
        for (uint32_t y = 0; y < height / 2; y++) {
            uint32_t swap = (height - 1) - y;
            for (uint32_t x = 0; x < width; x++) {
                memcpy(&temp, PIXEL(&image, x, y), sizeof(pixel_t));
                memcpy(PIXEL(&image, x, y), PIXEL(&image, x, swap), sizeof(pixel_t));
                memcpy(PIXEL(&image, x, swap), &temp, sizeof(pixel_t));
            }
        }
    }

    write_image(image, width, height);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpl $0x1, %edi
jle 0x14d4
movq (%rsi), %rax
movq %rax, 0x2ea3(%rip)      # 0x4070
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x8(%rsi), %rdx
testq %rdx, %rdx
je 0x1221
movb 0x1(%rdx), %al
testb %al, %al
je 0x1221
cmpb $0x2d, (%rdx)
jne 0x1221
cmpb $0x2d, %al
jne 0x11f1
cmpb $0x0, 0x2(%rdx)
je 0x1221
addq $0x8, %rsi
incq %rdx
movl %ebp, %ecx
movl %ecx, %ebp
movq %rdx, (%rsi)
movzbl (%rdx), %eax
incq %rdx
movb $0x1, %cl
cmpl $0x76, %eax
je 0x11fa
testl %eax, %eax
je 0x11d2
cmpl $0x68, %eax
jne 0x14a7
decq %rdx
movb $0x1, %r12b
jmp 0x11f5
testb $0x1, %bpl
jne 0x1231
testb $0x1, %r12b
je 0x14f1
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdi
movaps %xmm0, (%rdi)
movq 0x2d95(%rip), %rbx      # 0x3fd8
movq (%rbx), %rcx
pushq $0x1
popq %rsi
pushq $0x10
popq %rdx
callq 0x1040
cmpq $0x10, %rax
jne 0x150f
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, 0x30(%rsp)
jne 0x1524
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %r13d
movl %eax, 0x2c(%rsp)
bswapl %eax
movl %r13d, %r15d
bswapl %r15d
movq %r15, 0x10(%rsp)
movq %rax, 0x8(%rsp)
imull %eax, %r15d
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x1060
testq %rax, %rax
je 0x14e3
movq %rax, %r14
movl %r13d, 0x24(%rsp)
movl %r12d, 0x28(%rsp)
movq (%rbx), %rcx
pushq $0x8
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x1040
movq %r15, 0x40(%rsp)
cmpq %r15, %rax
jne 0x1539
movl 0x8(%rsp), %eax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
xorl %r13d, %r13d
cmpl 0x10(%rsp), %r13d
je 0x1315
movq 0x18(%rsp), %rbx
movl %r15d, %r12d
subq $0x1, %rbx
jb 0x130b
movl %r12d, %eax
leaq (%r14,%rax,8), %rdi
movq %rdi, %rsi
callq 0x1587
incl %r12d
jmp 0x12f1
incl %r13d
addl 0x8(%rsp), %r15d
jmp 0x12e2
movq 0x2cb4(%rip), %rbx      # 0x3fd0
movq (%rbx), %rsi
leaq 0xd77(%rip), %rdi       # 0x209d
callq 0x1050
leaq 0x30(%rsp), %rdi
movl 0x2c(%rsp), %eax
movl %eax, (%rdi)
movq (%rbx), %rcx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x10a0
cmpq $0x1, %rax
jne 0x157f
leaq 0x30(%rsp), %rdi
movl 0x24(%rsp), %eax
movl %eax, (%rdi)
movq (%rbx), %rcx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x10a0
cmpq $0x1, %rax
movq 0x40(%rsp), %rdi
movl 0x28(%rsp), %eax
jne 0x157f
testb $0x1, %al
je 0x13cf
movq 0x8(%rsp), %rcx
movl %ecx, %eax
shrl %eax
decl %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpl 0x10(%rsp), %esi
je 0x13cf
movl %ecx, %r12d
movl %edx, %r8d
movq %rax, %r9
subq $0x1, %r9
jb 0x13c0
movl %r8d, %r10d
movq (%r14,%r10,8), %r11
movl %r12d, %ebx
movq (%r14,%rbx,8), %r15
movq %r15, (%r14,%r10,8)
movq %r11, (%r14,%rbx,8)
incl %r8d
decl %r12d
jmp 0x139c
incl %esi
movq 0x8(%rsp), %r8
addl %r8d, %edx
addl %r8d, %ecx
jmp 0x138d
testb $0x1, %bpl
je 0x1429
movq 0x10(%rsp), %rcx
movl %ecx, %eax
shrl %eax
decl %ecx
imull 0x8(%rsp), %ecx
xorl %edx, %edx
xorl %esi, %esi
cmpl %eax, %esi
je 0x1429
movl %edx, %ebp
movl %ecx, %r8d
movq 0x18(%rsp), %r9
subq $0x1, %r9
jb 0x141a
movl %ebp, %r10d
movq (%r14,%r10,8), %r11
movl %r8d, %ebx
movq (%r14,%rbx,8), %r15
movq %r15, (%r14,%r10,8)
movq %r11, (%r14,%rbx,8)
incl %r8d
incl %ebp
jmp 0x13f7
incl %esi
movq 0x8(%rsp), %r8
subl %r8d, %ecx
addl %r8d, %edx
jmp 0x13e9
pushq $0x8
popq %rsi
callq 0x1060
testq %rax, %rax
je 0x154e
movq %rax, %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x18(%rsp), %rbx
movl %r12d, %ebp
cmpl 0x10(%rsp), %r13d
je 0x1475
subq $0x1, %rbx
jb 0x146b
movl %ebp, %eax
leaq (%r15,%rax,8), %rdi
leaq (%r14,%rax,8), %rsi
callq 0x1587
incl %ebp
jmp 0x1452
incl %r13d
addl 0x8(%rsp), %r12d
jmp 0x1443
movq 0x2b54(%rip), %rax      # 0x3fd0
movq (%rax), %rcx
pushq $0x8
popq %rsi
movq %r15, %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rdx
callq 0x10a0
cmpq %rbx, %rax
jne 0x1563
movq %r15, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x14c5
movq 0x2b4a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
decq %rdx
leaq 0xb49(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x1070
pushq $0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2b1d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rsi), %rdx
jmp 0x14b4
leaq 0xb87(%rip), %rdi       # 0x2071
callq 0x1080
jmp 0x14c2
movq 0x2b00(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb4d(%rip), %rdi       # 0x204f
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
callq 0x10a0
jmp 0x14c2
movq 0x2ae2(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb6a(%rip), %rdi       # 0x208a
pushq $0x12
jmp 0x1576
movq 0x2acd(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb71(%rip), %rdi       # 0x20a6
pushq $0x14
jmp 0x1576
movq 0x2ab8(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb71(%rip), %rdi       # 0x20bb
pushq $0xf
jmp 0x1576
movq 0x2aa3(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb6c(%rip), %rdi       # 0x20cb
pushq $0x19
jmp 0x1576
movq 0x2a8e(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb71(%rip), %rdi       # 0x20e5
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
callq 0x10a0
pushq $0x1
popq %rdi
callq 0x1090