#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 dummy;
};
struct bufferevent {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ BEV_FLUSH;
int BUFSIZ;
int /*<<< orphan*/ EV_WRITE;
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ bufferevent_flush(struct bufferevent *, int /*<<< orphan*/,
                                     int /*<<< orphan*/);
int /*<<< orphan*/ bufferevent_free(struct bufferevent *);
int /*<<< orphan*/ bufferevent_read(struct bufferevent *, char *, int);
int /*<<< orphan*/ bufferevent_write(struct bufferevent *, char *, size_t);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ hexdump(int /*<<< orphan*/, char *, int, int /*<<< orphan*/);
char *memchr(char *, char, int);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ stdout;
size_t strlen(char *);
void readcb(struct bufferevent *bev, void *ctx) {
  char line[PATH_MAX], *end, buf[BUFSIZ];
  int fd;
  struct stat st;
  size_t len;
  bufferevent_read(bev, line, PATH_MAX);
  hexdump(stdout, line, 16, 0);
  if ((line[0] == '\n') || (line[0] == '\r') || (line[0] == '\t')) {
    if ((fd = open(".selectors", O_RDONLY)) == -1) {
      bufferevent_write(
          bev,
          "3No .selectors file. Bug administrator to fix.\t\terror.host\t1\r\n",
          strlen("3No .selectors file. Bug administrator to "
                 "fix.\t\terror.host\t1\r\n"));
      goto end;
    }
  } else {
    if ((end = memchr(line, '\r', PATH_MAX)) ||
        (end = memchr(line, '\n', PATH_MAX))) {
      *end = '\0';
    } else {
      bufferevent_write(bev, "3Malformed request\t\terror.host\t1\r\n",
                        strlen("3Malformed request\t\terror.host\t1\r\n"));
      goto end;
    }
    if ((fd = open(line, O_RDONLY)) == -1) {
      bufferevent_write(bev, "3'", 2);
      bufferevent_write(bev, line, strlen(line));
      bufferevent_write(
          bev, "' does not exist (no handler found)\t\terror.host\t1\r\n",
          strlen("' does not exist (no handler found)\t\terror.host\t1\r\n"));
      goto end;
    }
  }
  while ((len = read(fd, buf, BUFSIZ)) > 0) {
    bufferevent_write(bev, buf, len);
  }
  close(fd);
end:
  bufferevent_flush(bev, EV_WRITE, BEV_FLUSH);
  bufferevent_free(bev);
}


x86 assembly (clang, -O0):
<readcb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
call   1030 <bufferevent_read@plt>
mov    -0x48(%rbp),%rsi
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    $0x10,%edx
xor    %ecx,%ecx
call   1090 <hexdump@plt>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     125c <readcb+0xbc>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
je     125c <readcb+0xbc>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
jne    12ae <readcb+0x10e>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0xd94(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    12a9 <readcb+0x109>
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    0xd7f(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rdx
lea    0xd6c(%rip),%rsi
call   10c0 <bufferevent_write@plt>
jmp    13fb <readcb+0x25b>
jmp    13b5 <readcb+0x215>
mov    -0x48(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
mov    $0xd,%esi
call   1070 <memchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12f8 <readcb+0x158>
mov    -0x48(%rbp),%rdi
mov    0x2d12(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   1070 <memchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1304 <readcb+0x164>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
jmp    1330 <readcb+0x190>
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    0xd37(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,%rdx
lea    0xd24(%rip),%rsi
call   10c0 <bufferevent_write@plt>
jmp    13fb <readcb+0x25b>
mov    -0x48(%rbp),%rdi
mov    0x2ca5(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    13b0 <readcb+0x210>
mov    -0x8(%rbp),%rdi
lea    0xd14(%rip),%rsi
mov    $0x2,%edx
call   10c0 <bufferevent_write@plt>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %rax,-0x70(%rbp)
call   1050 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
call   10c0 <bufferevent_write@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    0xcdd(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    %rax,%rdx
lea    0xcca(%rip),%rsi
call   10c0 <bufferevent_write@plt>
jmp    13fb <readcb+0x25b>
jmp    13b5 <readcb+0x215>
jmp    13ba <readcb+0x21a>
mov    -0x50(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    0x2c00(%rip),%rax
mov    (%rax),%edx
call   1080 <read@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jbe    13f3 <readcb+0x253>
mov    -0x50(%rbp),%rsi
mov    -0x8(%rbp),%rdi
mov    -0x40(%rbp),%rdx
call   10c0 <bufferevent_write@plt>
jmp    13ba <readcb+0x21a>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rdi
mov    0x2baa(%rip),%rax
mov    (%rax),%esi
mov    0x2bc9(%rip),%rax
mov    (%rax),%edx
call   1040 <bufferevent_flush@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <bufferevent_free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readcb>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%r13
add    $0xf,%r13
and    $0xfffffffffffffff0,%r13
mov    %rbx,%r15
sub    %r13,%r15
mov    %r15,%rsp
neg    %r13
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r15,%rsi
call   1030 <bufferevent_read@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0x10,%edx
xor    %ecx,%ecx
call   1090 <hexdump@plt>
movzbl (%rbx,%r13,1),%eax
cmp    $0xd,%rax
ja     1251 <readcb+0xb1>
mov    $0x2600,%ecx
bt     %rax,%rcx
jae    1251 <readcb+0xb1>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xdcc(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    129f <readcb+0xff>
lea    0xdc4(%rip),%rsi
mov    $0x3e,%edx
jmp    131f <readcb+0x17f>
mov    0x2d98(%rip),%rbx
mov    (%rbx),%edx
mov    %r15,%rdi
mov    $0xd,%esi
call   1070 <memchr@plt>
test   %rax,%rax
jne    1284 <readcb+0xe4>
mov    (%rbx),%edx
mov    %r15,%rdi
mov    $0xa,%esi
call   1070 <memchr@plt>
test   %rax,%rax
je     1313 <readcb+0x173>
movb   $0x0,(%rax)
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10b0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12db <readcb+0x13b>
mov    0x2d22(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r12,%rsi
call   1080 <read@plt>
test   %rax,%rax
je     12d2 <readcb+0x132>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   10c0 <bufferevent_write@plt>
jmp    12b0 <readcb+0x110>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1327 <readcb+0x187>
lea    0xd8b(%rip),%rsi
mov    $0x2,%edx
mov    %r14,%rdi
call   10c0 <bufferevent_write@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   10c0 <bufferevent_write@plt>
lea    0xd64(%rip),%rsi
mov    $0x33,%edx
jmp    131f <readcb+0x17f>
lea    0xd30(%rip),%rsi
mov    $0x22,%edx
mov    %r14,%rdi
call   10c0 <bufferevent_write@plt>
mov    0x2c82(%rip),%rax
mov    (%rax),%esi
mov    0x2ca1(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1040 <bufferevent_flush@plt>
mov    %r14,%rdi
call   10a0 <bufferevent_free@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bufferevent = type { i32 }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@BUFSIZ = global i32 0
@stdout = global i32 0
@.str = [11 x i8] c".selectors\00"
@O_RDONLY = global i32 0
@.str.1 = [63 x i8] c"3No .selectors file. Bug administrator to fix.\09\09error.host\091\0D\0A\00"
@.str.2 = [35 x i8] c"3Malformed request\09\09error.host\091\0D\0A\00"
@.str.3 = [3 x i8] c"3'\00"
@.str.4 = [52 x i8] c"' does not exist (no handler found)\09\09error.host\091\0D\0A\00"
@EV_WRITE = global i32 0
@BEV_FLUSH = global i32 0
define void @readcb(%struct.bufferevent* %0, i8* %1) {
%3 = alloca %struct.bufferevent*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i64
store %struct.bufferevent* %0, %struct.bufferevent** %3
store i8* %1, i8** %4
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %5
%15 = alloca i8, i64 %13
store i64 %13, i64* %6
%16 = load i32, i32* @BUFSIZ
%17 = zext i32 %16 to i64
%18 = alloca i8, i64 %17
store i64 %17, i64* %8
%19 = load %struct.bufferevent*, %struct.bufferevent** %3
%20 = load i32, i32* @PATH_MAX
%21 = call i32 @bufferevent_read(%struct.bufferevent* %19, i8* %15, i32 %20)
%22 = load i32, i32* @stdout
%23 = call i32 @hexdump(i32 %22, i8* %15, i32 16, i32 0)
%24 = getelementptr inbounds i8, i8* %15, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br i1 %27, label %38, label %28
28: ; preds = %2
%29 = getelementptr inbounds i8, i8* %15, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 13
br i1 %32, label %38, label %33
33: ; preds = %28
%34 = getelementptr inbounds i8, i8* %15, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 9
br i1 %37, label %38, label %47
38: ; preds = %33, %28, %2
%39 = load i32, i32* @O_RDONLY
%40 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %39)
store i32 %40, i32* %9
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load %struct.bufferevent*, %struct.bufferevent** %3
%44 = call i64 @strlen(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0))
%45 = call i32 @bufferevent_write(%struct.bufferevent* %43, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i64 %44)
br label %88
46: ; preds = %38
br label %75
47: ; preds = %33
%48 = load i32, i32* @PATH_MAX
%49 = call i8* @memchr(i8* %15, i8 signext 13, i32 %48)
store i8* %49, i8** %7
%50 = icmp ne i8* %49, null
br i1 %50, label %55, label %51
51: ; preds = %47
%52 = load i32, i32* @PATH_MAX
%53 = call i8* @memchr(i8* %15, i8 signext 10, i32 %52)
store i8* %53, i8** %7
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %57
55: ; preds = %51, %47
%56 = load i8*, i8** %7
store i8 0, i8* %56
br label %61
57: ; preds = %51
%58 = load %struct.bufferevent*, %struct.bufferevent** %3
%59 = call i64 @strlen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @bufferevent_write(%struct.bufferevent* %58, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i64 %59)
br label %88
61: ; preds = %55
%62 = load i32, i32* @O_RDONLY
%63 = call i32 @open(i8* %15, i32 %62)
store i32 %63, i32* %9
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %74
65: ; preds = %61
%66 = load %struct.bufferevent*, %struct.bufferevent** %3
%67 = call i32 @bufferevent_write(%struct.bufferevent* %66, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 2)
%68 = load %struct.bufferevent*, %struct.bufferevent** %3
%69 = call i64 @strlen(i8* %15)
%70 = call i32 @bufferevent_write(%struct.bufferevent* %68, i8* %15, i64 %69)
%71 = load %struct.bufferevent*, %struct.bufferevent** %3
%72 = call i64 @strlen(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
%73 = call i32 @bufferevent_write(%struct.bufferevent* %71, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i64 %72)
br label %88
74: ; preds = %61
br label %75
75: ; preds = %74, %46
br label %76
76: ; preds = %81, %75
%77 = load i32, i32* %9
%78 = load i32, i32* @BUFSIZ
%79 = call i64 @read(i32 %77, i8* %18, i32 %78)
store i64 %79, i64* %11
%80 = icmp ugt i64 %79, 0
br i1 %80, label %81, label %85
81: ; preds = %76
%82 = load %struct.bufferevent*, %struct.bufferevent** %3
%83 = load i64, i64* %11
%84 = call i32 @bufferevent_write(%struct.bufferevent* %82, i8* %18, i64 %83)
br label %76
85: ; preds = %76
%86 = load i32, i32* %9
%87 = call i32 @close(i32 %86)
br label %88
88: ; preds = %85, %65, %57, %42
%89 = load %struct.bufferevent*, %struct.bufferevent** %3
%90 = load i32, i32* @EV_WRITE
%91 = load i32, i32* @BEV_FLUSH
%92 = call i32 @bufferevent_flush(%struct.bufferevent* %89, i32 %90, i32 %91)
%93 = load %struct.bufferevent*, %struct.bufferevent** %3
%94 = call i32 @bufferevent_free(%struct.bufferevent* %93)
%95 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
%struct.bufferevent = type { i32 }
@PATH_MAX = global i32 0
@BUFSIZ = global i32 0
@stdout = global i32 0
@.str = [11 x i8] c".selectors\00"
@O_RDONLY = global i32 0
@.str.1 = [63 x i8] c"3No .selectors file. Bug administrator to fix.\09\09error.host\091\0D\0A\00"
@.str.2 = [35 x i8] c"3Malformed request\09\09error.host\091\0D\0A\00"
@.str.3 = [3 x i8] c"3'\00"
@.str.4 = [52 x i8] c"' does not exist (no handler found)\09\09error.host\091\0D\0A\00"
@EV_WRITE = global i32 0
@BEV_FLUSH = global i32 0
define void @readcb(%struct.bufferevent* %0, i8* nocapture readnone %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i32, i32* @BUFSIZ
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = call i32 @bufferevent_read(%struct.bufferevent* %0, i8* %5, i32 %3)
%10 = load i32, i32* @stdout
%11 = call i32 @hexdump(i32 %10, i8* %5, i32 16, i32 0)
%12 = load i8, i8* %5
switch i8 %12, label %19 [
i8 10, label %13
i8 13, label %13
i8 9, label %13
]
13: ; preds = %2, %2, %2
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %39
17: ; preds = %13
%18 = call i32 @bufferevent_write(%struct.bufferevent* %0, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i64 62)
br label %52
19: ; preds = %2
%20 = load i32, i32* @PATH_MAX
%21 = call i8* @memchr(i8* %5, i8 signext 13, i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @PATH_MAX
%25 = call i8* @memchr(i8* %5, i8 signext 10, i32 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %32, label %27
27: ; preds = %23, %19
%28 = phi i8* [ %21, %19 ], [ %25, %23 ]
store i8 0, i8* %28
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* %5, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %34, label %39
32: ; preds = %23
%33 = call i32 @bufferevent_write(%struct.bufferevent* %0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i64 34)
br label %52
34: ; preds = %27
%35 = call i32 @bufferevent_write(%struct.bufferevent* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 2)
%36 = call i64 @strlen(i8* %5)
%37 = call i32 @bufferevent_write(%struct.bufferevent* %0, i8* %5, i64 %36)
%38 = call i32 @bufferevent_write(%struct.bufferevent* %0, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i64 51)
br label %52
39: ; preds = %27, %13
%40 = phi i32 [ %15, %13 ], [ %30, %27 ]
%41 = load i32, i32* @BUFSIZ
%42 = call i64 @read(i32 %40, i8* %8, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %39, %44
%45 = phi i64 [ %48, %44 ], [ %42, %39 ]
%46 = call i32 @bufferevent_write(%struct.bufferevent* %0, i8* %8, i64 %45)
%47 = load i32, i32* @BUFSIZ
%48 = call i64 @read(i32 %40, i8* %8, i32 %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %44
50: ; preds = %44, %39
%51 = call i32 @close(i32 %40)
br label %52
52: ; preds = %50, %34, %32, %17
%53 = load i32, i32* @EV_WRITE
%54 = load i32, i32* @BEV_FLUSH
%55 = call i32 @bufferevent_flush(%struct.bufferevent* %0, i32 %53, i32 %54)
%56 = call i32 @bufferevent_free(%struct.bufferevent* %0)
ret void
}