// File: /workspace/github_c_makefile_star2_O2/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, %rbx
leaq 0x69f6(%rip), %rsi      # 0xb9d8
callq 0x3420
testq %rax, %rax
je 0x5254
leaq 0x28(%rsp), %r14
movq %rax, 0x18(%r14)
movaps 0x6980(%rip), %xmm0     # 0xb980
movups %xmm0, 0x4(%r14)
leaq 0x6b71(%rip), %rdi      # 0xbb7d
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %rax, 0x18(%rsp)
movq %rax, %rcx
callq 0x37f0
leaq 0x6b60(%rip), %rsi      # 0xbb86
pushq $0xd
popq %rdx
movq %r14, %rdi
callq 0x5269
movl (%rbx), %esi
movq %r14, %rdi
callq 0x52b4
movl 0x4(%rbx), %esi
movq %r14, %rdi
callq 0x52b4
pushq $0x8
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x4f48
pushq $0x6
popq %rsi
movq %r14, %rdi
callq 0x4f48
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
callq 0x4f48
movq %r14, %rdi
xorl %esi, %esi
callq 0x4f48
movq %r14, %rdi
xorl %esi, %esi
callq 0x4f48
movl (%r14), %esi
notl %esi
movq %r14, %rdi
callq 0x52b4
movq 0x18(%r14), %rdi
callq 0x3030
movq %rax, 0x10(%rsp)
leaq 0x6aea(%rip), %rsi      # 0xbb8b
movq %r14, %rdi
xorl %edx, %edx
callq 0x5269
movq %r14, %rdi
movl %ebp, %esi
callq 0x4f48
pushq $0x1d
popq %rsi
movq %r14, %rdi
callq 0x4f48
pushq $0x3
popq %rdx
movq %r14, %rdi
movl %edx, %esi
callq 0x4f05
movq %rbx, %rdx
movq %rbx, 0x20(%rsp)
cmpl 0x4(%rdx), %r15d
jge 0x5193
movq 0x18(%rdx), %rbx
movl (%rdx), %ebp
movl %r15d, 0xc(%rsp)
imull %r15d, %ebp
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x7712
movslq %ebp, %rax
leaq (%rbx,%rax,4), %r12
addq $0x3, %r12
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %eax, %eax
movq 0x20(%rsp), %rdx
movslq (%rdx), %rcx
cmpq %rcx, %rbx
jge 0x5186
movb -0x3(%r12,%rbx,4), %cl
subb %al, %cl
movzbl %cl, %esi
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x7712
movb -0x2(%r12,%rbx,4), %al
subb %r14b, %al
movzbl %al, %esi
movq %r15, %rdi
callq 0x7712
movb -0x1(%r12,%rbx,4), %al
subb %r13b, %al
movzbl %al, %esi
movq %r15, %rdi
callq 0x7712
movb (%r12,%rbx,4), %al
subb %bpl, %al
movzbl %al, %esi
movq %r15, %rdi
callq 0x7712
movb -0x3(%r12,%rbx,4), %al
movb -0x2(%r12,%rbx,4), %r14b
movb -0x1(%r12,%rbx,4), %r13b
movb (%r12,%rbx,4), %bpl
incq %rbx
jmp 0x5112
movl 0xc(%rsp), %r15d
incl %r15d
jmp 0x50d5
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x7772
movq %r14, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x7807
pushq $0x1
popq %rbp
cmpl $0x80, 0x30(%rsp)
je 0x51c8
movq %r14, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x4f05
jmp 0x51b0
leaq 0x28(%rsp), %r14
movl 0x4(%r14), %esi
movq %r14, %rdi
callq 0x52b4
movq 0x18(%r14), %rdi
callq 0x3030
movq %rax, %r15
movq 0x10(%rsp), %r12
subl %r12d, %r15d
movl (%r14), %esi
notl %esi
movq %r14, %rdi
callq 0x52b4
leaq 0x67da(%rip), %rsi      # 0xb9db
movq %r14, %rdi
xorl %edx, %edx
callq 0x5269
movl (%r14), %esi
notl %esi
movq %r14, %rdi
callq 0x52b4
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x33b0
addl $-0x8, %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x52b4
movq %rbx, %rdi
callq 0x3790
movl %eax, %ebp
movq %rbx, %rdi
callq 0x3360
xorl %eax, %eax
testl %ebp, %ebp
sete %al
jmp 0x5257
pushq $0x1
popq %rax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq