// File: cloudinary[P]fuif/./import/read_gif.h
// Function: gd_open_gif(char const*)
gd_GIF *
gd_open_gif(const char *fname)
{
    int fd;
    uint8_t sigver[3];
    uint16_t width, height, depth;
    uint8_t fdsz, bgidx, aspect;
    int gct_sz;
    gd_GIF *gif;

    fd = open(fname, O_RDONLY);
    if (fd == -1) return NULL;
    /* Header */
    read(fd, sigver, 3);
    if (memcmp(sigver, "GIF", 3) != 0) {
//        fprintf(stderr, "invalid signature\n");
        goto fail;
    }
    /* Version */
    read(fd, sigver, 3);
    if (memcmp(sigver, "89a", 3) != 0) {
        fprintf(stderr, "invalid GIF version\n");
        goto fail;
    }
    /* Width x Height */
    width  = read_num(fd);
    height = read_num(fd);
    /* FDSZ */
    read(fd, &fdsz, 1);
    /* Presence of GCT */
    if (!(fdsz & 0x80)) {
        fprintf(stderr, "no global color table\n");
        goto fail;
    }
    /* Color Space's Depth */
    depth = ((fdsz >> 4) & 7) + 1;
    /* Ignore Sort Flag. */
    /* GCT Size */
    gct_sz = 1 << ((fdsz & 0x07) + 1);
    /* Background Color Index */
    read(fd, &bgidx, 1);
    /* Aspect Ratio */
    read(fd, &aspect, 1);
    /* Create gd_GIF Structure. */
    gif = (gd_GIF*) calloc(1, sizeof(*gif) + 5 * width * height);
    if (!gif) goto fail;
    gif->fd = fd;
    gif->width  = width;
    gif->height = height;
    gif->depth  = depth;
    /* Read GCT */
    gif->gct.size = gct_sz;
    read(fd, gif->gct.colors, 3 * gif->gct.size);
    gif->palette = &gif->gct;
    gif->bgindex = bgidx;
    gif->canvas = (uint8_t *) &gif[1];
    gif->frame = &gif->canvas[4 * width * height];
    if (gif->bgindex)
        memset(gif->frame, gif->bgindex, gif->width * gif->height);
    gif->anim_start = lseek(fd, 0, SEEK_CUR);
    goto ok;
fail:
    close(fd);
ok:
    return gif;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq 0x28(%rsp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x32b0
movl %eax, 0x24(%rsp)
cmpl $-0x1, 0x24(%rsp)
jne 0x3ae60
movq $0x0, 0x30(%rsp)
jmp 0x3b10c
movl 0x24(%rsp), %edi
leaq 0x21(%rsp), %rsi
movl $0x3, %edx
callq 0x3520
leaq 0x21(%rsp), %rdi
leaq 0xd1a1(%rip), %rsi      # 0x48020
movl $0x3, %edx
callq 0x31b0
cmpl $0x0, %eax
je 0x3ae93
jmp 0x3b0f9
movl 0x24(%rsp), %edi
leaq 0x21(%rsp), %rsi
movl $0x3, %edx
callq 0x3520
leaq 0x21(%rsp), %rdi
leaq 0xd172(%rip), %rsi      # 0x48024
movl $0x3, %edx
callq 0x31b0
cmpl $0x0, %eax
je 0x3aede
movq 0x1f110(%rip), %rax     # 0x59fd8
movq (%rax), %rdi
leaq 0xd156(%rip), %rsi      # 0x48028
movb $0x0, %al
callq 0x3500
jmp 0x3b0f9
movl 0x24(%rsp), %edi
callq 0x3b250
movw %ax, 0x1e(%rsp)
movl 0x24(%rsp), %edi
callq 0x3b250
movw %ax, 0x1c(%rsp)
movl 0x24(%rsp), %edi
leaq 0x19(%rsp), %rsi
movl $0x1, %edx
callq 0x3520
movzbl 0x19(%rsp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x3af39
movq 0x1f0b5(%rip), %rax     # 0x59fd8
movq (%rax), %rdi
leaq 0xd110(%rip), %rsi      # 0x4803d
movb $0x0, %al
callq 0x3500
jmp 0x3b0f9
movzbl 0x19(%rsp), %eax
sarl $0x4, %eax
andl $0x7, %eax
addl $0x1, %eax
movw %ax, 0x1a(%rsp)
movzbl 0x19(%rsp), %ecx
andl $0x7, %ecx
addl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x10(%rsp)
movl 0x24(%rsp), %edi
leaq 0x18(%rsp), %rsi
movl $0x1, %edx
callq 0x3520
movl 0x24(%rsp), %edi
leaq 0x17(%rsp), %rsi
movl $0x1, %edx
callq 0x3520
movzwl 0x1e(%rsp), %eax
imull $0x5, %eax, %eax
movzwl 0x1c(%rsp), %ecx
imull %ecx, %eax
movslq %eax, %rsi
addq $0x670, %rsi            # imm = 0x670
movl $0x1, %edi
callq 0x3280
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x3afbe
jmp 0x3b0f9
movl 0x24(%rsp), %ecx
movq 0x8(%rsp), %rax
movl %ecx, (%rax)
movw 0x1e(%rsp), %cx
movq 0x8(%rsp), %rax
movw %cx, 0x10(%rax)
movw 0x1c(%rsp), %cx
movq 0x8(%rsp), %rax
movw %cx, 0x12(%rax)
movw 0x1a(%rsp), %cx
movq 0x8(%rsp), %rax
movw %cx, 0x14(%rax)
movl 0x10(%rsp), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x334(%rax)
movl 0x24(%rsp), %edi
movq 0x8(%rsp), %rsi
addq $0x334, %rsi            # imm = 0x334
addq $0x4, %rsi
movq 0x8(%rsp), %rax
imull $0x3, 0x334(%rax), %eax
movslq %eax, %rdx
callq 0x3520
movq 0x8(%rsp), %rcx
addq $0x334, %rcx            # imm = 0x334
movq 0x8(%rsp), %rax
movq %rcx, 0x28(%rax)
movb 0x18(%rsp), %cl
movq 0x8(%rsp), %rax
movb %cl, 0x658(%rax)
movq 0x8(%rsp), %rcx
addq $0x670, %rcx            # imm = 0x670
movq 0x8(%rsp), %rax
movq %rcx, 0x660(%rax)
movq 0x8(%rsp), %rax
movq 0x660(%rax), %rcx
movzwl 0x1e(%rsp), %eax
shll $0x2, %eax
movzwl 0x1c(%rsp), %edx
imull %edx, %eax
cltq
addq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x668(%rax)
movq 0x8(%rsp), %rax
cmpb $0x0, 0x658(%rax)
je 0x3b0d9
movq 0x8(%rsp), %rax
movq 0x668(%rax), %rdi
movq 0x8(%rsp), %rax
movzbl 0x658(%rax), %eax
movq 0x8(%rsp), %rcx
movzwl 0x10(%rcx), %ecx
movq 0x8(%rsp), %rdx
movzwl 0x12(%rdx), %edx
imull %edx, %ecx
movslq %ecx, %rdx
movzbl %al, %esi
callq 0x31f0
movl 0x24(%rsp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x36d0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
jmp 0x3b102
movl 0x24(%rsp), %edi
callq 0x36f0
movq 0x8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)