// File: /workspace/github_c_makefile_star2_O1/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 0x2eb76(%rip), %rsi     # 0x3103d
movq %rax, %rdi
callq 0x2140
testq %rax, %rax
je 0x262e
movq %rax, %rbx
cmpl $0x0, 0x4(%r14)
jle 0x2553
xorl %r15d, %r15d
cmpl $0x0, (%r14)
jle 0x254a
xorl %r12d, %r12d
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq *0x68(%r14)
movl %eax, %edx
shrl $0x10, %edx
movq 0x6ec09(%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
cmpl (%r14), %r12d
jl 0x24ee
incl %r15d
cmpl 0x4(%r14), %r15d
jl 0x24e5
leaq -0x318(%rbp), %rdi
callq 0x86a0
leaq -0x270(%rbp), %r15
movq %rax, (%r15)
movl $0x248, %edx            # imm = 0x248
movq %r15, %rdi
movl $0x50, %esi
callq 0x26c0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x82c0
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 0x53b0
movq %r15, %rdi
movl $0x5a, %esi
movl $0x1, %edx
callq 0x4f20
movq %r15, %rdi
movl $0x1, %esi
callq 0x2b50
movl 0x154(%r15), %eax
cmpl 0x34(%r15), %eax
jae 0x260b
leaq -0x28(%rbp), %r12
imull (%r14), %eax
leal (%rax,%rax,2), %eax
addq 0x6eb27(%rip), %rax     # 0x71110
movq %rax, -0x28(%rbp)
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x2be0
movl -0x11c(%rbp), %eax
cmpl -0x23c(%rbp), %eax
jb 0x25db
leaq -0x270(%rbp), %r14
movq %r14, %rdi
callq 0x28d0
movq %r14, %rdi
callq 0x2850
movq %rbx, %rdi
callq 0x2070
xorl %eax, %eax
jmp 0x2633
movl $0x1, %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop