// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rsi, %rax
movq %rdi, %r14
leaq 0x2eba8(%rip), %rsi     # 0x3103d
movq %rax, %rdi
callq 0x2120
testq %rax, %rax
je 0x25e2
movq %rax, %rbx
xorl %r15d, %r15d
cmpl 0x4(%r14), %r15d
jge 0x2518
xorl %r12d, %r12d
cmpl (%r14), %r12d
jge 0x2513
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq *0x68(%r14)
movl %eax, %edx
shrl $0x10, %edx
movq 0x6ec3d(%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
jmp 0x24b5
incl %r15d
jmp 0x24ac
leaq -0x320(%rbp), %rdi
callq 0x85e0
leaq -0x278(%rbp), %r15
movq %rax, (%r15)
pushq $0x50
popq %rsi
movl $0x248, %edx            # imm = 0x248
movq %r15, %rdi
callq 0x2600
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8200
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 0x52f0
pushq $0x5a
popq %rsi
pushq $0x1
popq %r12
movq %r15, %rdi
movl %r12d, %edx
callq 0x4e60
movq %r15, %rdi
movl %r12d, %esi
callq 0x2a90
leaq -0x30(%rbp), %r13
movl -0x124(%rbp), %eax
cmpl -0x244(%rbp), %eax
jae 0x25bf
imull (%r14), %eax
leal (%rax,%rax,2), %eax
addq 0x6eb65(%rip), %rax     # 0x71110
movq %rax, -0x30(%rbp)
movq %r15, %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x2b20
jmp 0x258f
leaq -0x278(%rbp), %r14
movq %r14, %rdi
callq 0x2810
movq %r14, %rdi
callq 0x2790
movq %rbx, %rdi
callq 0x2070
xorl %eax, %eax
jmp 0x25e5
pushq $0x1
popq %rax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop