#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 {
  int /*<<< orphan*/ st_size;
};
struct evhttp_request {
  int dummy;
};
struct evbuffer {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ evbuffer_add_file(struct evbuffer *, int, int /*<<< orphan*/,
                             int /*<<< orphan*/);
int /*<<< orphan*/ evbuffer_add_printf(struct evbuffer *, char *, char *);
struct evbuffer *evbuffer_new();
int /*<<< orphan*/ evhttp_send_reply(struct evhttp_request *, int, char *,
                                     struct evbuffer *);
scalar_t__ fstat(int, struct stat *);
char *get_request_host(struct evhttp_request *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void html_request_cb(struct evhttp_request *req, void *arg) {
  struct evbuffer *evb = evbuffer_new();
  int fd;
  char *url;
  strcpy(url, "/home/yilang/www/");
  strcat(url, "index.html");
  if ((fd = open(url, O_RDONLY)) < 0) {
    perror("open");
    return;
  }
  struct stat st;
  if ((fstat(fd, &st)) < 0) {
    perror("fstat");
    return;
  }
  evbuffer_add_printf(
      evb, "%s", "<h1><center>fake baidu for html request!</center></h1>\n");
  evbuffer_add_printf(evb, "host:%s", get_request_host(req));
  if ((evbuffer_add_file(evb, fd, 0, st.st_size)) < 0) {
    perror("evbuffer_add_file");
    return;
  }
  evhttp_send_reply(req, 200, "ok", evb);
}


x86 assembly (clang, -O0):
<html_request_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <evbuffer_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    120a <html_request_cb+0x6a>
lea    0xe1d(%rip),%rdi
call   1090 <perror@plt>
jmp    12b1 <html_request_cb+0x111>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
jge    1231 <html_request_cb+0x91>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
jmp    12b1 <html_request_cb+0x111>
mov    -0x18(%rbp),%rdi
lea    0xe29(%rip),%rsi
lea    0xde5(%rip),%rdx
call   1050 <evbuffer_add_printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <get_request_host@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rdx
lea    0xdf9(%rip),%rsi
call   1050 <evbuffer_add_printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%ecx
xor    %edx,%edx
call   1040 <evbuffer_add_file@plt>
cmp    $0x0,%rax
jge    1298 <html_request_cb+0xf8>
lea    0xdda(%rip),%rdi
call   1090 <perror@plt>
jmp    12b1 <html_request_cb+0x111>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0xc8,%esi
lea    0xdce(%rip),%rdx
call   10b0 <evhttp_send_reply@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<html_request_cb>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <evbuffer_new@plt>
mov    %rax,%rbx
lea    0xe44(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xe4a(%rip),%rsi
call   10a0 <strcat@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1256 <html_request_cb+0xb6>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
test   %rax,%rax
js     125f <html_request_cb+0xbf>
lea    0xe6c(%rip),%rsi
lea    0xe28(%rip),%rdx
mov    %rbx,%rdi
call   1050 <evbuffer_add_printf@plt>
mov    %r14,%rdi
call   1060 <get_request_host@plt>
lea    0xe49(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1050 <evbuffer_add_printf@plt>
mov    0x8(%rsp),%ecx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1040 <evbuffer_add_file@plt>
test   %rax,%rax
js     1268 <html_request_cb+0xc8>
lea    0xe3c(%rip),%rdx
mov    %r14,%rdi
mov    $0xc8,%esi
mov    %rbx,%rcx
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    10b0 <evhttp_send_reply@plt>
lea    0xdc0(%rip),%rdi
jmp    126f <html_request_cb+0xcf>
lea    0xdbc(%rip),%rdi
jmp    126f <html_request_cb+0xcf>
lea    0xdf9(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <perror@plt>

LLVM IR (-O0):
%struct.evhttp_request = type { i32 }
%struct.evbuffer = type { i32 }
%struct.stat = type { i32 }
@.str = [18 x i8] c"/home/yilang/www/\00"
@.str.1 = [11 x i8] c"index.html\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"fstat\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [56 x i8] c"<h1><center>fake baidu for html request!</center></h1>\0A\00"
@.str.6 = [8 x i8] c"host:%s\00"
@.str.7 = [18 x i8] c"evbuffer_add_file\00"
@.str.8 = [3 x i8] c"ok\00"
define void @html_request_cb(%struct.evhttp_request* %0, i8* %1) {
%3 = alloca %struct.evhttp_request*
%4 = alloca i8*
%5 = alloca %struct.evbuffer*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
store %struct.evhttp_request* %0, %struct.evhttp_request** %3
store i8* %1, i8** %4
%9 = call %struct.evbuffer* (...) @evbuffer_new()
store %struct.evbuffer* %9, %struct.evbuffer** %5
%10 = load i8*, i8** %7
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %7
%13 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%14 = load i8*, i8** %7
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %45
20: ; preds = %2
%21 = load i32, i32* %6
%22 = call i64 @fstat(i32 %21, %struct.stat* %8)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %45
26: ; preds = %20
%27 = load %struct.evbuffer*, %struct.evbuffer** %5
%28 = call i32 @evbuffer_add_printf(%struct.evbuffer* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0))
%29 = load %struct.evbuffer*, %struct.evbuffer** %5
%30 = load %struct.evhttp_request*, %struct.evhttp_request** %3
%31 = call i8* @get_request_host(%struct.evhttp_request* %30)
%32 = call i32 @evbuffer_add_printf(%struct.evbuffer* %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* %31)
%33 = load %struct.evbuffer*, %struct.evbuffer** %5
%34 = load i32, i32* %6
%35 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @evbuffer_add_file(%struct.evbuffer* %33, i32 %34, i32 0, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %26
%40 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %45
41: ; preds = %26
%42 = load %struct.evhttp_request*, %struct.evhttp_request** %3
%43 = load %struct.evbuffer*, %struct.evbuffer** %5
%44 = call i32 @evhttp_send_reply(%struct.evhttp_request* %42, i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), %struct.evbuffer* %43)
br label %45
45: ; preds = %41, %39, %24, %18
ret void
}

LLVM IR (-O2):
%struct.evhttp_request = type { i32 }
%struct.stat = type { i32 }
%struct.evbuffer = type { i32 }
@.str = [18 x i8] c"/home/yilang/www/\00"
@.str.1 = [11 x i8] c"index.html\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [6 x i8] c"fstat\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [56 x i8] c"<h1><center>fake baidu for html request!</center></h1>\0A\00"
@.str.6 = [8 x i8] c"host:%s\00"
@.str.7 = [18 x i8] c"evbuffer_add_file\00"
@.str.8 = [3 x i8] c"ok\00"
define void @html_request_cb(%struct.evhttp_request* %0, i8* nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = tail call %struct.evbuffer* (...) @evbuffer_new()
%5 = tail call i32 @strcpy(i8* undef, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @strcat(i8* undef, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* undef, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %31
12: ; preds = %2
%13 = bitcast %struct.stat* %3 to i8*
%14 = call i64 @fstat(i32 %8, %struct.stat* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %30
18: ; preds = %12
%19 = tail call i32 @evbuffer_add_printf(%struct.evbuffer* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0))
%20 = tail call i8* @get_request_host(%struct.evhttp_request* %0)
%21 = tail call i32 @evbuffer_add_printf(%struct.evbuffer* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* %20)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i64 @evbuffer_add_file(%struct.evbuffer* %4, i32 %8, i32 0, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %30
28: ; preds = %18
%29 = tail call i32 @evhttp_send_reply(%struct.evhttp_request* %0, i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), %struct.evbuffer* %4)
br label %30
30: ; preds = %28, %26, %16
br label %31
31: ; preds = %30, %10
ret void
}