// File: /workspace/github_c_makefile_star2_O1/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 0x61fba(%rip), %rsi     # 0x73481
callq 0x69863
testq %rax, %rax
je 0x114f8
cmpb $0x0, (%rax)
je 0x114f8
leaq 0x61fb6(%rip), %rsi     # 0x73493
movq %rbx, %rdi
xorl %eax, %eax
addq $0x6a8, %rsp            # imm = 0x6A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x69675
movq 0x8(%rbx), %rsi
leaq 0xa8(%rsp), %rdi
leaq 0x170(%rsp), %rdx
callq 0xcae0
leaq 0x180(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x6a088(%rip), %rsi     # 0x7b5ac
leaq 0x6a082(%rip), %rdx     # 0x7b5ad
leaq 0x170(%rsp), %rdi
callq 0xedfa
leaq 0x48(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x170(%rsp), %rdx
callq 0xf87f
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11564
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 0x12396
leaq 0x38(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0x61f0c(%rip), %rsi     # 0x734b1
leaq 0x80(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x11c7f
leaq 0x170(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x61dda(%rip), %rsi     # 0x733a3
leaq 0x61dd7(%rip), %rdx     # 0x733a7
callq 0xedfa
movq 0x10(%rsp), %rsi
movq 0x178(%rsp), %rdx
subq %rdx, %rsi
jae 0x115eb
xorl %ebp, %ebp
jmp 0x11603
leaq 0x8(%rsp), %rdi
leaq 0x170(%rsp), %rcx
callq 0xcbf0
testl %eax, %eax
sete %bpl
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11615
callq 0xc640
testb %bpl, %bpl
je 0x11629
movl $0xa, %r8d
leaq 0x61e92(%rip), %rcx     # 0x734b9
jmp 0x11699
leaq 0x170(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x61d6d(%rip), %rsi     # 0x733a8
leaq 0x61d6a(%rip), %rdx     # 0x733ac
callq 0xedfa
movq 0x10(%rsp), %rsi
movq 0x178(%rsp), %rdx
subq %rdx, %rsi
jae 0x1165d
xorl %ebp, %ebp
jmp 0x11675
leaq 0x8(%rsp), %rdi
leaq 0x170(%rsp), %rcx
callq 0xcbf0
testl %eax, %eax
sete %bpl
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11687
callq 0xc640
testb %bpl, %bpl
je 0x116a3
movl $0x9, %r8d
leaq 0x61e2b(%rip), %rcx     # 0x734c4
movq 0x30(%rsp), %rdx
jmp 0x1172a
leaq 0x170(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x61cf8(%rip), %rsi     # 0x733ad
leaq 0x61cf5(%rip), %rdx     # 0x733b1
callq 0xedfa
movq 0x10(%rsp), %rsi
movq 0x178(%rsp), %rdx
subq %rdx, %rsi
jae 0x116d7
xorl %ebp, %ebp
jmp 0x116ef
leaq 0x8(%rsp), %rdi
leaq 0x170(%rsp), %rcx
callq 0xcbf0
testl %eax, %eax
sete %bpl
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11701
callq 0xc640
movq 0x30(%rsp), %rdx
leaq 0x61dc1(%rip), %rax     # 0x734ce
leaq 0x61dc4(%rip), %rcx     # 0x734d8
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 0x11bfe
movq %rax, %r15
leaq 0x70(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x61d33(%rip), %rsi     # 0x734f1
leaq 0x61d39(%rip), %rdx     # 0x734fe
leaq 0x60(%rsp), %rdi
callq 0xedfa
addq $0x38, %r15
leaq 0x60(%rsp), %rsi
movq %r15, %rdi
callq 0x12426
movslq (%rax), %rax
imulq $0x15180, %rax, %rax    # imm = 0x15180
addq %rax, %r14
movq 0x60(%rsp), %rdi
cmpq %rbp, %rdi
je 0x117fc
callq 0xc640
leaq 0xa0(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x5e0(%rsp), %r14
movq %r14, %rsi
callq 0xc8f0
leaq 0x61950(%rip), %rdx     # 0x7316e
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 0x11862
leaq 0x61d3c(%rip), %rsi     # 0x7358f
movq %rbx, %rdi
xorl %eax, %eax
callq 0x69675
jmp 0x118f6
movq 0x80(%rsp), %rdi
leaq 0x61c8e(%rip), %rsi     # 0x734ff
callq 0xcd00
testq %rax, %rax
je 0x1184c
movq %rax, %r14
movq 0x28(%rsp), %rdx
movl 0x648(%rsp), %r8d
leaq 0x61c70(%rip), %rsi     # 0x73502
leaq 0x570(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x69675
leaq 0x170(%rsp), %r15
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %r14, %rcx
callq 0xc600
testl %eax, %eax
jle 0x118da
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x69bf7
jmp 0x118ac
movq %r14, %rdi
callq 0xc4c0
leaq 0x68aa1(%rip), %rsi     # 0x7a38a
movl $0x2, %edx
movq %rbx, %rdi
callq 0x69bf7
leaq 0x100(%rsp), %rbx
leaq 0x138(%rsp), %rdi
callq 0x11d00
movq %rbx, %rdi
callq 0x11d96
leaq 0xc8(%rsp), %rdi
callq 0x11e2c
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11936
callq 0xc640
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x11945
callq 0xc640
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11954
callq 0xc640
leaq 0x48(%rsp), %rdi
callq 0x11c3c
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11974
callq 0xc640
addq $0x6a8, %rsp            # imm = 0x6A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1198e
jmp 0x119bd
jmp 0x1198e
jmp 0x119bd
movq %rax, %rbx
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x119fa
callq 0xc640
jmp 0x119fa
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %rbp, %rdi
je 0x119ed
callq 0xc640
jmp 0x119ed
jmp 0x119ea
jmp 0x119bd
movq %rax, %rbx
jmp 0x119fa
movq %rax, %rbx
jmp 0x11a10
movq %rax, %rbx
jmp 0x11a2e
movq %rax, %rbx
movq 0x170(%rsp), %rdi
cmpq %r14, %rdi
je 0x11a38
callq 0xc640
jmp 0x11a38
movq %rax, %rbx
jmp 0x11a38
jmp 0x119ea
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x11c1c
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11a10
callq 0xc640
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x11a1f
callq 0xc640
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x11a2e
callq 0xc640
leaq 0x48(%rsp), %rdi
callq 0x11c3c
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11a4e
callq 0xc640
movq %rbx, %rdi
callq 0xc760