// File: cloudinary[P]fuif/./import/read_gif.h
// Function: read_GIF_file(char const*)
Image read_GIF_file(const char *filename) {
    gd_GIF *gif;
    gif = gd_open_gif(filename);
    if (!gif) {
//        v_printf(3,"%s could not be loaded as GIF\n",filename);
        return Image();
    }
    int w = gif->width;
    int h = gif->height;
    uint8_t *frame = (uint8_t*) calloc(1, w * h * 4);

    Image image(w, h, 255, 4);
    image.den=100;
    int ret = 1;
    int f = 0;
    while (ret) {
        ret = gd_get_frame(gif);
        if (ret < 1) break;

        // make room
        image.channel[0].resize(w, h+h*f);
        image.channel[1].resize(w, h+h*f);
        image.channel[2].resize(w, h+h*f);
        image.channel[3].resize(w, h+h*f);

        gd_render_frame(gif, frame);
        image.num.push_back(gif->gce.delay);
//        printf("Frame %i: ret=%i, delay=%i\n",f,ret,gif->gce.delay);

        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                image.channel[0].value(y+h*f,x) = frame[y*4*w+x*4];
                image.channel[1].value(y+h*f,x) = frame[y*4*w+x*4+1];
                image.channel[2].value(y+h*f,x) = frame[y*4*w+x*4+2];
                image.channel[3].value(y+h*f,x) = frame[y*4*w+x*4+3];
            }
        }
        f++;
    }
    free(frame);
    gd_close_gif(gif);
    image.nb_frames = f;
    image.h = h*f;

    return image;
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0x48(%rsp)
movq %rdi, %rax
movq %rax, 0x50(%rsp)
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0x3ae30
movq %rax, 0x90(%rsp)
cmpq $0x0, 0x90(%rsp)
jne 0x3a9a3
movq 0x48(%rsp), %rdi
callq 0x1f570
jmp 0x3ae0a
movq 0x90(%rsp), %rax
movzwl 0x10(%rax), %eax
movl %eax, 0x8c(%rsp)
movq 0x90(%rsp), %rax
movzwl 0x12(%rax), %eax
movl %eax, 0x88(%rsp)
movl 0x8c(%rsp), %eax
imull 0x88(%rsp), %eax
shll $0x2, %eax
movslq %eax, %rsi
movl $0x1, %edi
callq 0x3280
movq 0x48(%rsp), %rdi
movq %rax, 0x80(%rsp)
movb $0x0, 0x7f(%rsp)
movl 0x8c(%rsp), %esi
movl 0x88(%rsp), %edx
movl $0xff, %ecx
movl $0x4, %r8d
xorl %r9d, %r9d
callq 0x25430
movq 0x48(%rsp), %rax
movl $0x64, 0x3c(%rax)
movl $0x1, 0x78(%rsp)
movl $0x0, 0x74(%rsp)
cmpl $0x0, 0x78(%rsp)
je 0x3adbb
movq 0x90(%rsp), %rdi
callq 0x3b120
movl %eax, 0x44(%rsp)
jmp 0x3aa55
movl 0x44(%rsp), %eax
movl %eax, 0x78(%rsp)
cmpl $0x1, 0x78(%rsp)
jge 0x3aa86
jmp 0x3adbb
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
callq 0x12ab0
jmp 0x3ae17
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4ba0
movq %rax, %rdi
movl 0x8c(%rsp), %esi
movl 0x88(%rsp), %edx
movl 0x74(%rsp), %ecx
movl %edx, %eax
imull %ecx, %eax
addl %eax, %edx
callq 0x36420
jmp 0x3aab7
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x4ba0
movq %rax, %rdi
movl 0x8c(%rsp), %esi
movl 0x88(%rsp), %edx
movl 0x74(%rsp), %ecx
movl %edx, %eax
imull %ecx, %eax
addl %eax, %edx
callq 0x36420
jmp 0x3aae9
movq 0x48(%rsp), %rdi
movl $0x2, %esi
callq 0x4ba0
movq %rax, %rdi
movl 0x8c(%rsp), %esi
movl 0x88(%rsp), %edx
movl 0x74(%rsp), %ecx
movl %edx, %eax
imull %ecx, %eax
addl %eax, %edx
callq 0x36420
jmp 0x3ab1b
movq 0x48(%rsp), %rdi
movl $0x3, %esi
callq 0x4ba0
movq %rax, %rdi
movl 0x8c(%rsp), %esi
movl 0x88(%rsp), %edx
movl 0x74(%rsp), %ecx
movl %edx, %eax
imull %ecx, %eax
addl %eax, %edx
callq 0x36420
jmp 0x3ab4d
movq 0x90(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x3b1d0
jmp 0x3ab64
movq 0x48(%rsp), %rdi
addq $0x40, %rdi
movq 0x90(%rsp), %rax
movzwl 0x18(%rax), %eax
movl %eax, 0x60(%rsp)
leaq 0x60(%rsp), %rsi
callq 0x121b0
jmp 0x3ab89
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x88(%rsp), %eax
jge 0x3adab
movl $0x0, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jge 0x3ad99
movq 0x48(%rsp), %rdi
movq 0x80(%rsp), %rax
movl 0x5c(%rsp), %ecx
movl 0x8c(%rsp), %edx
imull %edx, %ecx
movl 0x58(%rsp), %esi
shll $0x2, %esi
movl %ecx, %edx
movl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movw %ax, 0x36(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x4ba0
movq %rax, %rdi
movl 0x5c(%rsp), %esi
movl 0x88(%rsp), %eax
movl 0x74(%rsp), %ecx
imull %ecx, %eax
addl %eax, %esi
movl 0x58(%rsp), %edx
callq 0x12110
movq %rax, 0x38(%rsp)
jmp 0x3ac20
movq 0x48(%rsp), %rdi
movq 0x38(%rsp), %rax
movw 0x36(%rsp), %cx
movw %cx, (%rax)
movq 0x80(%rsp), %rax
movl 0x5c(%rsp), %ecx
movl 0x8c(%rsp), %edx
imull %edx, %ecx
movl 0x58(%rsp), %esi
shll $0x2, %esi
movl %ecx, %edx
movl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movslq %ecx, %rcx
movzbl 0x1(%rax,%rcx), %eax
movw %ax, 0x26(%rsp)
movl $0x1, %esi
callq 0x4ba0
movq %rax, %rdi
movl 0x5c(%rsp), %esi
movl 0x88(%rsp), %eax
movl 0x74(%rsp), %ecx
imull %ecx, %eax
addl %eax, %esi
movl 0x58(%rsp), %edx
callq 0x12110
movq %rax, 0x28(%rsp)
jmp 0x3ac94
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rax
movw 0x26(%rsp), %cx
movw %cx, (%rax)
movq 0x80(%rsp), %rax
movl 0x5c(%rsp), %ecx
movl 0x8c(%rsp), %edx
imull %edx, %ecx
movl 0x58(%rsp), %esi
shll $0x2, %esi
movl %ecx, %edx
movl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movslq %ecx, %rcx
movzbl 0x2(%rax,%rcx), %eax
movw %ax, 0x16(%rsp)
movl $0x2, %esi
callq 0x4ba0
movq %rax, %rdi
movl 0x5c(%rsp), %esi
movl 0x88(%rsp), %eax
movl 0x74(%rsp), %ecx
imull %ecx, %eax
addl %eax, %esi
movl 0x58(%rsp), %edx
callq 0x12110
movq %rax, 0x18(%rsp)
jmp 0x3ad08
movq 0x48(%rsp), %rdi
movq 0x18(%rsp), %rax
movw 0x16(%rsp), %cx
movw %cx, (%rax)
movq 0x80(%rsp), %rax
movl 0x5c(%rsp), %ecx
movl 0x8c(%rsp), %edx
imull %edx, %ecx
movl 0x58(%rsp), %esi
shll $0x2, %esi
movl %ecx, %edx
movl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movslq %ecx, %rcx
movzbl 0x3(%rax,%rcx), %eax
movw %ax, 0x6(%rsp)
movl $0x3, %esi
callq 0x4ba0
movq %rax, %rdi
movl 0x5c(%rsp), %esi
movl 0x88(%rsp), %eax
movl 0x74(%rsp), %ecx
imull %ecx, %eax
addl %eax, %esi
movl 0x58(%rsp), %edx
callq 0x12110
movq %rax, 0x8(%rsp)
jmp 0x3ad7c
movq 0x8(%rsp), %rax
movw 0x6(%rsp), %cx
movw %cx, (%rax)
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x3abaa
jmp 0x3ad9b
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x3ab91
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x3aa37
movq 0x80(%rsp), %rdi
callq 0x3470
movq 0x90(%rsp), %rdi
callq 0x3b230
jmp 0x3add7
movq 0x48(%rsp), %rax
movl 0x74(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl 0x88(%rsp), %ecx
imull 0x74(%rsp), %ecx
movl %ecx, 0x34(%rax)
movb $0x1, 0x7f(%rsp)
testb $0x1, 0x7f(%rsp)
jne 0x3ae08
movq 0x48(%rsp), %rdi
callq 0x12ab0
jmp 0x3ae0a
movq 0x50(%rsp), %rax
addq $0xa8, %rsp
retq
movq 0x68(%rsp), %rdi
callq 0x36a0
nopw %cs:(%rax,%rax)