#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 st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;
typedef int off_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
int /*<<< orphan*/ S_ISLNK(int /*<<< orphan*/);
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dprintf(int, char *, ...);
int /*<<< orphan*/ errno;
int fileno(int /*<<< orphan*/ *);
int fstat(int, struct stat *);
char *memchr(char *, char, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int read(int, char *, int);
int sendfile(int, int, int *, int);
int splice(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/ *,
           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncmp(char *, char *, int);
void do_gopher(int fd) {
  char buf[PATH_MAX], *end;
  struct stat sbuf;
  ssize_t len;
  off_t offset = 0;
  FILE *fp;
  int ifd;
  if ((len = read(fd, buf, PATH_MAX)) == -1) {
    dprintf(fd, "3Error reading input: %s\tfake\terror.host\t1\r\n.\r\n",
            strerror(errno));
    return;
  }
  if ((buf[0] == '\n') || (buf[0] == '\r') || (buf[0] == '\t')) {
    if ((ifd = open(".selectors", O_RDONLY)) == -1) {
      dprintf(fd, "3No .selectors file. Bug administrator to "
                  "fix.\tfake\terror.host\t1\r\n.\r\n");
      return;
    }
  } else if (!strncmp(buf, "fortune", 7)) {
    if ((fp = popen("/usr/games/fortune", "r")) == NULL) {
      dprintf(fd, "3popen() failed: %s\tfake\terror.host\t1\r\n.\r\n",
              strerror(errno));
      return;
    }
    ifd = fileno(fp);
    while ((len = splice(ifd, NULL, fd, NULL, BUFSIZ, 0)) > 0)
      ;
    if (len == -1) {
      dprintf(fd, "3splice() failed: %s\tfake\terror.host\t1\r\n.\r\n",
              strerror(errno));
    }
    pclose(fp);
    return;
  } else {
    if ((end = memchr(buf, '\r', PATH_MAX)) ||
        (end = memchr(buf, '\n', PATH_MAX))) {
      *end = '\0';
    } else {
      dprintf(fd, "3Malfomed request\tfake\terror.host\t1\r\n.\r\n");
      return;
    }
    if ((ifd = open(buf, O_RDONLY)) == -1) {
      dprintf(fd,
              "3'%s' does not exist (no handler "
              "found)\tfake\terror.host\t1\r\n.\r\n",
              buf);
      return;
    }
  }
  if (fstat(ifd, &sbuf) == -1) {
    dprintf(fd, "3fstat failed\tfake\terror.host\t1\r\n.\r\n");
    close(ifd);
    return;
  }
  if (!S_ISREG(sbuf.st_mode) && !S_ISFIFO(sbuf.st_mode) &&
      !S_ISLNK(sbuf.st_mode)) {
    dprintf(fd, "3invalid file type\tfake\terror.host\t1\r\n.\r\n");
    close(ifd);
    return;
  }
  if ((len = sendfile(fd, ifd, &offset, sbuf.st_size)) < sbuf.st_size) {
    if (len == -1) {
      dprintf(fd, "3sendfile() failed\tfake\terror.host\t1\r\n.\r\n");
      close(ifd);
      return;
    } else {
      while ((len = sendfile(fd, ifd, &offset, sbuf.st_size - offset)) <
             (sbuf.st_size - offset)) {
        if (len == -1)
          goto abend;
      }
    }
  }
abend:
  close(ifd);
  return;
}


x86 assembly (clang, -O0):
<do_gopher>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%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,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
call   10b0 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    128e <do_gopher+0x8e>
mov    -0x4(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %eax,%edx
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     12be <do_gopher+0xbe>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xd,%eax
je     12be <do_gopher+0xbe>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
jne    1301 <do_gopher+0x101>
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
lea    0xd62(%rip),%rdi
call   10f0 <open@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    12fc <do_gopher+0xfc>
mov    -0x4(%rbp),%edi
lea    0xd52(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
jmp    14b1 <do_gopher+0x2b1>
mov    -0x48(%rbp),%rdi
lea    0xd80(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    13f5 <do_gopher+0x1f5>
lea    0xd5b(%rip),%rdi
lea    0xd67(%rip),%rsi
call   1100 <popen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1373 <do_gopher+0x173>
mov    -0x4(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %eax,%edx
lea    0xd36(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
mov    -0x38(%rbp),%rdi
call   10c0 <fileno@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x4(%rbp),%edx
mov    0x2c44(%rip),%rax
mov    (%rax),%r8d
xor    %eax,%eax
mov    %eax,%ecx
xor    %r9d,%r9d
mov    %rcx,%rsi
call   1040 <splice@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    13af <do_gopher+0x1af>
jmp    137f <do_gopher+0x17f>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    13e0 <do_gopher+0x1e0>
mov    -0x4(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    0x2c02(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    %eax,%edx
lea    0xce8(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
mov    -0x38(%rbp),%rdi
call   1070 <pclose@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
mov    -0x48(%rbp),%rdi
mov    0x2bf0(%rip),%rax
mov    (%rax),%edx
mov    $0xd,%esi
call   10a0 <memchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    143f <do_gopher+0x23f>
mov    -0x48(%rbp),%rdi
mov    0x2bcb(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   10a0 <memchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     144b <do_gopher+0x24b>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1468 <do_gopher+0x268>
mov    -0x4(%rbp),%edi
lea    0xc98(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
mov    -0x48(%rbp),%rdi
mov    0x2b6d(%rip),%rax
mov    (%rax),%esi
call   10f0 <open@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    14a7 <do_gopher+0x2a7>
mov    -0x48(%rbp),%rdx
mov    -0x4(%rbp),%edi
lea    0xc82(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
jmp    14ac <do_gopher+0x2ac>
jmp    14b1 <do_gopher+0x2b1>
mov    -0x3c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1120 <fstat@plt>
cmp    $0xffffffff,%eax
jne    14eb <do_gopher+0x2eb>
mov    -0x4(%rbp),%edi
lea    0xc85(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
mov    -0x24(%rbp),%edi
call   10d0 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1543 <do_gopher+0x343>
mov    -0x24(%rbp),%edi
call   1060 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    1543 <do_gopher+0x343>
mov    -0x24(%rbp),%edi
call   1050 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    1543 <do_gopher+0x343>
mov    -0x4(%rbp),%edi
lea    0xc52(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
mov    -0x4(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0x28(%rbp),%ecx
lea    -0x30(%rbp),%rdx
call   10e0 <sendfile@plt>
mov    %eax,-0x2c(%rbp)
cmp    -0x28(%rbp),%eax
jge    15d9 <do_gopher+0x3d9>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1590 <do_gopher+0x390>
mov    -0x4(%rbp),%edi
lea    0xc2f(%rip),%rsi
mov    $0x0,%al
call   1090 <dprintf@plt>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x40(%rbp)
jmp    15ed <do_gopher+0x3ed>
jmp    1595 <do_gopher+0x395>
mov    -0x4(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0x28(%rbp),%ecx
sub    -0x30(%rbp),%ecx
lea    -0x30(%rbp),%rdx
call   10e0 <sendfile@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%ecx
sub    -0x30(%rbp),%ecx
cmp    %ecx,%eax
jge    15cf <do_gopher+0x3cf>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    15ca <do_gopher+0x3ca>
jmp    15de <do_gopher+0x3de>
jmp    1595 <do_gopher+0x395>
jmp    15d4 <do_gopher+0x3d4>
jmp    15d9 <do_gopher+0x3d9>
jmp    15de <do_gopher+0x3de>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_gopher>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    0x2dd7(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x0,-0x24(%rbp)
mov    %rbx,%rsi
call   10b0 <read@plt>
cmp    $0xffffffff,%eax
je     131b <do_gopher+0x11b>
movzbl (%rbx),%eax
cmp    $0xd,%rax
ja     1365 <do_gopher+0x165>
mov    $0x2600,%ecx
bt     %rax,%rcx
jae    1365 <do_gopher+0x165>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
lea    0xdbc(%rip),%rdi
call   10f0 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     1348 <do_gopher+0x148>
lea    -0x30(%rbp),%rsi
mov    %r15d,%edi
call   1120 <fstat@plt>
cmp    $0xffffffff,%eax
je     13e4 <do_gopher+0x1e4>
mov    -0x2c(%rbp),%ebx
mov    %ebx,%edi
call   10d0 <S_ISREG@plt>
test   %eax,%eax
jne    12c2 <do_gopher+0xc2>
mov    %ebx,%edi
call   1060 <S_ISFIFO@plt>
test   %eax,%eax
jne    12c2 <do_gopher+0xc2>
mov    %ebx,%edi
call   1050 <S_ISLNK@plt>
test   %eax,%eax
je     14a8 <do_gopher+0x2a8>
mov    -0x30(%rbp),%ebx
lea    -0x24(%rbp),%rdx
mov    %r14d,%edi
mov    %r15d,%esi
mov    %ebx,%ecx
call   10e0 <sendfile@plt>
cmp    %ebx,%eax
jge    14b9 <do_gopher+0x2b9>
cmp    $0xffffffff,%eax
je     1469 <do_gopher+0x269>
mov    -0x24(%rbp),%edx
lea    -0x24(%rbp),%r12
xchg   %ax,%ax
mov    %ebx,%ecx
sub    %edx,%ecx
mov    %r14d,%edi
mov    %r15d,%esi
mov    %r12,%rdx
call   10e0 <sendfile@plt>
cmp    $0xffffffff,%eax
je     14b9 <do_gopher+0x2b9>
mov    -0x24(%rbp),%edx
mov    %ebx,%ecx
sub    %edx,%ecx
cmp    %ecx,%eax
jl     12f0 <do_gopher+0xf0>
jmp    14b9 <do_gopher+0x2b9>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xcd0(%rip),%rsi
mov    %r14d,%edi
mov    %eax,%edx
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <dprintf@plt>
lea    0xcec(%rip),%rsi
mov    %r14d,%edi
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <dprintf@plt>
lea    0xd20(%rip),%rsi
mov    %rbx,%rdi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     13f0 <do_gopher+0x1f0>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0xd,%esi
call   10a0 <memchr@plt>
test   %rax,%rax
jne    13ab <do_gopher+0x1ab>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0xa,%esi
call   10a0 <memchr@plt>
test   %rax,%rax
je     1495 <do_gopher+0x295>
movb   $0x0,(%rax)
mov    0x2c2b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10f0 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1285 <do_gopher+0x85>
lea    0xd44(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <dprintf@plt>
jmp    14c1 <do_gopher+0x2c1>
lea    0xd6a(%rip),%rsi
jmp    14af <do_gopher+0x2af>
lea    0xc8a(%rip),%rdi
lea    0xc96(%rip),%rsi
call   1100 <popen@plt>
test   %rax,%rax
je     1472 <do_gopher+0x272>
mov    %rax,%r15
mov    %rax,%rdi
call   10c0 <fileno@plt>
mov    %eax,%ebx
mov    0x2bb4(%rip),%r12
nopl   0x0(%rax)
mov    (%r12),%r8d
mov    %ebx,%edi
xor    %esi,%esi
mov    %r14d,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1040 <splice@plt>
test   %eax,%eax
jg     1420 <do_gopher+0x220>
cmp    $0xffffffff,%eax
jne    145f <do_gopher+0x25f>
mov    0x2b83(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xc6e(%rip),%rsi
mov    %r14d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <dprintf@plt>
mov    %r15,%rdi
call   1070 <pclose@plt>
jmp    14c1 <do_gopher+0x2c1>
lea    0xd34(%rip),%rsi
jmp    14af <do_gopher+0x2af>
mov    0x2b4f(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xc0f(%rip),%rsi
mov    %r14d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <dprintf@plt>
jmp    14c1 <do_gopher+0x2c1>
lea    0xc51(%rip),%rsi
mov    %r14d,%edi
xor    %eax,%eax
call   1090 <dprintf@plt>
jmp    14c1 <do_gopher+0x2c1>
lea    0xccb(%rip),%rsi
mov    %r14d,%edi
xor    %eax,%eax
call   1090 <dprintf@plt>
mov    %r15d,%edi
call   1080 <close@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [48 x i8] c"3Error reading input: %s\09fake\09error.host\091\0D\0A.\0D\0A\00"
@errno = global i32 0
@.str.1 = [11 x i8] c".selectors\00"
@O_RDONLY = global i32 0
@.str.2 = [70 x i8] c"3No .selectors file. Bug administrator to fix.\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.3 = [8 x i8] c"fortune\00"
@.str.4 = [19 x i8] c"/usr/games/fortune\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [43 x i8] c"3popen() failed: %s\09fake\09error.host\091\0D\0A.\0D\0A\00"
@BUFSIZ = global i32 0
@.str.7 = [44 x i8] c"3splice() failed: %s\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.8 = [41 x i8] c"3Malfomed request\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.9 = [63 x i8] c"3'%s' does not exist (no handler found)\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.10 = [37 x i8] c"3fstat failed\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.11 = [42 x i8] c"3invalid file type\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.12 = [42 x i8] c"3sendfile() failed\09fake\09error.host\091\0D\0A.\0D\0A\00"
define void @do_gopher(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %2
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %3
%15 = alloca i8, i64 %13
store i64 %13, i64* %4
store i32 0, i32* %8
%16 = load i32, i32* %2
%17 = load i32, i32* @PATH_MAX
%18 = call i32 @read(i32 %16, i8* %15, i32 %17)
store i32 %18, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %1
%21 = load i32, i32* %2
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 (i32, i8*, ...) @dprintf(i32 %21, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 1, i32* %11
br label %173
25: ; preds = %1
%26 = getelementptr inbounds i8, i8* %15, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %40, label %30
30: ; preds = %25
%31 = getelementptr inbounds i8, i8* %15, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 13
br i1 %34, label %40, label %35
35: ; preds = %30
%36 = getelementptr inbounds i8, i8* %15, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 9
br i1 %39, label %40, label %48
40: ; preds = %35, %30, %25
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %41)
store i32 %42, i32* %10
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* %2
%46 = call i32 (i32, i8*, ...) @dprintf(i32 %45, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %11
br label %173
47: ; preds = %40
br label %102
48: ; preds = %35
%49 = call i32 @strncmp(i8* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 7)
%50 = icmp ne i32 %49, 0
br i1 %50, label %80, label %51
51: ; preds = %48
%52 = call i32* @popen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %52, i32** %9
%53 = icmp eq i32* %52, null
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i32, i32* %2
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = call i32 (i32, i8*, ...) @dprintf(i32 %55, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i32 %57)
store i32 1, i32* %11
br label %173
59: ; preds = %51
%60 = load i32*, i32** %9
%61 = call i32 @fileno(i32* %60)
store i32 %61, i32* %10
br label %62
62: ; preds = %68, %59
%63 = load i32, i32* %10
%64 = load i32, i32* %2
%65 = load i32, i32* @BUFSIZ
%66 = call i32 @splice(i32 %63, i32* null, i32 %64, i32* null, i32 %65, i32 0)
store i32 %66, i32* %7
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %62
br label %62
69: ; preds = %62
%70 = load i32, i32* %7
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %77
72: ; preds = %69
%73 = load i32, i32* %2
%74 = load i32, i32* @errno
%75 = call i32 @strerror(i32 %74)
%76 = call i32 (i32, i8*, ...) @dprintf(i32 %73, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %75)
br label %77
77: ; preds = %72, %69
%78 = load i32*, i32** %9
%79 = call i32 @pclose(i32* %78)
store i32 1, i32* %11
br label %173
80: ; preds = %48
%81 = load i32, i32* @PATH_MAX
%82 = call i8* @memchr(i8* %15, i8 signext 13, i32 %81)
store i8* %82, i8** %5
%83 = icmp ne i8* %82, null
br i1 %83, label %88, label %84
84: ; preds = %80
%85 = load i32, i32* @PATH_MAX
%86 = call i8* @memchr(i8* %15, i8 signext 10, i32 %85)
store i8* %86, i8** %5
%87 = icmp ne i8* %86, null
br i1 %87, label %88, label %90
88: ; preds = %84, %80
%89 = load i8*, i8** %5
store i8 0, i8* %89
br label %93
90: ; preds = %84
%91 = load i32, i32* %2
%92 = call i32 (i32, i8*, ...) @dprintf(i32 %91, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %11
br label %173
93: ; preds = %88
%94 = load i32, i32* @O_RDONLY
%95 = call i32 @open(i8* %15, i32 %94)
store i32 %95, i32* %10
%96 = icmp eq i32 %95, -1
br i1 %96, label %97, label %100
97: ; preds = %93
%98 = load i32, i32* %2
%99 = call i32 (i32, i8*, ...) @dprintf(i32 %98, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.9, i64 0, i64 0), i8* %15)
store i32 1, i32* %11
br label %173
100: ; preds = %93
br label %101
101: ; preds = %100
br label %102
102: ; preds = %101, %47
%103 = load i32, i32* %10
%104 = call i32 @fstat(i32 %103, %struct.stat* %6)
%105 = icmp eq i32 %104, -1
br i1 %105, label %106, label %111
106: ; preds = %102
%107 = load i32, i32* %2
%108 = call i32 (i32, i8*, ...) @dprintf(i32 %107, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0))
%109 = load i32, i32* %10
%110 = call i32 @close(i32 %109)
store i32 1, i32* %11
br label %173
111: ; preds = %102
%112 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%113 = load i32, i32* %112
%114 = call i32 @S_ISREG(i32 %113)
%115 = icmp ne i32 %114, 0
br i1 %115, label %131, label %116
116: ; preds = %111
%117 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%118 = load i32, i32* %117
%119 = call i32 @S_ISFIFO(i32 %118)
%120 = icmp ne i32 %119, 0
br i1 %120, label %131, label %121
121: ; preds = %116
%122 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%123 = load i32, i32* %122
%124 = call i32 @S_ISLNK(i32 %123)
%125 = icmp ne i32 %124, 0
br i1 %125, label %131, label %126
126: ; preds = %121
%127 = load i32, i32* %2
%128 = call i32 (i32, i8*, ...) @dprintf(i32 %127, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.11, i64 0, i64 0))
%129 = load i32, i32* %10
%130 = call i32 @close(i32 %129)
store i32 1, i32* %11
br label %173
131: ; preds = %121, %116, %111
%132 = load i32, i32* %2
%133 = load i32, i32* %10
%134 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%135 = load i32, i32* %134
%136 = call i32 @sendfile(i32 %132, i32 %133, i32* %8, i32 %135)
store i32 %136, i32* %7
%137 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%138 = load i32, i32* %137
%139 = icmp slt i32 %136, %138
br i1 %139, label %140, label %169
140: ; preds = %131
%141 = load i32, i32* %7
%142 = icmp eq i32 %141, -1
br i1 %142, label %143, label %148
143: ; preds = %140
%144 = load i32, i32* %2
%145 = call i32 (i32, i8*, ...) @dprintf(i32 %144, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.12, i64 0, i64 0))
%146 = load i32, i32* %10
%147 = call i32 @close(i32 %146)
store i32 1, i32* %11
br label %173
148: ; preds = %140
br label %149
149: ; preds = %166, %148
%150 = load i32, i32* %2
%151 = load i32, i32* %10
%152 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%153 = load i32, i32* %152
%154 = load i32, i32* %8
%155 = sub nsw i32 %153, %154
%156 = call i32 @sendfile(i32 %150, i32 %151, i32* %8, i32 %155)
store i32 %156, i32* %7
%157 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%158 = load i32, i32* %157
%159 = load i32, i32* %8
%160 = sub nsw i32 %158, %159
%161 = icmp slt i32 %156, %160
br i1 %161, label %162, label %167
162: ; preds = %149
%163 = load i32, i32* %7
%164 = icmp eq i32 %163, -1
br i1 %164, label %165, label %166
165: ; preds = %162
br label %170
166: ; preds = %162
br label %149
167: ; preds = %149
br label %168
168: ; preds = %167
br label %169
169: ; preds = %168, %131
br label %170
170: ; preds = %169, %165
%171 = load i32, i32* %10
%172 = call i32 @close(i32 %171)
store i32 1, i32* %11
br label %173
173: ; preds = %170, %143, %126, %106, %97, %90, %77, %54, %44, %20
%174 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [48 x i8] c"3Error reading input: %s\09fake\09error.host\091\0D\0A.\0D\0A\00"
@errno = global i32 0
@.str.1 = [11 x i8] c".selectors\00"
@O_RDONLY = global i32 0
@.str.2 = [70 x i8] c"3No .selectors file. Bug administrator to fix.\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.3 = [8 x i8] c"fortune\00"
@.str.4 = [19 x i8] c"/usr/games/fortune\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [43 x i8] c"3popen() failed: %s\09fake\09error.host\091\0D\0A.\0D\0A\00"
@BUFSIZ = global i32 0
@.str.7 = [44 x i8] c"3splice() failed: %s\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.8 = [41 x i8] c"3Malfomed request\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.9 = [63 x i8] c"3'%s' does not exist (no handler found)\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.10 = [37 x i8] c"3fstat failed\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.11 = [42 x i8] c"3invalid file type\09fake\09error.host\091\0D\0A.\0D\0A\00"
@.str.12 = [42 x i8] c"3sendfile() failed\09fake\09error.host\091\0D\0A.\0D\0A\00"
define void @do_gopher(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca i32
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %2 to i8*
%8 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%9 = call i32 @read(i32 %0, i8* %6, i32 %4)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %108
15: ; preds = %1
%16 = load i8, i8* %6
switch i8 %16, label %23 [
i8 10, label %17
i8 13, label %17
i8 9, label %17
]
17: ; preds = %15, %15, %15
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %64
21: ; preds = %17
%22 = tail call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0))
br label %108
23: ; preds = %15
%24 = call i32 @strncmp(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 7)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %47
26: ; preds = %23
%27 = call i32* @popen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i32 %31)
br label %108
33: ; preds = %26
%34 = call i32 @fileno(i32* %27)
br label %35
35: ; preds = %35, %33
%36 = load i32, i32* @BUFSIZ
%37 = call i32 @splice(i32 %34, i32* null, i32 %0, i32* null, i32 %36, i32 0)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %35, label %39
39: ; preds = %35
%40 = icmp eq i32 %37, -1
br i1 %40, label %41, label %45
41: ; preds = %39
%42 = load i32, i32* @errno
%43 = call i32 @strerror(i32 %42)
%44 = call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %41, %39
%46 = call i32 @pclose(i32* %27)
br label %108
47: ; preds = %23
%48 = load i32, i32* @PATH_MAX
%49 = call i8* @memchr(i8* %6, i8 signext 13, i32 %48)
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* @PATH_MAX
%53 = call i8* @memchr(i8* %6, i8 signext 10, i32 %52)
%54 = icmp eq i8* %53, null
br i1 %54, label %60, label %55
55: ; preds = %51, %47
%56 = phi i8* [ %49, %47 ], [ %53, %51 ]
store i8 0, i8* %56
%57 = load i32, i32* @O_RDONLY
%58 = call i32 @open(i8* %6, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %62, label %64
60: ; preds = %51
%61 = call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.8, i64 0, i64 0))
br label %108
62: ; preds = %55
%63 = call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.9, i64 0, i64 0), i8* %6)
br label %108
64: ; preds = %55, %17
%65 = phi i32 [ %19, %17 ], [ %58, %55 ]
%66 = call i32 @fstat(i32 %65, %struct.stat* %2)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %64
%69 = call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.10, i64 0, i64 0))
%70 = call i32 @close(i32 %65)
br label %108
71: ; preds = %64
%72 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%73 = load i32, i32* %72
%74 = call i32 @S_ISREG(i32 %73)
%75 = icmp eq i32 %74, 0
br i1 %75, label %76, label %85
76: ; preds = %71
%77 = call i32 @S_ISFIFO(i32 %73)
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %85
79: ; preds = %76
%80 = call i32 @S_ISLNK(i32 %73)
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.11, i64 0, i64 0))
%84 = call i32 @close(i32 %65)
br label %108
85: ; preds = %79, %76, %71
%86 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%87 = load i32, i32* %86
%88 = call i32 @sendfile(i32 %0, i32 %65, i32* %3, i32 %87)
%89 = icmp slt i32 %88, %87
br i1 %89, label %90, label %106
90: ; preds = %85
%91 = icmp eq i32 %88, -1
br i1 %91, label %94, label %92
92: ; preds = %90
%93 = load i32, i32* %3
br label %97
94: ; preds = %90
%95 = call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.12, i64 0, i64 0))
%96 = call i32 @close(i32 %65)
br label %108
97: ; preds = %92, %97
%98 = phi i32 [ %93, %92 ], [ %101, %97 ]
%99 = sub nsw i32 %87, %98
%100 = call i32 @sendfile(i32 %0, i32 %65, i32* %3, i32 %99)
%101 = load i32, i32* %3
%102 = sub nsw i32 %87, %101
%103 = icmp sge i32 %100, %102
%104 = icmp eq i32 %100, -1
%105 = or i1 %104, %103
br i1 %105, label %106, label %97
106: ; preds = %97, %85
%107 = call i32 @close(i32 %65)
br label %108
108: ; preds = %106, %94, %82, %68, %62, %60, %45, %29, %21, %11
ret void
}