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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int epoll_wait(int, struct epoll_event *, size_t const, int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ handle(struct epoll_event *, int, int, int);
int init_epoll(int);
int /*<<< orphan*/ init_fs(char *, char **, int *);
int /*<<< orphan*/ init_tty();
int open_fifo(char *, int);
void bridge(char *dir, int cmdfd) {
  int in_fd, out_fd;
  char *in_path;
  init_fs(dir, &in_path, &out_fd);
  init_tty();
  int epfd = init_epoll(cmdfd);
  in_fd = open_fifo(in_path, epfd);
  const size_t kMaxEvents = 64;
  struct epoll_event events[kMaxEvents];
  while (1) {
    int count = epoll_wait(epfd, events, kMaxEvents, -1);
    if (count == -1) {
      fatal("epoll_wait failed");
    }
    for (int i = 0; i < count; i++) {
      if (handle(&events[i], cmdfd, in_fd, out_fd)) {
        close(in_fd);
        in_fd = open_fifo(in_path, epfd);
      }
    }
  }
  free(in_path);
}


x86 assembly (clang, -O0):
<bridge>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1030 <init_fs@plt>
mov    $0x0,%al
call   1090 <init_tty@plt>
mov    -0xc(%rbp),%edi
call   1080 <init_epoll@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <open_fifo@plt>
mov    %eax,-0x10(%rbp)
movq   $0x40,-0x30(%rbp)
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
jmp    11d8 <bridge+0x58>
mov    -0x24(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x40,%edx
mov    $0xffffffff,%ecx
call   1070 <epoll_wait@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    120a <bridge+0x8a>
lea    0xdfb(%rip),%rdi
call   10a0 <fatal@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1271 <bridge+0xf1>
movslq -0x40(%rbp),%rax
lea    -0x140(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1050 <handle@plt>
cmp    $0x0,%rax
je     125e <bridge+0xde>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <open_fifo@plt>
mov    %eax,-0x10(%rbp)
jmp    1263 <bridge+0xe3>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1211 <bridge+0x91>
jmp    11d3 <bridge+0x53>

x86 assembly (clang, -O2):
<bridge>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %esi,%r14d
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1030 <init_fs@plt>
xor    %eax,%eax
call   1090 <init_tty@plt>
mov    %r14d,%edi
call   1080 <init_epoll@plt>
mov    %eax,%r13d
mov    0x8(%rsp),%rdi
mov    %eax,%esi
call   1060 <open_fifo@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
lea    0xe29(%rip),%rbp
jmp    11e8 <bridge+0x68>
nopl   0x0(%rax)
mov    %rbp,%rdi
call   10a0 <fatal@plt>
mov    $0x40,%edx
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0xffffffff,%ecx
call   1070 <epoll_wait@plt>
cmp    $0xffffffff,%eax
je     11e0 <bridge+0x60>
test   %eax,%eax
jle    11e8 <bridge+0x68>
mov    %eax,%r15d
mov    %r12,%rbp
jmp    121a <bridge+0x9a>
xchg   %ax,%ax
add    $0x4,%rbp
add    $0xffffffffffffffff,%r15
je     11d0 <bridge+0x50>
mov    0x4(%rsp),%ecx
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %ebx,%edx
call   1050 <handle@plt>
test   %rax,%rax
je     1210 <bridge+0x90>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%rdi
mov    %r13d,%esi
call   1060 <open_fifo@plt>
mov    %eax,%ebx
jmp    1210 <bridge+0x90>

LLVM IR (-O0):
%struct.epoll_event = type { i32 }
@.str = [18 x i8] c"epoll_wait failed\00"
define void @bridge(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%13 = load i8*, i8** %3
%14 = call i32 @init_fs(i8* %13, i8** %7, i32* %6)
%15 = call i32 (...) @init_tty()
%16 = load i32, i32* %4
%17 = call i32 @init_epoll(i32 %16)
store i32 %17, i32* %8
%18 = load i8*, i8** %7
%19 = load i32, i32* %8
%20 = call i32 @open_fifo(i8* %18, i32 %19)
store i32 %20, i32* %5
store i64 64, i64* %9
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %10
%22 = alloca %struct.epoll_event, i64 64
br label %23
23: ; preds = %55, %2
br label %24
24: ; preds = %23
%25 = load i32, i32* %8
%26 = call i32 @epoll_wait(i32 %25, %struct.epoll_event* %22, i64 64, i32 -1)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
store i32 0, i32* %12
br label %32
32: ; preds = %52, %31
%33 = load i32, i32* %12
%34 = load i32, i32* %11
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %55
36: ; preds = %32
%37 = load i32, i32* %12
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %22, i64 %38
%40 = load i32, i32* %4
%41 = load i32, i32* %5
%42 = load i32, i32* %6
%43 = call i64 @handle(%struct.epoll_event* %39, i32 %40, i32 %41, i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %36
%46 = load i32, i32* %5
%47 = call i32 @close(i32 %46)
%48 = load i8*, i8** %7
%49 = load i32, i32* %8
%50 = call i32 @open_fifo(i8* %48, i32 %49)
store i32 %50, i32* %5
br label %51
51: ; preds = %45, %36
br label %52
52: ; preds = %51
%53 = load i32, i32* %12
%54 = add nsw i32 %53, 1
store i32 %54, i32* %12
br label %32
55: ; preds = %32
br label %23
}

LLVM IR (-O2):
%struct.epoll_event = type { i32 }
@.str = [18 x i8] c"epoll_wait failed\00"
define void @bridge(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = bitcast i32* %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = call i32 @init_fs(i8* %0, i8** %4, i32* %3)
%8 = call i32 (...) @init_tty()
%9 = call i32 @init_epoll(i32 %1)
%10 = load i8*, i8** %4
%11 = call i32 @open_fifo(i8* %10, i32 %9)
%12 = alloca [64 x %struct.epoll_event]
%13 = getelementptr inbounds [64 x %struct.epoll_event], [64 x %struct.epoll_event]* %12, i64 0, i64 0
br label %14
14: ; preds = %37, %2
%15 = phi i32 [ %11, %2 ], [ %38, %37 ]
br label %16
16: ; preds = %21, %14
%17 = call i32 @epoll_wait(i32 %9, %struct.epoll_event* %13, i64 64, i32 -1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %22
br label %16
22: ; preds = %16
%23 = icmp sgt i32 %17, 0
br i1 %23, label %24, label %21
24: ; preds = %22
%25 = zext i32 %17 to i64
br label %26
26: ; preds = %24, %37
%27 = phi i64 [ 0, %24 ], [ %39, %37 ]
%28 = phi i32 [ %15, %24 ], [ %38, %37 ]
%29 = getelementptr inbounds [64 x %struct.epoll_event], [64 x %struct.epoll_event]* %12, i64 0, i64 %27
%30 = load i32, i32* %3
%31 = call i64 @handle(%struct.epoll_event* %29, i32 %1, i32 %28, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %26
%34 = call i32 @close(i32 %28)
%35 = load i8*, i8** %4
%36 = call i32 @open_fifo(i8* %35, i32 %9)
br label %37
37: ; preds = %26, %33
%38 = phi i32 [ %36, %33 ], [ %28, %26 ]
%39 = add nuw nsw i64 %27, 1
%40 = icmp eq i64 %39, %25
br i1 %40, label %14, label %26
}