// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./src/helper.cpp
// Function: cc_serve_image_file(mg_connection*)
void cc_serve_image_file(mg_connection* conn){
    const char *ims = mg_get_header(conn, "If-Modified-Since");
    
    if (ims && (strcmp(ims, "") != 0)){
        mg_printf(conn, "HTTP/1.1 304 Not Modified\r\n\r\n");
        return;
    }

    string url(conn->uri);
    vector<string> tmp = cc_split(url, "/");
    string fname = tmp[tmp.size() - 1];
    string ctype;

    string path = "images/" + fname;

    if (cc_ends_with(fname, ".jpg"))
        ctype = "image/jpeg";
    else if (cc_ends_with(fname, ".gif"))
        ctype = "image/gif";
    else if (cc_ends_with(fname, ".png"))
        ctype = "image/png";
    else
        ctype = "application/octet-stream";

    FILE *fp; 
    struct stat st;
    char buf[1024];
    int n;

    ConfigManager c;

    char expire[100];
    struct tm exp;
    time_t t = time(NULL) + 60 * 60 * 24 * c.global().get<int>("image::expire");
    gmtime_r(&t, &exp);
    strftime(expire, sizeof(expire), "%a, %d %b %Y %H:%M:%S GMT", &exp);

    if (stat(path.c_str(), &st) == 0 && (fp = fopen(path.c_str(), "rb")) != NULL) {
        mg_printf(conn, "HTTP/1.1 200 OK\r\n"
                        "Content-Type: %s\r\n"
                        "Last-Modified: Sat, 31 Jul 1993 00:00:00 GMT\r\n"
                        // "Expires: Tue, 31 Jul 2035 00:00:00 GMT\r\n"
                        "Expires: %s\r\n"
                        // "Cache-Control: must-revalidate\r\n"
                        "Cache-Control: Public\r\n"
                        "Content-Length: %lu\r\n\r\n", ctype.c_str(), expire, (unsigned int)st.st_size);
        while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
            mg_write(conn, buf, n);
        }
        fclose(fp);
        mg_write(conn, "\r\n", 2);
    }else{
        mg_printf(conn, "HTTP/1.1 404 Not Found\r\nContent-Type: image/jpeg\r\nContent-Length: 0\r\n\r\n");
    }   //image
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x6a0, %rsp            # imm = 0x6A0
movq %rdi, %rbx
leaq 0x519da(%rip), %rsi     # 0x61457
callq 0x58dc0
testq %rax, %rax
je 0xfaac
cmpb $0x0, (%rax)
je 0xfaac
leaq 0x519d6(%rip), %rsi     # 0x61469
movq %rbx, %rdi
xorl %eax, %eax
addq $0x6a0, %rsp            # imm = 0x6A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x58c06
movq 0x8(%rbx), %rsi
leaq 0xa8(%rsp), %rdi
leaq 0x210(%rsp), %rdx
callq 0xcb10
leaq 0x59ac0(%rip), %rsi     # 0x6958c
leaq 0x210(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0xcb10
leaq 0x28(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x210(%rsp), %rdx
callq 0xe782
leaq 0x210(%rsp), %rdi
callq 0xcd88
movq 0x30(%rsp), %rsi
addq $-0x20, %rsi
leaq 0x8(%rsp), %rdi
callq 0xc640
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x51954(%rip), %rsi     # 0x61487
leaq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x10196
leaq 0x51830(%rip), %rsi     # 0x61379
leaq 0x210(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0xcb10
leaq 0x8(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0xe9f6
movl %eax, %ebp
leaq 0x210(%rsp), %rdi
callq 0xcd88
testb %bpl, %bpl
je 0xfb90
leaq 0x51904(%rip), %rsi     # 0x6148f
jmp 0xfc2d
leaq 0x517e7(%rip), %rsi     # 0x6137e
leaq 0x210(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0xcb10
leaq 0x8(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0xe9f6
movl %eax, %ebp
leaq 0x210(%rsp), %rdi
callq 0xcd88
testb %bpl, %bpl
je 0xfbdb
leaq 0x518c1(%rip), %rsi     # 0x6149a
jmp 0xfc2d
leaq 0x517a1(%rip), %rsi     # 0x61383
leaq 0x210(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0xcb10
leaq 0x8(%rsp), %rdi
leaq 0x210(%rsp), %rsi
callq 0xe9f6
movl %eax, %ebp
leaq 0x210(%rsp), %rdi
callq 0xcd88
leaq 0x51885(%rip), %rax     # 0x614a4
leaq 0x51888(%rip), %rsi     # 0x614ae
testb %bpl, %bpl
cmovneq %rax, %rsi
leaq 0x60(%rsp), %rdi
callq 0xcd10
leaq 0xf8(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rcx
movq %rcx, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x130(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x168(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
xorl %edi, %edi
callq 0xc850
movq %rax, %r14
leaq 0xc8(%rsp), %rdi
callq 0x1008c
movq %rax, %r15
leaq 0x51813(%rip), %rsi     # 0x614c7
leaq 0x80(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0xcb10
leaq 0x80(%rsp), %rsi
movq %r15, %rdi
callq 0x100e4
imull $0x15180, %eax, %eax    # imm = 0x15180
cltq
addq %rax, %r14
leaq 0x80(%rsp), %rdi
callq 0xcd88
leaq 0xa0(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x1d8(%rsp), %r14
movq %r14, %rsi
callq 0xc900
leaq 0x51434(%rip), %rdx     # 0x61144
leaq 0x170(%rsp), %rdi
pushq $0x64
popq %rsi
movq %r14, %rcx
callq 0xca00
movq 0x40(%rsp), %rdi
leaq 0x610(%rsp), %rsi
callq 0xc120
testl %eax, %eax
je 0xfd4f
leaq 0x51825(%rip), %rsi     # 0x61565
movq %rbx, %rdi
xorl %eax, %eax
callq 0x58c06
jmp 0xfde0
movq 0x40(%rsp), %rdi
leaq 0x5177a(%rip), %rsi     # 0x614d5
callq 0xcd40
testq %rax, %rax
je 0xfd39
movq %rax, %r14
movq 0x60(%rsp), %rdx
movl 0x640(%rsp), %r8d
leaq 0x5175c(%rip), %rsi     # 0x614d8
leaq 0x170(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x58c06
leaq 0x210(%rsp), %r15
pushq $0x1
popq %r12
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rcx
callq 0xc610
testl %eax, %eax
jle 0xfdc6
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x59168
jmp 0xfd9a
movq %r14, %rdi
callq 0xc4c0
leaq 0x58595(%rip), %rsi     # 0x6836a
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x59168
leaq 0xc8(%rsp), %rdi
callq 0x10136
leaq 0x40(%rsp), %rdi
callq 0xcd88
leaq 0x60(%rsp), %rdi
callq 0xcd88
leaq 0x8(%rsp), %rdi
callq 0xcd88
leaq 0x28(%rsp), %rdi
callq 0x10156
leaq 0xa8(%rsp), %rdi
callq 0xcd88
addq $0x6a0, %rsp            # imm = 0x6A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0xfe50
jmp 0xfe62
jmp 0xfe50
jmp 0xfe62
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0xcd88
jmp 0xfe8d
jmp 0xfe8a
jmp 0xfe62
movq %rax, %rbx
leaq 0x210(%rsp), %rdi
callq 0xcd88
jmp 0xfe9a
movq %rax, %rbx
jmp 0xfe9a
movq %rax, %rbx
jmp 0xfea4
movq %rax, %rbx
jmp 0xfeb8
movq %rax, %rbx
leaq 0x210(%rsp), %rdi
callq 0xcd88
jmp 0xfec2
movq %rax, %rbx
jmp 0xfec2
jmp 0xfe8a
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x10136
leaq 0x40(%rsp), %rdi
callq 0xcd88
leaq 0x60(%rsp), %rdi
callq 0xcd88
leaq 0x8(%rsp), %rdi
callq 0xcd88
leaq 0x28(%rsp), %rdi
callq 0x10156
leaq 0xa8(%rsp), %rdi
callq 0xcd88
movq %rbx, %rdi
callq 0xc790