#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 evhttp_uri {
  int dummy;
};
struct evhttp_request {
  int dummy;
};
struct evbuffer {
  int dummy;
};

/* Variables and functions */
scalar_t__ EVHTTP_REQ_GET;
int HTTP_BADREQUEST;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dump_request_cb(struct evhttp_request *, void *);
int /*<<< orphan*/ evbuffer_free(struct evbuffer *);
struct evbuffer *evbuffer_new();
scalar_t__ evhttp_request_get_command(struct evhttp_request *);
char *evhttp_request_get_uri(struct evhttp_request *);
int /*<<< orphan*/ evhttp_send_error(struct evhttp_request *, int, char *);
int /*<<< orphan*/ evhttp_send_reply(struct evhttp_request *, int, char *,
                                     struct evbuffer *);
int /*<<< orphan*/ evhttp_uri_free(struct evhttp_uri *);
char *evhttp_uri_get_path(struct evhttp_uri *);
struct evhttp_uri *evhttp_uri_parse(char const *);
char *evhttp_uridecode(char const *, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
void send_document_cb(struct evhttp_request *req, void *arg) {
  struct evbuffer *evb = NULL;
  const char *uri = evhttp_request_get_uri(req);
  struct evhttp_uri *decoded = NULL;
  const char *path;
  char *decoded_path;
  char *whole_path = NULL;
  int fd = -1;
  if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) {
    dump_request_cb(req, arg);
    return;
  }
  printf("Got a GET request for <%s>\n", uri);
  decoded = evhttp_uri_parse(uri);
  if (!decoded) {
    printf("It's not a good URI. Sending BADREQUEST\n");
    evhttp_send_error(req, HTTP_BADREQUEST, 0);
    return;
  }
  path = evhttp_uri_get_path(decoded);
  if (!path)
    path = "/";
  decoded_path = evhttp_uridecode(path, 0, NULL);
  if (decoded_path == NULL)
    goto err;
  evb = evbuffer_new();
  evhttp_send_reply(req, 200, "OK", evb);
  goto done;
err:
  evhttp_send_error(req, 404, "Document was not found");
  if (fd >= 0)
    close(fd);
done:
  if (decoded)
    evhttp_uri_free(decoded);
  if (decoded_path)
    free(decoded_path);
  if (whole_path)
    free(whole_path);
  if (evb)
    evbuffer_free(evb);
}


x86 assembly (clang, -O0):
<send_document_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <evhttp_request_get_uri@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0xffffffff,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <evhttp_request_get_command@plt>
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
je     1247 <send_document_cb+0x67>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <dump_request_cb@plt>
jmp    1385 <send_document_cb+0x1a5>
mov    -0x20(%rbp),%rsi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10c0 <evhttp_uri_parse@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    129a <send_document_cb+0xba>
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   10f0 <evhttp_send_error@plt>
jmp    1385 <send_document_cb+0x1a5>
mov    -0x28(%rbp),%rdi
call   1090 <evhttp_uri_get_path@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12bd <send_document_cb+0xdd>
lea    0xd8c(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <evhttp_uridecode@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12e0 <send_document_cb+0x100>
jmp    1309 <send_document_cb+0x129>
mov    $0x0,%al
call   10d0 <evbuffer_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0xc8,%esi
lea    0xd48(%rip),%rdx
call   1100 <evhttp_send_reply@plt>
jmp    1335 <send_document_cb+0x155>
mov    -0x8(%rbp),%rdi
mov    $0x194,%esi
lea    0xd31(%rip),%rdx
call   10f0 <evhttp_send_error@plt>
cmpl   $0x0,-0x44(%rbp)
jl     1330 <send_document_cb+0x150>
mov    -0x44(%rbp),%edi
call   1080 <close@plt>
jmp    1335 <send_document_cb+0x155>
cmpq   $0x0,-0x28(%rbp)
je     1349 <send_document_cb+0x169>
mov    -0x28(%rbp),%rdi
call   10e0 <evhttp_uri_free@plt>
cmpq   $0x0,-0x38(%rbp)
je     135d <send_document_cb+0x17d>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     1371 <send_document_cb+0x191>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
je     1385 <send_document_cb+0x1a5>
mov    -0x18(%rbp),%rdi
call   1070 <evbuffer_free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_document_cb>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r13
call   10a0 <evhttp_request_get_uri@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1050 <evhttp_request_get_command@plt>
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <send_document_cb+0xc6>
lea    0xdea(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   10c0 <evhttp_uri_parse@plt>
test   %rax,%rax
je     12ba <send_document_cb+0xda>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <evhttp_uri_get_path@plt>
test   %rax,%rax
lea    0xdd6(%rip),%rdi
cmovne %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <evhttp_uridecode@plt>
test   %rax,%rax
je     12e2 <send_document_cb+0x102>
mov    %rax,%r15
xor    %eax,%eax
call   10d0 <evbuffer_new@plt>
mov    %rax,%r12
lea    0xdae(%rip),%rdx
mov    %r13,%rdi
mov    $0xc8,%esi
mov    %rax,%rcx
call   1100 <evhttp_send_reply@plt>
mov    %r14,%rdi
call   10e0 <evhttp_uri_free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
test   %r12,%r12
je     1307 <send_document_cb+0x127>
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1080 <evbuffer_free@plt>
mov    %r13,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <dump_request_cb@plt>
lea    0xd77(%rip),%rdi
call   1060 <puts@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
xor    %edx,%edx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    10f0 <evhttp_send_error@plt>
lea    0xd38(%rip),%rdx
mov    %r13,%rdi
mov    $0x194,%esi
call   10f0 <evhttp_send_error@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    10e0 <evhttp_uri_free@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.evhttp_request = type { i32 }
%struct.evbuffer = type { i32 }
%struct.evhttp_uri = type { i32 }
@EVHTTP_REQ_GET = global i64 0
@.str = [28 x i8] c"Got a GET request for <%s>\0A\00"
@.str.1 = [41 x i8] c"It's not a good URI. Sending BADREQUEST\0A\00"
@HTTP_BADREQUEST = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [3 x i8] c"OK\00"
@.str.4 = [23 x i8] c"Document was not found\00"
define void @send_document_cb(%struct.evhttp_request* %0, i8* %1) {
%3 = alloca %struct.evhttp_request*
%4 = alloca i8*
%5 = alloca %struct.evbuffer*
%6 = alloca i8*
%7 = alloca %struct.evhttp_uri*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store %struct.evhttp_request* %0, %struct.evhttp_request** %3
store i8* %1, i8** %4
store %struct.evbuffer* null, %struct.evbuffer** %5
%12 = load %struct.evhttp_request*, %struct.evhttp_request** %3
%13 = call i8* @evhttp_request_get_uri(%struct.evhttp_request* %12)
store i8* %13, i8** %6
store %struct.evhttp_uri* null, %struct.evhttp_uri** %7
store i8* null, i8** %10
store i32 -1, i32* %11
%14 = load %struct.evhttp_request*, %struct.evhttp_request** %3
%15 = call i64 @evhttp_request_get_command(%struct.evhttp_request* %14)
%16 = load i64, i64* @EVHTTP_REQ_GET
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load %struct.evhttp_request*, %struct.evhttp_request** %3
%20 = load i8*, i8** %4
%21 = call i32 @dump_request_cb(%struct.evhttp_request* %19, i8* %20)
br label %84
22: ; preds = %2
%23 = load i8*, i8** %6
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %6
%26 = call %struct.evhttp_uri* @evhttp_uri_parse(i8* %25)
store %struct.evhttp_uri* %26, %struct.evhttp_uri** %7
%27 = load %struct.evhttp_uri*, %struct.evhttp_uri** %7
%28 = icmp ne %struct.evhttp_uri* %27, null
br i1 %28, label %34, label %29
29: ; preds = %22
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%31 = load %struct.evhttp_request*, %struct.evhttp_request** %3
%32 = load i32, i32* @HTTP_BADREQUEST
%33 = call i32 @evhttp_send_error(%struct.evhttp_request* %31, i32 %32, i8* null)
br label %84
34: ; preds = %22
%35 = load %struct.evhttp_uri*, %struct.evhttp_uri** %7
%36 = call i8* @evhttp_uri_get_path(%struct.evhttp_uri* %35)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %40, label %39
39: ; preds = %34
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %8
br label %40
40: ; preds = %39, %34
%41 = load i8*, i8** %8
%42 = call i8* @evhttp_uridecode(i8* %41, i32 0, i32* null)
store i8* %42, i8** %9
%43 = load i8*, i8** %9
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %46
45: ; preds = %40
br label %51
46: ; preds = %40
%47 = call %struct.evbuffer* (...) @evbuffer_new()
store %struct.evbuffer* %47, %struct.evbuffer** %5
%48 = load %struct.evhttp_request*, %struct.evhttp_request** %3
%49 = load %struct.evbuffer*, %struct.evbuffer** %5
%50 = call i32 @evhttp_send_reply(%struct.evhttp_request* %48, i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), %struct.evbuffer* %49)
br label %60
51: ; preds = %45
%52 = load %struct.evhttp_request*, %struct.evhttp_request** %3
%53 = call i32 @evhttp_send_error(%struct.evhttp_request* %52, i32 404, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32, i32* %11
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = load i32, i32* %11
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56, %51
br label %60
60: ; preds = %59, %46
%61 = load %struct.evhttp_uri*, %struct.evhttp_uri** %7
%62 = icmp ne %struct.evhttp_uri* %61, null
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load %struct.evhttp_uri*, %struct.evhttp_uri** %7
%65 = call i32 @evhttp_uri_free(%struct.evhttp_uri* %64)
br label %66
66: ; preds = %63, %60
%67 = load i8*, i8** %9
%68 = icmp ne i8* %67, null
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i8*, i8** %9
%71 = call i32 @free(i8* %70)
br label %72
72: ; preds = %69, %66
%73 = load i8*, i8** %10
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i8*, i8** %10
%77 = call i32 @free(i8* %76)
br label %78
78: ; preds = %75, %72
%79 = load %struct.evbuffer*, %struct.evbuffer** %5
%80 = icmp ne %struct.evbuffer* %79, null
br i1 %80, label %81, label %84
81: ; preds = %78
%82 = load %struct.evbuffer*, %struct.evbuffer** %5
%83 = call i32 @evbuffer_free(%struct.evbuffer* %82)
br label %84
84: ; preds = %18, %29, %81, %78
ret void
}

LLVM IR (-O2):
%struct.evhttp_request = type { i32 }
%struct.evbuffer = type { i32 }
%struct.evhttp_uri = type { i32 }
@EVHTTP_REQ_GET = global i64 0
@.str = [28 x i8] c"Got a GET request for <%s>\0A\00"
@HTTP_BADREQUEST = global i32 0
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [3 x i8] c"OK\00"
@.str.4 = [23 x i8] c"Document was not found\00"
@str = [40 x i8] c"It's not a good URI. Sending BADREQUEST\00"
define void @send_document_cb(%struct.evhttp_request* %0, i8* %1) {
%3 = tail call i8* @evhttp_request_get_uri(%struct.evhttp_request* %0)
%4 = tail call i64 @evhttp_request_get_command(%struct.evhttp_request* %0)
%5 = load i64, i64* @EVHTTP_REQ_GET
%6 = icmp eq i64 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @dump_request_cb(%struct.evhttp_request* %0, i8* %1)
br label %34
9: ; preds = %2
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %3)
%11 = tail call %struct.evhttp_uri* @evhttp_uri_parse(i8* %3)
%12 = icmp eq %struct.evhttp_uri* %11, null
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%15 = load i32, i32* @HTTP_BADREQUEST
%16 = tail call i32 @evhttp_send_error(%struct.evhttp_request* %0, i32 %15, i8* null)
br label %34
17: ; preds = %9
%18 = tail call i8* @evhttp_uri_get_path(%struct.evhttp_uri* %11)
%19 = icmp eq i8* %18, null
%20 = select i1 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %18
%21 = tail call i8* @evhttp_uridecode(i8* %20, i32 0, i32* null)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = tail call i32 @evhttp_send_error(%struct.evhttp_request* %0, i32 404, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @evhttp_uri_free(%struct.evhttp_uri* %11)
br label %34
26: ; preds = %17
%27 = tail call %struct.evbuffer* (...) @evbuffer_new()
%28 = tail call i32 @evhttp_send_reply(%struct.evhttp_request* %0, i32 200, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), %struct.evbuffer* %27)
%29 = tail call i32 @evhttp_uri_free(%struct.evhttp_uri* %11)
%30 = tail call i32 @free(i8* %21)
%31 = icmp eq %struct.evbuffer* %27, null
br i1 %31, label %34, label %32
32: ; preds = %26
%33 = tail call i32 @evbuffer_free(%struct.evbuffer* %27)
br label %34
34: ; preds = %23, %26, %32, %13, %7
ret void
}