// File: /workspace/github_c_makefile_star2_O1/nkanaev[P]tipsy/src/tigr.c
// Function: tigrSaveImage
int tigrSaveImage(const char* fileName, Tigr* bmp) {
    Save s;
    long dataPos, dataSize, err;

    // TODO - unicode?
    FILE* out = fopen(fileName, "wb");
    if (!out)
        return 1;

    s.out = out;
    s.adler = 1;
    s.bits = 0x80;
    s.prev = 0xffff;
    s.runlen = 0;

    savePngHeader(&s, bmp);
    dataPos = ftell(s.out);
    dataSize = savePngData(&s, bmp, dataPos);

    // End chunk.
    begin(&s, "IEND", 0);
    put32(&s, ~s.crc);

    // Write back payload size.
    fseek(out, dataPos, SEEK_SET);
    put32(&s, dataSize);

    err = ferror(out);
    fclose(out);
    return !err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rsi, %r14
leaq 0x7dfe(%rip), %rsi      # 0xc9f8
callq 0x3400
testq %rax, %rax
je 0x4fb0
leaq 0x28(%rsp), %r12
movq %rax, 0x18(%r12)
movaps 0x7d67(%rip), %xmm0     # 0xc980
movups %xmm0, 0x4(%r12)
leaq 0x7f77(%rip), %rdi      # 0xcb9d
movl $0x8, %esi
movl $0x1, %edx
movq %rax, 0x18(%rsp)
movq %rax, %rcx
callq 0x37e0
leaq 0x7f62(%rip), %rsi      # 0xcba6
movq %r12, %rdi
movl $0xd, %edx
callq 0x4fc7
movl (%r14), %esi
movq %r12, %rdi
callq 0x5015
movl 0x4(%r14), %esi
movq %r12, %rdi
callq 0x5015
movq 0x18(%r12), %r15
movl $0x8, %edi
movq %r15, %rsi
callq 0x3480
movl (%r12), %eax
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x8, %eax
leaq 0x7a70(%rip), %rbx      # 0xc700
xorl (%rbx,%rax,4), %ecx
movq %r14, %rbp
movl %ecx, %r14d
shrl $0x4, %r14d
andl $0xf, %ecx
xorl (%rbx,%rcx,4), %r14d
movl %r14d, (%r12)
movl $0x6, %edi
movq %r15, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl $0x6, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %r14d
shrl $0x4, %r14d
andl $0xf, %eax
xorl (%rbx,%rax,4), %r14d
movl %r14d, (%r12)
movl $0x0, 0xc(%rsp)
xorl %edi, %edi
movq %r15, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %r14d
shrl $0x4, %r14d
andl $0xf, %eax
xorl (%rbx,%rax,4), %r14d
movl %r14d, (%r12)
xorl %edi, %edi
movq %r15, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %r14d
shrl $0x4, %r14d
andl $0xf, %eax
xorl (%rbx,%rax,4), %r14d
movl %r14d, (%r12)
xorl %edi, %edi
movq %r15, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl (%rbx,%r14,4), %eax
movl %eax, %esi
shrl $0x4, %esi
andl $0xf, %eax
xorl (%rbx,%rax,4), %esi
movl %esi, (%r12)
notl %esi
movq %r12, %rdi
callq 0x5015
movq 0x18(%r12), %rdi
callq 0x3030
movq %rax, 0x10(%rsp)
leaq 0x7e2f(%rip), %rsi      # 0xcbab
movq %r12, %rdi
xorl %edx, %edx
callq 0x4fc7
movq 0x18(%r12), %r13
movl $0x8, %edi
movq %r13, %rsi
callq 0x3480
movl (%r12), %eax
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x8, %eax
xorl (%rbx,%rax,4), %ecx
movl %ecx, %r14d
shrl $0x4, %r14d
andl $0xf, %ecx
xorl (%rbx,%rcx,4), %r14d
movl %r14d, (%r12)
movl $0x1d, %edi
movq %r13, %rsi
callq 0x3480
movl %r14d, %eax
shrl $0x4, %eax
andl $0xf, %r14d
xorl $0xd, %r14d
xorl (%rbx,%r14,4), %eax
movq %rbp, %r14
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x1, %eax
xorl (%rbx,%rax,4), %ecx
movl %ecx, (%r12)
movq %r12, %rdi
movl $0x3, %esi
movl $0x3, %edx
callq 0x4b25
cmpl $0x0, 0x4(%rbp)
jle 0x4ed7
leaq 0x28(%rsp), %r12
movq %r14, 0x20(%rsp)
movq 0x18(%r14), %rbx
movl (%r14), %ebp
movq %r12, %rdi
movl $0x1, %esi
callq 0x7d0d
cmpl $0x0, (%r14)
movq %r14, %rdx
jle 0x4ec1
imull 0xc(%rsp), %ebp
movslq %ebp, %rax
leaq (%rbx,%rax,4), %rbx
addq $0x3, %rbx
xorl %r13d, %r13d
xorl %eax, %eax
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r15d, %r15d
movb -0x3(%rbx,%r13,4), %cl
subb %al, %cl
movzbl %cl, %esi
movq %r12, %rdi
callq 0x7d0d
movb -0x2(%rbx,%r13,4), %al
subb %r14b, %al
movzbl %al, %esi
movq %r12, %rdi
callq 0x7d0d
movb -0x1(%rbx,%r13,4), %al
subb %bpl, %al
movzbl %al, %esi
movq %r12, %rdi
callq 0x7d0d
movb (%rbx,%r13,4), %al
subb %r15b, %al
movzbl %al, %esi
movq %r12, %rdi
callq 0x7d0d
movq 0x20(%rsp), %rdx
movb -0x3(%rbx,%r13,4), %al
movb -0x2(%rbx,%r13,4), %r14b
movb -0x1(%rbx,%r13,4), %bpl
movb (%rbx,%r13,4), %r15b
incq %r13
movslq (%rdx), %rcx
cmpq %rcx, %r13
jl 0x4e54
movl 0xc(%rsp), %eax
incl %eax
movl %eax, 0xc(%rsp)
cmpl 0x4(%rdx), %eax
movq %rdx, %r14
jl 0x4e16
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x7d85
movl $0xfffffff9, %ebx       # imm = 0xFFFFFFF9
movq %r14, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x4b25
incl %ebx
jne 0x4ee9
cmpl $0x80, 0x30(%rsp)
je 0x4f24
leaq 0x28(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x4b25
cmpl $0x80, 0x30(%rsp)
jne 0x4f0b
leaq 0x28(%rsp), %r14
movl 0x4(%r14), %esi
movq %r14, %rdi
callq 0x5015
movq 0x18(%r14), %rdi
callq 0x3030
movq %rax, %r12
movq 0x10(%rsp), %r15
subl %r15d, %r12d
movl (%r14), %esi
notl %esi
movq %r14, %rdi
callq 0x5015
leaq 0x7a9e(%rip), %rsi      # 0xc9fb
movq %r14, %rdi
xorl %edx, %edx
callq 0x4fc7
movl (%r14), %esi
notl %esi
movq %r14, %rdi
callq 0x5015
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x33a0
addl $-0x8, %r12d
movq %r14, %rdi
movl %r12d, %esi
callq 0x5015
movq %rbx, %rdi
callq 0x3780
movl %eax, %ebp
movq %rbx, %rdi
callq 0x3350
xorl %eax, %eax
testl %ebp, %ebp
sete %al
jmp 0x4fb5
movl $0x1, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq