#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct stat {
  scalar_t__ st_size;
};
struct mg_connection {
  int dummy;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ mg_printf(struct mg_connection *, char *, unsigned long);
int /*<<< orphan*/ mg_write(struct mg_connection *, char *, int);
scalar_t__ stat(char const *, struct stat *);
void send_file(struct mg_connection *conn, const char *path) {
  char buf[1024];
  struct stat st;
  int n;
  FILE *fp;
  if (stat(path, &st) == 0 && (fp = fopen(path, "rb")) != NULL) {
    mg_printf(conn,
              "--w00t\r\nContent-Type: image/jpeg\r\n"
              "Content-Length: %lu\r\n\r\n",
              (unsigned long)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);
  }
}


x86 assembly (clang, -O0):
<send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    122d <send_file+0xcd>
mov    -0x10(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     122d <send_file+0xcd>
mov    -0x8(%rbp),%rdi
mov    -0x418(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1040 <mg_printf@plt>
lea    -0x410(%rbp),%rdi
mov    -0x428(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x41c(%rbp)
cmp    $0x0,%eax
jle    120c <send_file+0xac>
mov    -0x8(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1060 <mg_write@plt>
jmp    11c5 <send_file+0x65>
mov    -0x428(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x8(%rbp),%rdi
lea    0xe17(%rip),%rsi
mov    $0x2,%edx
call   1060 <mg_write@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jne    1222 <send_file+0xc2>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1222 <send_file+0xc2>
mov    %rax,%r15
mov    0x8(%rsp),%rdx
lea    0xe54(%rip),%rsi
mov    %r14,%rdi
call   1040 <mg_printf@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1206 <send_file+0xa6>
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <mg_write@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     11e0 <send_file+0x80>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xe25(%rip),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
call   1060 <mg_write@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.mg_connection = type { i32 }
%struct.stat = type { i64 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [58 x i8] c"--w00t\0D\0AContent-Type: image/jpeg\0D\0AContent-Length: %lu\0D\0A\0D\0A\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
define void @send_file(%struct.mg_connection* %0, i8* %1) {
%3 = alloca %struct.mg_connection*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32*
store %struct.mg_connection* %0, %struct.mg_connection** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = call i64 @stat(i8* %9, %struct.stat* %6)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %36
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %36
16: ; preds = %12
%17 = load %struct.mg_connection*, %struct.mg_connection** %3
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%19 = load i64, i64* %18
%20 = call i32 @mg_printf(%struct.mg_connection* %17, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %19)
br label %21
21: ; preds = %26, %16
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = load i32*, i32** %8
%24 = call i32 @fread(i8* %22, i32 1, i32 1024, i32* %23)
store i32 %24, i32* %7
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load %struct.mg_connection*, %struct.mg_connection** %3
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* %7
%30 = call i32 @mg_write(%struct.mg_connection* %27, i8* %28, i32 %29)
br label %21
31: ; preds = %21
%32 = load i32*, i32** %8
%33 = call i32 @fclose(i32* %32)
%34 = load %struct.mg_connection*, %struct.mg_connection** %3
%35 = call i32 @mg_write(%struct.mg_connection* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %36
36: ; preds = %31, %12, %2
ret void
}

LLVM IR (-O2):
%struct.mg_connection = type { i32 }
%struct.stat = type { i64 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [58 x i8] c"--w00t\0D\0AContent-Type: image/jpeg\0D\0AContent-Length: %lu\0D\0A\0D\0A\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
define void @send_file(%struct.mg_connection* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i64 @stat(i8* %1, %struct.stat* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %2
%10 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %26, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i64, i64* %13
%15 = call i32 @mg_printf(%struct.mg_connection* %0, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %14)
%16 = call i32 @fread(i8* %5, i32 1, i32 1024, i32* %10)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %12, %18
%19 = phi i32 [ %21, %18 ], [ %16, %12 ]
%20 = call i32 @mg_write(%struct.mg_connection* %0, i8* %5, i32 %19)
%21 = call i32 @fread(i8* %5, i32 1, i32 1024, i32* %10)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %18, label %23
23: ; preds = %18, %12
%24 = call i32 @fclose(i32* %10)
%25 = call i32 @mg_write(%struct.mg_connection* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %26
26: ; preds = %23, %9, %2
ret void
}