#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 */

/* Variables and functions */
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_MAKEPRI(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ LOG_USER;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit_error(char *);
int fork();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int handle_input_fifo(char *name_fifo_in, int terminal) {
  int pid = fork();
  if (-1 == pid)
    exit_error("fork failed");
  if (0 != pid)
    return pid;
  while (1) {
    char buf;
    (void)(LOG_MAKEPRI(LOG_USER, LOG_DEBUG), "input: waiting");
    int fifo_in = open(name_fifo_in, O_RDONLY, 0);
    (void)(LOG_MAKEPRI(LOG_USER, LOG_DEBUG), "input: connected");
    if (fifo_in == -1)
      exit_error("input pipe not found");
    while (1 == read(fifo_in, &buf, 1))
      write(terminal, &buf, 1);
    close(fifo_in);
  }
  _exit(1);
}


x86 assembly (clang, -O0):
<handle_input_fifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    $0xffffffff,%eax
cmp    -0x10(%rbp),%eax
jne    11a3 <handle_input_fifo+0x33>
lea    0xe62(%rip),%rdi
call   1050 <exit_error@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%eax
je     11b7 <handle_input_fifo+0x47>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    11bc <handle_input_fifo+0x4c>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1070 <LOG_MAKEPRI@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1070 <LOG_MAKEPRI@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1217 <handle_input_fifo+0xa7>
lea    0xe1a(%rip),%rdi
call   1050 <exit_error@plt>
jmp    121c <handle_input_fifo+0xac>
mov    -0x18(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1252 <handle_input_fifo+0xe2>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    121c <handle_input_fifo+0xac>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    11bc <handle_input_fifo+0x4c>

x86 assembly (clang, -O2):
<handle_input_fifo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %rdi,%r14
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     121d <handle_input_fifo+0xad>
test   %ebx,%ebx
jne    1229 <handle_input_fifo+0xb9>
mov    0x2e46(%rip),%r13
mov    0x2e27(%rip),%r15
lea    0x7(%rsp),%rbx
xchg   %ax,%ax
mov    0x0(%r13),%edi
mov    (%r15),%esi
call   1070 <LOG_MAKEPRI@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x0(%r13),%edi
mov    (%r15),%esi
call   1070 <LOG_MAKEPRI@plt>
cmp    $0xffffffff,%ebp
jne    1200 <handle_input_fifo+0x90>
lea    0xe23(%rip),%rdi
call   1050 <exit_error@plt>
jmp    1200 <handle_input_fifo+0x90>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     11f0 <handle_input_fifo+0x80>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11b0 <handle_input_fifo+0x40>
lea    0xddc(%rip),%rdi
call   1050 <exit_error@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"fork failed\00"
@LOG_USER = global i32 0
@LOG_DEBUG = global i32 0
@.str.1 = [15 x i8] c"input: waiting\00"
@O_RDONLY = global i32 0
@.str.2 = [17 x i8] c"input: connected\00"
@.str.3 = [21 x i8] c"input pipe not found\00"
define i32 @handle_input_fifo(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @exit_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* %5
%15 = icmp ne i32 0, %14
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i32, i32* %5
ret i32 %17
18: ; preds = %13
br label %19
19: ; preds = %18, %41
%20 = load i32, i32* @LOG_USER
%21 = load i32, i32* @LOG_DEBUG
%22 = call i32 @LOG_MAKEPRI(i32 %20, i32 %21)
%23 = load i8*, i8** %3
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24, i32 0)
store i32 %25, i32* %7
%26 = load i32, i32* @LOG_USER
%27 = load i32, i32* @LOG_DEBUG
%28 = call i32 @LOG_MAKEPRI(i32 %26, i32 %27)
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %19
%32 = call i32 @exit_error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %19
br label %34
34: ; preds = %38, %33
%35 = load i32, i32* %7
%36 = call i32 @read(i32 %35, i8* %6, i32 1)
%37 = icmp eq i32 1, %36
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* %4
%40 = call i32 @write(i32 %39, i8* %6, i32 1)
br label %34
41: ; preds = %34
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
br label %19
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork failed\00"
@LOG_USER = global i32 0
@LOG_DEBUG = global i32 0
@O_RDONLY = global i32 0
@.str.3 = [21 x i8] c"input pipe not found\00"
define i32 @handle_input_fifo(i8* nocapture %0, i32 %1) {
%3 = alloca i8
%4 = tail call i32 (...) @fork()
switch i32 %4, label %7 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %2
%6 = tail call i32 @exit_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
ret i32 %4
8: ; preds = %2, %27
%9 = load i32, i32* @LOG_USER
%10 = load i32, i32* @LOG_DEBUG
%11 = tail call i32 @LOG_MAKEPRI(i32 %9, i32 %10)
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %0, i32 %12, i32 0)
%14 = load i32, i32* @LOG_USER
%15 = load i32, i32* @LOG_DEBUG
%16 = tail call i32 @LOG_MAKEPRI(i32 %14, i32 %15)
%17 = icmp eq i32 %13, -1
br i1 %17, label %18, label %20
18: ; preds = %8
%19 = tail call i32 @exit_error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %18, %8
%21 = call i32 @read(i32 %13, i8* %3, i32 1)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %27
23: ; preds = %20, %23
%24 = call i32 @write(i32 %1, i8* %3, i32 1)
%25 = call i32 @read(i32 %13, i8* %3, i32 1)
%26 = icmp eq i32 %25, 1
br i1 %26, label %23, label %27
27: ; preds = %23, %20
%28 = tail call i32 @close(i32 %13)
br label %8
}