// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x818, %rsp            # imm = 0x818
movq %rdi, 0x810(%rsp)
movq 0x810(%rsp), %rdi
leaq 0xab96d(%rip), %rsi     # 0xbf45b
callq 0xb0fe0
movq %rax, 0x808(%rsp)
cmpq $0x0, 0x808(%rsp)
je 0x13b3a
movq 0x808(%rsp), %rdi
leaq 0xb2036(%rip), %rsi     # 0xc5b4b
callq 0xdab0
cmpl $0x0, %eax
je 0x13b3a
movq 0x810(%rsp), %rdi
leaq 0xab93f(%rip), %rsi     # 0xbf46d
movb $0x0, %al
callq 0xb0cb0
jmp 0x14281
movq 0x810(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x88(%rsp)
leaq 0x7e7(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0xdc30
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
leaq 0x7e8(%rsp), %rdi
callq 0xdad0
jmp 0x13b82
leaq 0x7e7(%rsp), %rdi
callq 0xdf30
leaq 0x797(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0xdc30
movq 0x80(%rsp), %rdx
leaq 0xb42f9(%rip), %rsi     # 0xc7eac
leaq 0x798(%rsp), %rdi
callq 0xdad0
jmp 0x13bc2
leaq 0x7b8(%rsp), %rdi
leaq 0x7e8(%rsp), %rsi
leaq 0x798(%rsp), %rdx
callq 0x10e50
jmp 0x13be1
leaq 0x798(%rsp), %rdi
callq 0xdf50
leaq 0x797(%rsp), %rdi
callq 0xdf30
leaq 0x7b8(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x148f0
movq 0x78(%rsp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x14910
movq %rax, %rsi
leaq 0x770(%rsp), %rdi
callq 0xd1b0
jmp 0x13c2f
leaq 0x750(%rsp), %rdi
callq 0xd480
leaq 0xab848(%rip), %rsi     # 0xbf48b
leaq 0x730(%rsp), %rdi
leaq 0x770(%rsp), %rdx
callq 0x149e0
jmp 0x13c5a
leaq 0x70f(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0xdc30
movq 0x70(%rsp), %rdx
leaq 0xab705(%rip), %rsi     # 0xbf37d
leaq 0x710(%rsp), %rdi
callq 0xdad0
jmp 0x13c87
leaq 0x770(%rsp), %rdi
leaq 0x710(%rsp), %rsi
callq 0x115c0
movb %al, 0x6f(%rsp)
jmp 0x13ca2
leaq 0x710(%rsp), %rdi
callq 0xdf50
leaq 0x70f(%rsp), %rdi
callq 0xdf30
movb 0x6f(%rsp), %al
testb $0x1, %al
jne 0x13cc9
jmp 0x13de7
leaq 0xab7c3(%rip), %rsi     # 0xbf493
leaq 0x750(%rsp), %rdi
callq 0xd560
jmp 0x13cdf
jmp 0x13fa0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
leaq 0x7e7(%rsp), %rdi
callq 0xdf30
jmp 0x142d7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x13d41
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
leaq 0x798(%rsp), %rdi
callq 0xdf50
leaq 0x797(%rsp), %rdi
callq 0xdf30
jmp 0x142ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x142bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x142a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x13dbc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
leaq 0x710(%rsp), %rdi
callq 0xdf50
leaq 0x70f(%rsp), %rdi
callq 0xdf30
jmp 0x14296
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x14296
leaq 0x6e7(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0xdc30
movq 0x60(%rsp), %rdx
leaq 0xab57d(%rip), %rsi     # 0xbf382
leaq 0x6e8(%rsp), %rdi
callq 0xdad0
jmp 0x13e14
leaq 0x770(%rsp), %rdi
leaq 0x6e8(%rsp), %rsi
callq 0x115c0
movb %al, 0x5f(%rsp)
jmp 0x13e2f
leaq 0x6e8(%rsp), %rdi
callq 0xdf50
leaq 0x6e7(%rsp), %rdi
callq 0xdf30
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0x13e53
jmp 0x13eb7
leaq 0xab644(%rip), %rsi     # 0xbf49e
leaq 0x750(%rsp), %rdi
callq 0xd560
jmp 0x13e69
jmp 0x13f9e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x13ea5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
leaq 0x6e8(%rsp), %rdi
callq 0xdf50
leaq 0x6e7(%rsp), %rdi
callq 0xdf30
jmp 0x14296
leaq 0x6bf(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0xdc30
movq 0x50(%rsp), %rdx
leaq 0xab4b2(%rip), %rsi     # 0xbf387
leaq 0x6c0(%rsp), %rdi
callq 0xdad0
jmp 0x13ee4
leaq 0x770(%rsp), %rdi
leaq 0x6c0(%rsp), %rsi
callq 0x115c0
movb %al, 0x4f(%rsp)
jmp 0x13eff
leaq 0x6c0(%rsp), %rdi
callq 0xdf50
leaq 0x6bf(%rsp), %rdi
callq 0xdf30
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x13f23
jmp 0x13f84
leaq 0xab57e(%rip), %rsi     # 0xbf4a8
leaq 0x750(%rsp), %rdi
callq 0xd560
jmp 0x13f39
jmp 0x13f9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x13f72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
leaq 0x6c0(%rsp), %rdi
callq 0xdf50
leaq 0x6bf(%rsp), %rdi
callq 0xdf30
jmp 0x14296
leaq 0xab527(%rip), %rsi     # 0xbf4b2
leaq 0x750(%rsp), %rdi
callq 0xd560
jmp 0x13f9a
jmp 0x13f9c
jmp 0x13f9e
jmp 0x13fa0
leaq 0x170(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x14680
xorl %eax, %eax
movl %eax, %edi
callq 0xd980
movq 0x30(%rsp), %rdi
movq %rax, 0x38(%rsp)
callq 0x146c0
movq %rax, 0x40(%rsp)
jmp 0x13fd1
leaq 0x9f(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xdc30
movq 0x28(%rsp), %rdx
leaq 0xab4dc(%rip), %rsi     # 0xbf4cb
leaq 0xa0(%rsp), %rdi
callq 0xdad0
jmp 0x13ffe
movq 0x40(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x14720
movl %eax, 0x24(%rsp)
jmp 0x14016
movq 0x38(%rsp), %rax
movl 0x24(%rsp), %ecx
imull $0x15180, %ecx, %ecx    # imm = 0x15180
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xdf50
leaq 0x9f(%rsp), %rdi
callq 0xdf30
movq 0x18(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xda30
leaq 0x100(%rsp), %rdi
movl $0x64, %esi
leaq 0xab0c8(%rip), %rdx     # 0xbf148
leaq 0xc8(%rsp), %rcx
callq 0xdb40
leaq 0x730(%rsp), %rdi
callq 0xd190
movq %rax, %rdi
leaq 0x620(%rsp), %rsi
callq 0xd150
cmpl $0x0, %eax
jne 0x14219
leaq 0x730(%rsp), %rdi
callq 0xd190
movq %rax, %rdi
leaq 0xab40f(%rip), %rsi     # 0xbf4d9
callq 0xdef0
movq %rax, 0x10(%rsp)
jmp 0x140d6
movq 0x10(%rsp), %rax
movq %rax, 0x6b0(%rsp)
cmpq $0x0, %rax
je 0x14219
movq 0x810(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x750(%rsp), %rdi
callq 0xd190
movq 0x8(%rsp), %rdi
movq %rax, %rdx
movl 0x650(%rsp), %r8d
leaq 0xab3be(%rip), %rsi     # 0xbf4dc
xorl %eax, %eax
leaq 0x100(%rsp), %rcx
callq 0xb0cb0
jmp 0x1412f
jmp 0x14131
movq 0x6b0(%rsp), %rcx
leaq 0x220(%rsp), %rdi
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
callq 0xd720
movq %rax, (%rsp)
jmp 0x14156
movq (%rsp), %rax
movl %eax, 0x21c(%rsp)
cmpl $0x0, %eax
jle 0x141ed
movq 0x810(%rsp), %rdi
movslq 0x21c(%rsp), %rdx
leaq 0x220(%rsp), %rsi
callq 0xb1580
jmp 0x14189
jmp 0x14131
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x14289
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
jmp 0x141db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7d8(%rsp)
movl %eax, 0x7d4(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xdf50
leaq 0x9f(%rsp), %rdi
callq 0xdf30
jmp 0x14289
movq 0x6b0(%rsp), %rdi
callq 0xd5b0
jmp 0x141fc
movq 0x810(%rsp), %rdi
leaq 0xb2a66(%rip), %rsi     # 0xc6c71
movl $0x2, %edx
callq 0xb1580
jmp 0x14217
jmp 0x14233
movq 0x810(%rsp), %rdi
leaq 0xab341(%rip), %rsi     # 0xbf569
xorl %eax, %eax
callq 0xb0cb0
jmp 0x14231
jmp 0x14233
leaq 0x170(%rsp), %rdi
callq 0x14880
leaq 0x730(%rsp), %rdi
callq 0xdf50
leaq 0x750(%rsp), %rdi
callq 0xdf50
leaq 0x770(%rsp), %rdi
callq 0xdf50
leaq 0x7b8(%rsp), %rdi
callq 0x14930
leaq 0x7e8(%rsp), %rdi
callq 0xdf50
addq $0x818, %rsp            # imm = 0x818
retq
leaq 0x170(%rsp), %rdi
callq 0x14880
leaq 0x730(%rsp), %rdi
callq 0xdf50
leaq 0x750(%rsp), %rdi
callq 0xdf50
leaq 0x770(%rsp), %rdi
callq 0xdf50
leaq 0x7b8(%rsp), %rdi
callq 0x14930
leaq 0x7e8(%rsp), %rdi
callq 0xdf50
movq 0x7d8(%rsp), %rdi
callq 0xd8a0
nopw %cs:(%rax,%rax)