// File: /workspace/github_c_makefile_star2_O1/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
movq %rsi, %rbx
cmpl $0x1, %edi
jle 0x1673
movq (%rbx), %rax
movq %rax, 0x2da0(%rip)      # 0x4070
xorl %r15d, %r15d
movq 0x2d1e(%rip), %r12      # 0x3ff8
leaq 0xd23(%rip), %r14       # 0x2004
xorl %ebp, %ebp
xorl %eax, %eax
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x136b
movb 0x1(%rcx), %dl
testb %dl, %dl
je 0x136b
cmpb $0x2d, (%rcx)
jne 0x136b
cmpb $0x2d, %dl
jne 0x1305
cmpb $0x0, 0x2(%rcx)
je 0x133d
addq $0x8, %rbx
movq (%rbx), %rdx
incq %rdx
movl %ebp, %esi
movl %esi, %ebp
movq %rdx, (%rbx)
movzbl (%rdx), %ecx
incq %rdx
movb $0x1, %sil
cmpl $0x76, %ecx
je 0x1311
testl %ecx, %ecx
je 0x1335
decq %rdx
cmpl $0x68, %ecx
jne 0x1348
movb $0x1, %r15b
jmp 0x130c
xorl %ecx, %ecx
testl %ecx, %ecx
je 0x12e5
jmp 0x1362
addq $0x10, %rbx
movl $0x2, %ecx
jmp 0x1337
movq (%r12), %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1070
movl $0x1, %ecx
movl $0x1, %eax
jmp 0x1337
cmpl $0x2, %ecx
jne 0x1664
testb $0x1, %bpl
jne 0x137b
testb $0x1, %r15b
je 0x168b
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
movq 0x2c4b(%rip), %rax      # 0x3fd8
movq (%rax), %rcx
movl $0x1, %esi
movl $0x10, %edx
callq 0x1040
cmpq $0x10, %rax
jne 0x16a1
movabsq $0x646c656662726166, %rax # imm = 0x646C656662726166
cmpq %rax, 0x20(%rsp)
jne 0x16a6
movl 0x28(%rsp), %r12d
movl 0x2c(%rsp), %eax
movl %r12d, %r13d
bswapl %r13d
movl %eax, 0xc(%rsp)
movl %eax, %ebx
bswapl %ebx
movq %rbx, 0x10(%rsp)
imull %r13d, %ebx
movl $0x8, %esi
movq %rbx, %rdi
callq 0x1060
testq %rax, %rax
je 0x167d
movq %rax, %r14
movq 0x2bda(%rip), %rax      # 0x3fd8
movq (%rax), %rcx
movl $0x8, %esi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x1040
cmpq %rbx, %rax
jne 0x16ab
movl %r13d, %r9d
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r8
je 0x1465
xorl %eax, %eax
xorl %ecx, %ecx
movl %eax, %edx
movq %r9, %rsi
testl %r12d, %r12d
je 0x145b
movl %edx, %edi
rolw $0x8, (%r14,%rdi,8)
rolw $0x8, 0x2(%r14,%rdi,8)
rolw $0x8, 0x4(%r14,%rdi,8)
rolw $0x8, 0x6(%r14,%rdi,8)
incl %edx
decq %rsi
jne 0x1437
incl %ecx
addl %r13d, %eax
cmpl %r8d, %ecx
jne 0x142d
movl %r15d, 0x1c(%rsp)
movq %r9, 0x40(%rsp)
movq 0x2b5a(%rip), %r15      # 0x3fd0
movq (%r15), %rsi
leaq 0xc1d(%rip), %rdi       # 0x209d
callq 0x1050
leaq 0x20(%rsp), %rdi
movl %r12d, (%rdi)
movq (%r15), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1697
movl %r12d, 0x18(%rsp)
movq %rbx, 0x38(%rsp)
leaq 0x20(%rsp), %rdi
movl 0xc(%rsp), %ebx
movl %ebx, (%rdi)
movq (%r15), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x10a0
cmpq $0x1, %rax
jne 0x1697
movl 0x1c(%rsp), %ecx
notb %cl
testl %ebx, %ebx
sete %al
orb %cl, %al
testb $0x1, %al
jne 0x153f
movl %r13d, %eax
shrl %eax
movq 0x10(%rsp), %rcx
cmpl $0x1, %ecx
adcl $0x0, %ecx
leal -0x1(%r13), %edx
xorl %esi, %esi
xorl %edi, %edi
movq %rax, %r8
movl %edx, %r9d
movl %esi, %r10d
cmpl $0x2, %r13d
jb 0x1533
movl %r10d, %r11d
movq (%r14,%r11,8), %r15
movl %r9d, %ebx
movq (%r14,%rbx,8), %r12
movq %r12, (%r14,%r11,8)
movq %r15, (%r14,%rbx,8)
incl %r10d
decl %r9d
decq %r8
jne 0x1512
incl %edi
addl %r13d, %esi
addl %r13d, %edx
cmpl %ecx, %edi
jne 0x1503
testb $0x1, %bpl
je 0x15a6
cmpl $0x2, 0x10(%rsp)
jb 0x15a6
movq 0x10(%rsp), %rdx
movl %edx, %eax
shrl %eax
cmpl $0x1, %r13d
movl %r13d, %ecx
adcl $0x0, %ecx
decl %edx
imull %r13d, %edx
xorl %esi, %esi
xorl %edi, %edi
movq %rcx, %r8
movl %esi, %r9d
movl %edx, %r10d
cmpl $0x0, 0x18(%rsp)
je 0x159a
movl %r9d, %r11d
movq (%r14,%r11,8), %rbx
movl %r10d, %r15d
movq (%r14,%r15,8), %r12
movq %r12, (%r14,%r11,8)
movq %rbx, (%r14,%r15,8)
incl %r10d
incl %r9d
decq %r8
jne 0x1579
incl %edi
subl %r13d, %edx
addl %r13d, %esi
cmpl %eax, %edi
jne 0x1569
movl $0x8, %esi
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x1060
testq %rax, %rax
movl 0x18(%rsp), %r10d
je 0x16b0
movq %rax, %r15
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r11
movq 0x29f6(%rip), %r12      # 0x3fd0
je 0x1641
xorl %eax, %eax
xorl %ecx, %ecx
movl %eax, %edx
movq 0x40(%rsp), %rsi
testl %r10d, %r10d
je 0x1637
movl %edx, %edi
movzwl (%r14,%rdi,8), %r8d
rolw $0x8, %r8w
movzwl 0x2(%r14,%rdi,8), %r9d
rolw $0x8, %r9w
movw %r8w, (%r15,%rdi,8)
movzwl 0x4(%r14,%rdi,8), %r8d
rolw $0x8, %r8w
movw %r9w, 0x2(%r15,%rdi,8)
movzwl 0x6(%r14,%rdi,8), %r9d
rolw $0x8, %r9w
movw %r8w, 0x4(%r15,%rdi,8)
movw %r9w, 0x6(%r15,%rdi,8)
incl %edx
decq %rsi
jne 0x15ec
incl %ecx
addl %r13d, %eax
cmpl %r11d, %ecx
jne 0x15e0
movq (%r12), %rcx
movl $0x8, %esi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x10a0
cmpq %rbx, %rax
jne 0x16b5
movq %r15, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x1195
jmp 0x1690
leaq 0x9ed(%rip), %rdi       # 0x2071
callq 0x1080
jmp 0x1690
callq 0x10c0
movl $0x1, %eax
jmp 0x1664
movl $0x1, %edi
callq 0x1090
callq 0x10dc
callq 0x1101
callq 0x1126
callq 0x1170
callq 0x114b