#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*/ EXIT_FAILURE;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int child(int fd_inp, int fd_out) {
  int flags = 0;
  char buf[65536] = {};
  int cur_size = 0;
  flags = fcntl(fd_inp, F_GETFD, 0);
  flags &= ~O_NONBLOCK;
  if ((fcntl(fd_inp, F_SETFL, flags))) {
    perror(("Error fcntl"));
    exit(EXIT_FAILURE);
  }
  flags = fcntl(fd_out, F_GETFD, 0);
  flags &= ~O_NONBLOCK;
  if ((fcntl(fd_out, F_SETFL, flags))) {
    perror(("Error fcntl"));
    exit(EXIT_FAILURE);
  }
  while ((cur_size = read(fd_inp, buf, 4096)) != 0) {
    write(fd_out, buf, cur_size);
  }
  close(fd_out);
  close(fd_inp);
  return 0;
}


x86 assembly (clang, -O0):
<child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x10010(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
movl   $0x0,-0x10014(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     1204 <child+0x94>
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     1263 <child+0xf3>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1268 <child+0xf8>
mov    -0x4(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,-0x10014(%rbp)
cmp    $0x0,%eax
je     12a5 <child+0x135>
mov    -0x8(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    -0x10014(%rbp),%edx
call   1040 <write@plt>
jmp    1268 <child+0xf8>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10000,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    %rsp,%rdi
mov    $0x10000,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e36(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e3a(%rip),%r15
mov    (%r15),%edx
not    %edx
and    %eax,%edx
mov    0x2e1c(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
jne    1251 <child+0xe1>
mov    (%r12),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    (%r15),%edx
not    %edx
and    %eax,%edx
mov    0x0(%rbp),%esi
mov    %r14d,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
jne    1251 <child+0xe1>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1230 <child+0xc0>
mov    %rsp,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    1210 <child+0xa0>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@F_GETFD = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str = [12 x i8] c"Error fcntl\00"
@EXIT_FAILURE = global i32 0
define i32 @child(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [65536 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
%8 = bitcast [65536 x i8]* %6 to i8*
store i32 0, i32* %7
%9 = load i32, i32* %3
%10 = load i32, i32* @F_GETFD
%11 = call i32 @fcntl(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* @O_NONBLOCK
%13 = xor i32 %12, -1
%14 = load i32, i32* %5
%15 = and i32 %14, %13
store i32 %15, i32* %5
%16 = load i32, i32* %3
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* %5
%19 = call i32 @fcntl(i32 %16, i32 %17, i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = load i32, i32* %4
%27 = load i32, i32* @F_GETFD
%28 = call i32 @fcntl(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %5
%29 = load i32, i32* @O_NONBLOCK
%30 = xor i32 %29, -1
%31 = load i32, i32* %5
%32 = and i32 %31, %30
store i32 %32, i32* %5
%33 = load i32, i32* %4
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* %5
%36 = call i32 @fcntl(i32 %33, i32 %34, i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %25
%39 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %25
br label %43
43: ; preds = %48, %42
%44 = load i32, i32* %3
%45 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 4096)
store i32 %46, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load i32, i32* %4
%50 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%51 = load i32, i32* %7
%52 = call i32 @write(i32 %49, i8* %50, i32 %51)
br label %43
53: ; preds = %43
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %3
%57 = call i32 @close(i32 %56)
ret i32 0
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str = [12 x i8] c"Error fcntl\00"
@EXIT_FAILURE = global i32 0
define i32 @child(i32 %0, i32 %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @F_GETFD
%6 = tail call i32 @fcntl(i32 %0, i32 %5, i32 0)
%7 = load i32, i32* @O_NONBLOCK
%8 = xor i32 %7, -1
%9 = and i32 %6, %8
%10 = load i32, i32* @F_SETFL
%11 = tail call i32 @fcntl(i32 %0, i32 %10, i32 %9)
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32, i32* @F_GETFD
%19 = tail call i32 @fcntl(i32 %1, i32 %18, i32 0)
%20 = load i32, i32* @O_NONBLOCK
%21 = xor i32 %20, -1
%22 = and i32 %19, %21
%23 = load i32, i32* @F_SETFL
%24 = tail call i32 @fcntl(i32 %1, i32 %23, i32 %22)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = call i32 @read(i32 %0, i8* %4, i32 4096)
%28 = icmp eq i32 %27, 0
br i1 %28, label %38, label %33
29: ; preds = %17
%30 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %26, %33
%34 = phi i32 [ %36, %33 ], [ %27, %26 ]
%35 = call i32 @write(i32 %1, i8* %4, i32 %34)
%36 = call i32 @read(i32 %0, i8* %4, i32 4096)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %33
38: ; preds = %33, %26
%39 = tail call i32 @close(i32 %1)
%40 = tail call i32 @close(i32 %0)
ret i32 0
}