#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 int ssize_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int pipe_fd[2] = {};
  int inp_file = 0;
  int err_code = 0;
  pid_t temp = 0;
  char buf[4 * 1024] = {};
  err_code = pipe(pipe_fd);
  if (err_code) {
    printf("I can't open pipe\n");
    return 1;
  }
  if ((temp = fork()) == 0) {
    ssize_t inp_size = 0;
    inp_file = open(argv[1], O_RDONLY | O_NONBLOCK);
    while ((inp_size = read(inp_file, buf, 4 * 1024)) == 4 * 1024) {
      fprintf(stderr, "%d\n", inp_size);
      write(pipe_fd[1], buf, 4 * 1024);
    }
    if (inp_size != 0 && inp_size != 4 * 1024) {
      write(pipe_fd[1], buf, inp_size);
    }
    close(pipe_fd[1]);
    close(inp_file);
    fprintf(stderr, "\nERROR %d\n", inp_size);
    return 0;
  } else if (temp > 0) {
    ssize_t out_size = 0;
    close(pipe_fd[1]);
    while ((out_size = read(pipe_fd[0], buf, 4 * 1024)) == 4 * 1024) {
      write(1, buf, 4 * 1024);
      fprintf(stderr, "%d %d\n", out_size, pipe_fd[0]);
    }
    if (out_size != 0 && out_size != 4 * 1024) {
      write(1, buf, out_size);
    }
    close(pipe_fd[0]);
    return 0;
  }
  perror("Fork error\n");
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1220 <main+0x80>
lea    0xdf1(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x264>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1327 <main+0x187>
movl   $0x0,-0x1034(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1034(%rbp)
cmp    $0x1000,%eax
jne    12bc <main+0x11c>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x1034(%rbp),%edx
lea    0xd7e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
jmp    1261 <main+0xc1>
cmpl   $0x0,-0x1034(%rbp)
je     12ee <main+0x14e>
cmpl   $0x1000,-0x1034(%rbp)
je     12ee <main+0x14e>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1034(%rbp),%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
mov    -0x1034(%rbp),%edx
lea    0xcff(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1404 <main+0x264>
cmpq   $0x0,-0x28(%rbp)
jle    13ec <main+0x24c>
movl   $0x0,-0x1038(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1038(%rbp)
cmp    $0x1000,%eax
jne    13a4 <main+0x204>
lea    -0x1030(%rbp),%rsi
mov    $0x1,%edi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    0x2c4a(%rip),%rax
mov    (%rax),%edi
mov    -0x1038(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xc86(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1344 <main+0x1a4>
cmpl   $0x0,-0x1038(%rbp)
je     13d8 <main+0x238>
cmpl   $0x1000,-0x1038(%rbp)
je     13d8 <main+0x238>
lea    -0x1030(%rbp),%rsi
mov    -0x1038(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1404 <main+0x264>
jmp    13f1 <main+0x251>
lea    0xc2d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %eax,%eax
je     11f0 <main+0x50>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    134a <main+0x1aa>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     1297 <main+0xf7>
jle    135c <main+0x1bc>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rbx
mov    0x2db5(%rip),%r14
lea    0xde9(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
cmp    $0x1000,%eax
jne    1272 <main+0xd2>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    (%r14),%edi
mov    0x8(%rsp),%ecx
mov    %rbp,%rsi
mov    $0x1000,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1230 <main+0x90>
test   %eax,%eax
je     1287 <main+0xe7>
lea    0x10(%rsp),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %ebx,%ebx
jmp    134a <main+0x1aa>
mov    0x8(%rbx),%rdi
mov    0x2d3e(%rip),%rax
mov    0x2d3f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    0x2d0f(%rip),%r12
lea    0xd3f(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
cmp    $0x1000,%eax
jne    130d <main+0x16d>
mov    (%r12),%edi
mov    %r15,%rsi
mov    $0x1000,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
jmp    12d0 <main+0x130>
mov    %eax,%ebp
test   %eax,%eax
je     1323 <main+0x183>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    (%r12),%edi
lea    0xcc1(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebx,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcaf(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%ebx
jmp    134a <main+0x1aa>

LLVM IR (-O0):
@.str = [19 x i8] c"I can't open pipe\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [11 x i8] c"\0AERROR %d\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [12 x i8] c"Fork error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca [4096 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = bitcast [2 x i32]* %6 to i8*
store i32 0, i32* %7
store i32 0, i32* %8
store i64 0, i64* %9
%14 = bitcast [4096 x i8]* %10 to i8*
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %104
21: ; preds = %2
%22 = call i64 (...) @fork()
store i64 %22, i64* %9
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %66
24: ; preds = %21
store i32 0, i32* %11
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_RDONLY
%29 = load i32, i32* @O_NONBLOCK
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %27, i32 %30)
store i32 %31, i32* %7
br label %32
32: ; preds = %37, %24
%33 = load i32, i32* %7
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 4096)
store i32 %35, i32* %11
%36 = icmp eq i32 %35, 4096
br i1 %36, label %37, label %45
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i32, i32* %11
%40 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%44 = call i32 @write(i32 %42, i8* %43, i32 4096)
br label %32
45: ; preds = %32
%46 = load i32, i32* %11
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %57
48: ; preds = %45
%49 = load i32, i32* %11
%50 = icmp ne i32 %49, 4096
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%53 = load i32, i32* %52
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%55 = load i32, i32* %11
%56 = call i32 @write(i32 %53, i8* %54, i32 %55)
br label %57
57: ; preds = %51, %48, %45
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* @stderr
%64 = load i32, i32* %11
%65 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %64)
store i32 0, i32* %3
br label %104
66: ; preds = %21
%67 = load i64, i64* %9
%68 = icmp sgt i64 %67, 0
br i1 %68, label %69, label %101
69: ; preds = %66
store i32 0, i32* %12
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %79, %69
%74 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%75 = load i32, i32* %74
%76 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%77 = call i32 @read(i32 %75, i8* %76, i32 4096)
store i32 %77, i32* %12
%78 = icmp eq i32 %77, 4096
br i1 %78, label %79, label %87
79: ; preds = %73
%80 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%81 = call i32 @write(i32 1, i8* %80, i32 4096)
%82 = load i32, i32* @stderr
%83 = load i32, i32* %12
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%85 = load i32, i32* %84
%86 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %83, i32 %85)
br label %73
87: ; preds = %73
%88 = load i32, i32* %12
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %97
90: ; preds = %87
%91 = load i32, i32* %12
%92 = icmp ne i32 %91, 4096
br i1 %92, label %93, label %97
93: ; preds = %90
%94 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%95 = load i32, i32* %12
%96 = call i32 @write(i32 1, i8* %94, i32 %95)
br label %97
97: ; preds = %93, %90, %87
%98 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%99 = load i32, i32* %98
%100 = call i32 @close(i32 %99)
store i32 0, i32* %3
br label %104
101: ; preds = %66
br label %102
102: ; preds = %101
%103 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %3
br label %104
104: ; preds = %102, %97, %57, %19
%105 = load i32, i32* %3
ret i32 %105
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [11 x i8] c"\0AERROR %d\0A\00"
@.str.3 = [7 x i8] c"%d %d\0A\00"
@.str.4 = [12 x i8] c"Fork error\0A\00"
@str = [18 x i8] c"I can't open pipe\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to [2 x i32]*
%5 = alloca [4096 x i8]
%6 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = bitcast i64* %3 to i32*
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %61
13: ; preds = %2
%14 = call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %40
16: ; preds = %13
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %20, %19
%22 = call i32 @open(i8* %18, i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %24
24: ; preds = %26, %16
%25 = call i32 @read(i32 %22, i8* %7, i32 4096)
switch i32 %25, label %31 [
i32 4096, label %26
i32 0, label %34
]
26: ; preds = %24
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4096)
%29 = load i32, i32* %23
%30 = call i32 @write(i32 %29, i8* %7, i32 4096)
br label %24
31: ; preds = %24
%32 = load i32, i32* %23
%33 = call i32 @write(i32 %32, i8* %7, i32 %25)
br label %34
34: ; preds = %24, %31
%35 = load i32, i32* %23
%36 = call i32 @close(i32 %35)
%37 = call i32 @close(i32 %22)
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %61
40: ; preds = %13
%41 = icmp sgt i64 %14, 0
br i1 %41, label %42, label %59
42: ; preds = %40
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %49, %42
%47 = load i32, i32* %8
%48 = call i32 @read(i32 %47, i8* %7, i32 4096)
switch i32 %48, label %54 [
i32 4096, label %49
i32 0, label %56
]
49: ; preds = %46
%50 = call i32 @write(i32 1, i8* %7, i32 4096)
%51 = load i32, i32* @stderr
%52 = load i32, i32* %8
%53 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 4096, i32 %52)
br label %46
54: ; preds = %46
%55 = call i32 @write(i32 1, i8* %7, i32 %48)
br label %56
56: ; preds = %46, %54
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
br label %61
59: ; preds = %40
%60 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %59, %56, %34, %11
%62 = phi i32 [ 1, %11 ], [ 0, %34 ], [ 0, %56 ], [ 2, %59 ]
ret i32 %62
}