// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x6a8, %rsp            # imm = 0x6A8
movq %rdi, %rbx
leaq 0x5ffcc(%rip), %rsi     # 0x71481
callq 0x67c6a
testq %rax, %rax
je 0x114e6
cmpb $0x0, (%rax)
je 0x114e6
leaq 0x5ffc8(%rip), %rsi     # 0x71493
movq %rbx, %rdi
xorl %eax, %eax
addq $0x6a8, %rsp            # imm = 0x6A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x67a83
movq 0x8(%rbx), %rsi
leaq 0xa8(%rsp), %rdi
leaq 0x170(%rsp), %rdx
callq 0xcae0
leaq 0x180(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x6809a(%rip), %rsi     # 0x795ac
leaq 0x68094(%rip), %rdx     # 0x795ad
leaq 0x170(%rsp), %rdi
callq 0xedf0
leaq 0x48(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x170(%rsp), %rdx
callq 0xf87c
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11552
callq 0xc640
movq 0x50(%rsp), %rax
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq -0x20(%rax), %rsi
movq -0x18(%rax), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0x12380
leaq 0x38(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0x5ff1e(%rip), %rsi     # 0x714b1
leaq 0x80(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x11c73
leaq 0x170(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x5fdec(%rip), %rsi     # 0x713a3
leaq 0x5fde9(%rip), %rdx     # 0x713a7
callq 0xedf0
movq 0x10(%rsp), %rsi
movq 0x178(%rsp), %rdx
subq %rdx, %rsi
jae 0x115d9
xorl %ebp, %ebp
jmp 0x115f1
leaq 0x8(%rsp), %rdi
leaq 0x170(%rsp), %rcx
callq 0xcbf0
testl %eax, %eax
sete %bpl
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11603
callq 0xc640
testb %bpl, %bpl
je 0x11617
movl $0xa, %r8d
leaq 0x5fea4(%rip), %rcx     # 0x714b9
jmp 0x11687
leaq 0x170(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x5fd7f(%rip), %rsi     # 0x713a8
leaq 0x5fd7c(%rip), %rdx     # 0x713ac
callq 0xedf0
movq 0x10(%rsp), %rsi
movq 0x178(%rsp), %rdx
subq %rdx, %rsi
jae 0x1164b
xorl %ebp, %ebp
jmp 0x11663
leaq 0x8(%rsp), %rdi
leaq 0x170(%rsp), %rcx
callq 0xcbf0
testl %eax, %eax
sete %bpl
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11675
callq 0xc640
testb %bpl, %bpl
je 0x11691
movl $0x9, %r8d
leaq 0x5fe3d(%rip), %rcx     # 0x714c4
movq 0x30(%rsp), %rdx
jmp 0x11718
leaq 0x170(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x5fd0a(%rip), %rsi     # 0x713ad
leaq 0x5fd07(%rip), %rdx     # 0x713b1
callq 0xedf0
movq 0x10(%rsp), %rsi
movq 0x178(%rsp), %rdx
subq %rdx, %rsi
jae 0x116c5
xorl %ebp, %ebp
jmp 0x116dd
leaq 0x8(%rsp), %rdi
leaq 0x170(%rsp), %rcx
callq 0xcbf0
testl %eax, %eax
sete %bpl
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x116ef
callq 0xc640
movq 0x30(%rsp), %rdx
leaq 0x5fdd3(%rip), %rax     # 0x714ce
leaq 0x5fdd6(%rip), %rcx     # 0x714d8
testb %bpl, %bpl
cmovneq %rax, %rcx
movl $0x9, %eax
movl $0x18, %r8d
cmovneq %rax, %r8
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0xc5c0
leaq 0xf8(%rsp), %rax
movq %rax, -0x30(%rax)
movl $0x1, %ecx
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 0xc820
movq %rax, %r14
leaq 0xc8(%rsp), %rdi
callq 0x11bec
movq %rax, %r15
leaq 0x70(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x5fd45(%rip), %rsi     # 0x714f1
leaq 0x5fd4b(%rip), %rdx     # 0x714fe
leaq 0x60(%rsp), %rdi
callq 0xedf0
addq $0x38, %r15
leaq 0x60(%rsp), %rsi
movq %r15, %rdi
callq 0x12416
movslq (%rax), %rax
imulq $0x15180, %rax, %rax    # imm = 0x15180
addq %rax, %r14
movq 0x60(%rsp), %rdi
cmpq %rbp, %rdi
je 0x117ea
callq 0xc640
leaq 0xa0(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x5e0(%rsp), %r14
movq %r14, %rsi
callq 0xc8f0
leaq 0x5f962(%rip), %rdx     # 0x7116e
leaq 0x570(%rsp), %rdi
movl $0x64, %esi
movq %r14, %rcx
callq 0xc9d0
movq 0x80(%rsp), %rdi
leaq 0x618(%rsp), %rsi
callq 0xc110
testl %eax, %eax
je 0x11850
leaq 0x5fd4e(%rip), %rsi     # 0x7158f
movq %rbx, %rdi
xorl %eax, %eax
callq 0x67a83
jmp 0x118e4
movq 0x80(%rsp), %rdi
leaq 0x5fca0(%rip), %rsi     # 0x714ff
callq 0xcd00
testq %rax, %rax
je 0x1183a
movq %rax, %r14
movq 0x28(%rsp), %rdx
movl 0x648(%rsp), %r8d
leaq 0x5fc82(%rip), %rsi     # 0x71502
leaq 0x570(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x67a83
leaq 0x170(%rsp), %r15
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %r14, %rcx
callq 0xc600
testl %eax, %eax
jle 0x118c8
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x68033
jmp 0x1189a
movq %r14, %rdi
callq 0xc4c0
leaq 0x66ab3(%rip), %rsi     # 0x7838a
movl $0x2, %edx
movq %rbx, %rdi
callq 0x68033
leaq 0x100(%rsp), %rbx
leaq 0x138(%rsp), %rdi
callq 0x11cf4
movq %rbx, %rdi
callq 0x11d8a
leaq 0xc8(%rsp), %rdi
callq 0x11e20
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11924
callq 0xc640
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x11933
callq 0xc640
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11942
callq 0xc640
leaq 0x48(%rsp), %rdi
callq 0x11c2a
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11962
callq 0xc640
addq $0x6a8, %rsp            # imm = 0x6A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1197c
jmp 0x119ab
jmp 0x1197c
jmp 0x119ab
movq %rax, %rbx
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x119e8
callq 0xc640
jmp 0x119e8
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %rbp, %rdi
je 0x119db
callq 0xc640
jmp 0x119db
jmp 0x119d8
jmp 0x119ab
movq %rax, %rbx
jmp 0x119e8
movq %rax, %rbx
jmp 0x119fe
movq %rax, %rbx
jmp 0x11a1c
movq %rax, %rbx
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11a26
callq 0xc640
jmp 0x11a26
movq %rax, %rbx
jmp 0x11a26
jmp 0x119d8
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x11c0a
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x119fe
callq 0xc640
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x11a0d
callq 0xc640
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11a1c
callq 0xc640
leaq 0x48(%rsp), %rdi
callq 0x11c2a
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11a3c
callq 0xc640
movq %rbx, %rdi
callq 0xc760