#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};
typedef int ssize_t;

/* Variables and functions */
int EPOLLHUP;
int EPOLLIN;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(char *, ...);
int read(int, char *, size_t const);
int /*<<< orphan*/ write2(int, char *, int);
int handle(struct epoll_event *e, int cmdfd, int in_fd, int out_fd) {
  const size_t kBufSize = 1024;
  char buf[kBufSize];
  if (e->events & ~(EPOLLHUP | EPOLLIN)) {
    fatal("unexepected event %d on fd=%d", e->events, e->data.fd);
  }
  if (e->events & EPOLLIN) {
    ssize_t count = read(e->data.fd, buf, kBufSize);
    if (count == -1) {
      fatal("bad read()");
    }
    if (e->data.fd == STDIN_FILENO) {
      write2(cmdfd, buf, count);
    } else if (e->data.fd == cmdfd) {
      write2(STDOUT_FILENO, buf, count);
      write2(out_fd, buf, count);
    } else if (e->data.fd == in_fd) {
      write2(cmdfd, buf, count);
    } else {
      fatal("unexpected fd for EPOLLIN event");
    }
  }
  if (e->events & EPOLLHUP) {
    if (e->data.fd == cmdfd) {
      exit(0);
    } else if (e->data.fd == in_fd) {
      return 1;
    } else {
      fatal("unexpected");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x400,-0x28(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e63(%rip),%rdx
or     (%rdx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11a8 <handle+0x68>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e2b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12a3 <handle+0x163>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11f3 <handle+0xb3>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%eax
jne    1220 <handle+0xe0>
mov    -0x14(%rbp),%edi
mov    -0x34(%rbp),%edx
lea    -0x440(%rbp),%rsi
call   1040 <write2@plt>
jmp    129e <handle+0x15e>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x14(%rbp),%eax
jne    125f <handle+0x11f>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
lea    -0x440(%rbp),%rsi
call   1040 <write2@plt>
mov    -0x1c(%rbp),%edi
mov    -0x34(%rbp),%edx
lea    -0x440(%rbp),%rsi
call   1040 <write2@plt>
jmp    1299 <handle+0x159>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x18(%rbp),%eax
jne    1286 <handle+0x146>
mov    -0x14(%rbp),%edi
mov    -0x34(%rbp),%edx
lea    -0x440(%rbp),%rsi
call   1040 <write2@plt>
jmp    1294 <handle+0x154>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
jmp    1299 <handle+0x159>
jmp    129e <handle+0x15e>
jmp    12a3 <handle+0x163>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2d18(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     130d <handle+0x1cd>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x14(%rbp),%eax
jne    12d2 <handle+0x192>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x18(%rbp),%eax
jne    12f5 <handle+0x1b5>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    131b <handle+0x1db>
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
jmp    1308 <handle+0x1c8>
jmp    130d <handle+0x1cd>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%rbx
mov    (%rdi),%esi
mov    0x2e62(%rip),%r13
mov    0x2e73(%rip),%rbp
mov    0x0(%rbp),%eax
mov    0x0(%r13),%ecx
or     %eax,%ecx
not    %ecx
test   %ecx,%esi
je     1192 <handle+0x52>
mov    0x4(%rbx),%edx
lea    0xe7a(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    (%rbx),%esi
mov    0x0(%rbp),%eax
test   %esi,%eax
je     11f6 <handle+0xb6>
mov    0x4(%rbx),%edi
mov    %rsp,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11bb <handle+0x7b>
lea    0xe6a(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    0x4(%rbx),%eax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%eax
jne    11d1 <handle+0x91>
mov    %rsp,%rsi
mov    %r12d,%edi
jmp    11ef <handle+0xaf>
cmp    %r12d,%eax
jne    1238 <handle+0xf8>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    %ebp,%edx
call   1040 <write2@plt>
mov    %r15d,%edi
mov    %rsp,%rsi
mov    %ebp,%edx
call   1040 <write2@plt>
mov    0x0(%r13),%eax
test   %eax,(%rbx)
je     1222 <handle+0xe2>
mov    0x4(%rbx),%eax
cmp    %r12d,%eax
je     124d <handle+0x10d>
mov    $0x1,%ebx
cmp    %r14d,%eax
je     1224 <handle+0xe4>
lea    0xe32(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fatal@plt>
jmp    1224 <handle+0xe4>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    %r14d,%eax
je     11c9 <handle+0x89>
lea    0xde5(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
jmp    11f6 <handle+0xb6>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLHUP = global i32 0
@EPOLLIN = global i32 0
@.str = [30 x i8] c"unexepected event %d on fd=%d\00"
@.str.1 = [11 x i8] c"bad read()\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [32 x i8] c"unexpected fd for EPOLLIN event\00"
@.str.3 = [11 x i8] c"unexpected\00"
define i32 @handle(%struct.epoll_event* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.epoll_event*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store %struct.epoll_event* %0, %struct.epoll_event** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i64 1024, i64* %10
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %11
%15 = alloca i8, i64 1024
%16 = load %struct.epoll_event*, %struct.epoll_event** %6
%17 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @EPOLLHUP
%20 = load i32, i32* @EPOLLIN
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = and i32 %18, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %4
%26 = load %struct.epoll_event*, %struct.epoll_event** %6
%27 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load %struct.epoll_event*, %struct.epoll_event** %6
%30 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %29, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %32)
br label %34
34: ; preds = %25, %4
%35 = load %struct.epoll_event*, %struct.epoll_event** %6
%36 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @EPOLLIN
%39 = and i32 %37, %38
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %92
41: ; preds = %34
%42 = load %struct.epoll_event*, %struct.epoll_event** %6
%43 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %42, i32 0, i32 1
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @read(i32 %45, i8* %15, i64 1024)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %41
%52 = load %struct.epoll_event*, %struct.epoll_event** %6
%53 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %52, i32 0, i32 1
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i32, i32* @STDIN_FILENO
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %62
58: ; preds = %51
%59 = load i32, i32* %7
%60 = load i32, i32* %12
%61 = call i32 @write2(i32 %59, i8* %15, i32 %60)
br label %91
62: ; preds = %51
%63 = load %struct.epoll_event*, %struct.epoll_event** %6
%64 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %63, i32 0, i32 1
%65 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %64, i32 0, i32 0
%66 = load i32, i32* %65
%67 = load i32, i32* %7
%68 = icmp eq i32 %66, %67
br i1 %68, label %69, label %76
69: ; preds = %62
%70 = load i32, i32* @STDOUT_FILENO
%71 = load i32, i32* %12
%72 = call i32 @write2(i32 %70, i8* %15, i32 %71)
%73 = load i32, i32* %9
%74 = load i32, i32* %12
%75 = call i32 @write2(i32 %73, i8* %15, i32 %74)
br label %90
76: ; preds = %62
%77 = load %struct.epoll_event*, %struct.epoll_event** %6
%78 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %77, i32 0, i32 1
%79 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %78, i32 0, i32 0
%80 = load i32, i32* %79
%81 = load i32, i32* %8
%82 = icmp eq i32 %80, %81
br i1 %82, label %83, label %87
83: ; preds = %76
%84 = load i32, i32* %7
%85 = load i32, i32* %12
%86 = call i32 @write2(i32 %84, i8* %15, i32 %85)
br label %89
87: ; preds = %76
%88 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %89
89: ; preds = %87, %83
br label %90
90: ; preds = %89, %69
br label %91
91: ; preds = %90, %58
br label %92
92: ; preds = %91, %34
%93 = load %struct.epoll_event*, %struct.epoll_event** %6
%94 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %93, i32 0, i32 0
%95 = load i32, i32* %94
%96 = load i32, i32* @EPOLLHUP
%97 = and i32 %95, %96
%98 = icmp ne i32 %97, 0
br i1 %98, label %99, label %120
99: ; preds = %92
%100 = load %struct.epoll_event*, %struct.epoll_event** %6
%101 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %100, i32 0, i32 1
%102 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %101, i32 0, i32 0
%103 = load i32, i32* %102
%104 = load i32, i32* %7
%105 = icmp eq i32 %103, %104
br i1 %105, label %106, label %108
106: ; preds = %99
%107 = call i32 @exit(i32 0)
unreachable
108: ; preds = %99
%109 = load %struct.epoll_event*, %struct.epoll_event** %6
%110 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %109, i32 0, i32 1
%111 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %110, i32 0, i32 0
%112 = load i32, i32* %111
%113 = load i32, i32* %8
%114 = icmp eq i32 %112, %113
br i1 %114, label %115, label %116
115: ; preds = %108
store i32 1, i32* %5
store i32 1, i32* %13
br label %121
116: ; preds = %108
%117 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %118
118: ; preds = %116
br label %119
119: ; preds = %118
br label %120
120: ; preds = %119, %92
store i32 0, i32* %5
store i32 1, i32* %13
br label %121
121: ; preds = %120, %115
%122 = load i8*, i8** %11
%123 = load i32, i32* %5
ret i32 %123
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLHUP = global i32 0
@EPOLLIN = global i32 0
@.str = [30 x i8] c"unexepected event %d on fd=%d\00"
@.str.1 = [11 x i8] c"bad read()\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [32 x i8] c"unexpected fd for EPOLLIN event\00"
@.str.3 = [11 x i8] c"unexpected\00"
define i32 @handle(%struct.epoll_event* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @EPOLLHUP
%10 = load i32, i32* @EPOLLIN
%11 = or i32 %10, %9
%12 = xor i32 %11, -1
%13 = and i32 %8, %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %4
%16 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 0, i32 1, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %17)
%19 = load i32, i32* %7
%20 = load i32, i32* @EPOLLIN
br label %21
21: ; preds = %15, %4
%22 = phi i32 [ %20, %15 ], [ %10, %4 ]
%23 = phi i32 [ %19, %15 ], [ %8, %4 ]
%24 = and i32 %22, %23
%25 = icmp eq i32 %24, 0
br i1 %25, label %51, label %26
26: ; preds = %21
%27 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 0, i32 1, i32 0
%28 = load i32, i32* %27
%29 = call i32 @read(i32 %28, i8* %6, i64 1024)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i32, i32* %27
%35 = load i32, i32* @STDIN_FILENO
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @write2(i32 %1, i8* %6, i32 %29)
br label %51
39: ; preds = %33
%40 = icmp eq i32 %34, %1
br i1 %40, label %41, label %45
41: ; preds = %39
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @write2(i32 %42, i8* %6, i32 %29)
%44 = call i32 @write2(i32 %3, i8* %6, i32 %29)
br label %51
45: ; preds = %39
%46 = icmp eq i32 %34, %2
br i1 %46, label %47, label %49
47: ; preds = %45
%48 = call i32 @write2(i32 %1, i8* %6, i32 %29)
br label %51
49: ; preds = %45
%50 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %37, %47, %49, %41, %21
%52 = load i32, i32* %7
%53 = load i32, i32* @EPOLLHUP
%54 = and i32 %53, %52
%55 = icmp eq i32 %54, 0
br i1 %55, label %66, label %56
56: ; preds = %51
%57 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 0, i32 1, i32 0
%58 = load i32, i32* %57
%59 = icmp eq i32 %58, %1
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %56
%63 = icmp eq i32 %58, %2
br i1 %63, label %66, label %64
64: ; preds = %62
%65 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %66
66: ; preds = %51, %64, %62
%67 = phi i32 [ 1, %62 ], [ 0, %64 ], [ 0, %51 ]
ret i32 %67
}