// File: /workspace/github_c_makefile_star2_O3/yueyoum[P]termgif/wjpeg.c
// Function: write_jpeg
int write_jpeg(XImage *img, const char *filename)
{
    FILE *fp;
    unsigned long pixel;
    int x, y;
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    JSAMPROW row_prt;

    fp = fopen(filename, "wb");
    if (!fp) return 1;

    for(y=0; y<img->height; y++)
    {
        for(x=0; x<img->width; x++)
        {
            pixel = XGetPixel(img, x, y);
            buf[y*img->width*3 + x*3 + 0] = pixel >> 16;
            buf[y*img->width*3 + x*3 + 1] = (pixel & 0x00ff00) >> 8;
            buf[y*img->width*3 + x*3 + 2] = (pixel & 0x0000ff);
        }
    }

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, fp);

    cinfo.image_width = img->width;
    cinfo.image_height = img->height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;

    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, 90, TRUE);
    jpeg_start_compress(&cinfo, TRUE);

    while(cinfo.next_scanline < cinfo.image_height)
    {
        row_prt = (JSAMPROW) &buf[cinfo.next_scanline * 3 * img->width];
        jpeg_write_scanlines(&cinfo, &row_prt, 1);
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
    fclose(fp);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x300, %rsp            # imm = 0x300
movq %rsi, %rax
movq %rdi, %r14
leaq 0x2eb96(%rip), %rsi     # 0x3103d
movq %rax, %rdi
callq 0x2120
testq %rax, %rax
je 0x2616
movq %rax, %rbx
movl 0x4(%r14), %ecx
testl %ecx, %ecx
jle 0x253b
movl (%r14), %eax
xorl %r15d, %r15d
testl %eax, %eax
jle 0x2533
xorl %r12d, %r12d
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq *0x68(%r14)
movl %eax, %edx
shrl $0x10, %edx
movq 0x6ec27(%rip), %rcx     # 0x71110
movl (%r14), %esi
imull %r15d, %esi
addl %r12d, %esi
leal (%rsi,%rsi,2), %esi
movslq %esi, %rsi
movb %dl, (%rcx,%rsi)
movl (%r14), %edx
imull %r15d, %edx
addl %r12d, %edx
leal (%rdx,%rdx,2), %edx
movslq %edx, %rdx
movb %ah, 0x1(%rcx,%rdx)
movl (%r14), %edx
imull %r15d, %edx
addl %r12d, %edx
leal (%rdx,%rdx,2), %edx
movslq %edx, %rdx
movb %al, 0x2(%rcx,%rdx)
incl %r12d
movl (%r14), %eax
cmpl %eax, %r12d
jl 0x24d0
movl 0x4(%r14), %ecx
incl %r15d
cmpl %ecx, %r15d
jl 0x24c9
leaq -0x318(%rbp), %rdi
callq 0x8610
leaq -0x270(%rbp), %r15
movq %rax, (%r15)
movl $0x248, %edx            # imm = 0x248
movq %r15, %rdi
movl $0x50, %esi
callq 0x2630
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8230
movl (%r14), %eax
movl %eax, 0x30(%r15)
movl 0x4(%r14), %eax
movl %eax, 0x34(%r15)
movabsq $0x200000003, %rax      # imm = 0x200000003
movq %rax, 0x38(%r15)
movq %r15, %rdi
callq 0x5320
movq %r15, %rdi
movl $0x5a, %esi
movl $0x1, %edx
callq 0x4e90
movq %r15, %rdi
movl $0x1, %esi
callq 0x2ac0
movl 0x154(%r15), %eax
cmpl 0x34(%r15), %eax
jae 0x25f3
leaq -0x28(%rbp), %r12
imull (%r14), %eax
leal (%rax,%rax,2), %eax
addq 0x6eb3f(%rip), %rax     # 0x71110
movq %rax, -0x28(%rbp)
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x2b50
movl -0x11c(%rbp), %eax
cmpl -0x23c(%rbp), %eax
jb 0x25c3
leaq -0x270(%rbp), %r14
movq %r14, %rdi
callq 0x2840
movq %r14, %rdi
callq 0x27c0
movq %rbx, %rdi
callq 0x2070
xorl %eax, %eax
jmp 0x261b
movl $0x1, %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop