#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EX_USAGE;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int mkfifo(char *, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ write(int, int *, int);
int fork_pipe_child(int master_fd) {
  pid_t child_pid = fork();
  if (0 != child_pid)
    return child_pid;
  remove("io/input");
  int result = mkfifo("io/input", 0666);
  if (0 != result)
    printf("\ninput pipe failed: [%d]\n", result);
  while (1) {
    int num_read, num_to_read = 100;
    int buf[num_to_read];
    int fd = open("io/input", O_RDONLY | O_NONBLOCK);
    printf("#initialized input pipe.\n");
    while (0 <= (num_read = read(fd, buf, num_to_read)))
      write(master_fd, buf, num_read);
  }
  exit(EX_USAGE);
  return -1;
}


x86 assembly (clang, -O0):
<fork_pipe_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     119b <fork_pipe_child+0x2b>
mov    -0x10(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret
lea    0xe5e(%rip),%rdi
call   1030 <remove@plt>
lea    0xe52(%rip),%rdi
mov    $0x1b6,%esi
call   1070 <mkfifo@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
je     11d7 <fork_pipe_child+0x67>
mov    -0x14(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11dc <fork_pipe_child+0x6c>
movl   $0x64,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2dce(%rip),%rax
or     (%rax),%esi
lea    0xddd(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jg     126b <fork_pipe_child+0xfb>
mov    -0x40(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
jmp    1239 <fork_pipe_child+0xc9>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
jmp    11dc <fork_pipe_child+0x6c>

x86 assembly (clang, -O2):
<fork_pipe_child>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r12d
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11ac <fork_pipe_child+0x2c>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4d(%rip),%rbx
mov    %rbx,%rdi
call   1030 <remove@plt>
mov    %rbx,%rdi
mov    $0x1b6,%esi
call   1080 <mkfifo@plt>
test   %eax,%eax
je     11dc <fork_pipe_child+0x5c>
lea    0xe36(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe40(%rip),%r15
jmp    11f3 <fork_pipe_child+0x73>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rsp
mov    %rsp,%r14
mov    %rsp,%rbx
add    $0xfffffffffffffe70,%rbx
mov    %rbx,%rsp
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
or     (%rax),%esi
lea    0xde4(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r13d
mov    %r15,%rdi
call   1040 <puts@plt>
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
test   %eax,%eax
js     11f0 <fork_pipe_child+0x70>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
jmp    1230 <fork_pipe_child+0xb0>

LLVM IR (-O0):
@.str = [9 x i8] c"io/input\00"
@.str.1 = [26 x i8] c"\0Ainput pipe failed: [%d]\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [26 x i8] c"#initialized input pipe.\0A\00"
@EX_USAGE = global i32 0
define i32 @fork_pipe_child(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %2
%10 = call i64 (...) @fork()
store i64 %10, i64* %3
%11 = load i64, i64* %3
%12 = icmp ne i64 0, %11
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i64, i64* %3
%15 = trunc i64 %14 to i32
ret i32 %15
16: ; preds = %1
%17 = call i32 @remove(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @mkfifo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 438)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp ne i32 0, %19
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %4
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %21, %16
br label %25
25: ; preds = %24, %44
store i32 100, i32* %6
%26 = load i32, i32* %6
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
store i8* %28, i8** %7
%29 = alloca i32, i64 %27
store i64 %27, i64* %8
%30 = load i32, i32* @O_RDONLY
%31 = load i32, i32* @O_NONBLOCK
%32 = or i32 %30, %31
%33 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %32)
store i32 %33, i32* %9
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %40, %25
%36 = load i32, i32* %9
%37 = load i32, i32* %6
%38 = call i32 @read(i32 %36, i32* %29, i32 %37)
store i32 %38, i32* %5
%39 = icmp sle i32 0, %38
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i32, i32* %2
%42 = load i32, i32* %5
%43 = call i32 @write(i32 %41, i32* %29, i32 %42)
br label %35
44: ; preds = %35
%45 = load i8*, i8** %7
br label %25
}

LLVM IR (-O2):
@.str = [9 x i8] c"io/input\00"
@.str.1 = [26 x i8] c"\0Ainput pipe failed: [%d]\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@EX_USAGE = global i32 0
@str = [25 x i8] c"#initialized input pipe.\00"
define i32 @fork_pipe_child(i32 %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = trunc i64 %2 to i32
ret i32 %5
6: ; preds = %1
%7 = tail call i32 @remove(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @mkfifo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 438)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %12
12: ; preds = %10, %6
br label %13
13: ; preds = %12, %29
%14 = tail call i8* @llvm.stacksave()
%15 = alloca [100 x i32]
%16 = getelementptr inbounds [100 x i32], [100 x i32]* %15, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%22 = call i32 @read(i32 %20, i32* %16, i32 100)
%23 = icmp sgt i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %13, %24
%25 = phi i32 [ %27, %24 ], [ %22, %13 ]
%26 = call i32 @write(i32 %0, i32* %16, i32 %25)
%27 = call i32 @read(i32 %20, i32* %16, i32 100)
%28 = icmp sgt i32 %27, -1
br i1 %28, label %24, label %29
29: ; preds = %24, %13
tail call void @llvm.stackrestore(i8* %14)
br label %13
}