// File: /workspace/github_c_makefile_star2_O0/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
subq $0x330, %rsp            # imm = 0x330
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x2eae3(%rip), %rsi     # 0x31061
callq 0x2140
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x259a
movl $0x1, -0x4(%rbp)
jmp 0x2796
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x268c
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x267c
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
callq *%rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
shrq $0x10, %rax
movb %al, %dl
movq 0x6eb20(%rip), %rax     # 0x71110
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rsi
imull (%rsi), %ecx
imull $0x3, %ecx, %ecx
imull $0x3, -0x2c(%rbp), %esi
addl %esi, %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x28(%rbp), %rax
andq $0xff00, %rax           # imm = 0xFF00
shrq $0x8, %rax
movb %al, %dl
movq 0x6eaed(%rip), %rax     # 0x71110
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rsi
imull (%rsi), %ecx
imull $0x3, %ecx, %ecx
imull $0x3, -0x2c(%rbp), %esi
addl %esi, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x28(%rbp), %rax
andq $0xff, %rax
movb %al, %dl
movq 0x6eabe(%rip), %rax     # 0x71110
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rsi
imull (%rsi), %ecx
imull $0x3, %ecx, %ecx
imull $0x3, -0x2c(%rbp), %esi
addl %esi, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x25b8
jmp 0x267e
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x25a1
leaq -0x320(%rbp), %rdi
callq 0x8800
movq %rax, -0x278(%rbp)
leaq -0x278(%rbp), %rdi
movl $0x50, %esi
movl $0x248, %edx            # imm = 0x248
callq 0x2820
movq -0x20(%rbp), %rsi
leaq -0x278(%rbp), %rdi
callq 0x8420
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x248(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x244(%rbp)
movl $0x3, -0x240(%rbp)
movl $0x2, -0x23c(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x5510
leaq -0x278(%rbp), %rdi
movl $0x5a, %esi
movl $0x1, %edx
callq 0x5080
leaq -0x278(%rbp), %rdi
movl $0x1, %esi
callq 0x2cb0
movl -0x124(%rbp), %eax
cmpl -0x244(%rbp), %eax
jae 0x276e
movq 0x6e9d6(%rip), %rax     # 0x71110
imull $0x3, -0x124(%rbp), %ecx
movq -0x10(%rbp), %rdx
imull (%rdx), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x328(%rbp)
leaq -0x278(%rbp), %rdi
leaq -0x328(%rbp), %rsi
movl $0x1, %edx
callq 0x2d40
jmp 0x2725
leaq -0x278(%rbp), %rdi
callq 0x2a30
leaq -0x278(%rbp), %rdi
callq 0x29b0
movq -0x20(%rbp), %rdi
callq 0x2060
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x330, %rsp            # imm = 0x330
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)