#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ write(int, int *, int);
int main() {
  int pipes[2];
  if (pipe(pipes) < 0) {
    perror("pipe(pipes)");
    exit(-1);
  }
  if (fork() == 0) {
    close(pipes[0]);
    int num = 0;
    while (num != -1) {
      printf("(child) Give num > ");
      scanf("%d", &num);
      write(pipes[1], &num, 4);
    }
  } else {
    close(pipes[1]);
    int num;
    FILE *fp;
    fp = fopen("positive.txt", "w");
    if (fp <= 0) {
      perror("fopen()");
    }
    while (1) {
      read(pipes[0], &num, 4);
      if (num == -1) {
        fclose(fp);
        break;
      }
      if (num > 0) {
        fprintf(fp, "(parent) Num = %d\n", num);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11f8 <main+0x38>
lea    0xe17(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%rax
jne    1259 <main+0x99>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     1254 <main+0x94>
lea    0xde3(%rip),%rdi
call   1050 <printf@plt>
lea    0xdeb(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <scanf@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    1218 <main+0x58>
jmp    12e4 <main+0x124>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xdbb(%rip),%rdi
lea    0xdc1(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
ja     128f <main+0xcf>
lea    0xda8(%rip),%rdi
call   10c0 <perror@plt>
jmp    1294 <main+0xd4>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    12bd <main+0xfd>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12df <main+0x11f>
cmpl   $0x0,-0x14(%rbp)
jle    12da <main+0x11a>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd65(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1294 <main+0xd4>
jmp    12e4 <main+0x124>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     12e8 <main+0x128>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     1275 <main+0xb5>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0xe27(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     12d7 <main+0x117>
mov    0x8(%rsp),%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0x4(%rsp),%edx
cmp    $0xffffffff,%edx
je     126b <main+0xab>
lea    0xe03(%rip),%r14
lea    0x4(%rsp),%rbx
jmp    125a <main+0x9a>
xchg   %ax,%ax
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0x4(%rsp),%edx
cmp    $0xffffffff,%edx
je     126b <main+0xab>
test   %edx,%edx
jle    1240 <main+0x80>
mov    %r15,%rdi
mov    %r14,%rsi
call   1090 <fprintf@plt>
jmp    1240 <main+0x80>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    12cb <main+0x10b>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
movl   $0x0,0x4(%rsp)
lea    0xd7f(%rip),%r14
lea    0xd8c(%rip),%r15
lea    0x4(%rsp),%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <scanf@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmpl   $0xffffffff,0x4(%rsp)
jne    12a0 <main+0xe0>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd54(%rip),%rdi
call   10c0 <perror@plt>
jmp    1214 <main+0x54>
lea    0xd11(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"pipe(pipes)\00"
@.str.1 = [20 x i8] c"(child) Give num > \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [13 x i8] c"positive.txt\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [8 x i8] c"fopen()\00"
@.str.6 = [19 x i8] c"(parent) Num = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
store i32 0, i32* %3
br label %19
19: ; preds = %22, %15
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @write(i32 %26, i32* %3, i32 4)
br label %19
28: ; preds = %19
br label %57
29: ; preds = %12
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %33, i32** %5
%34 = load i32*, i32** %5
%35 = icmp ule i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %36, %29
br label %39
39: ; preds = %38, %55
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @read(i32 %41, i32* %4, i32 4)
%43 = load i32, i32* %4
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i32*, i32** %5
%47 = call i32 @fclose(i32* %46)
br label %56
48: ; preds = %39
%49 = load i32, i32* %4
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32*, i32** %5
%53 = load i32, i32* %4
%54 = call i32 @fprintf(i32* %52, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %51, %48
br label %39
56: ; preds = %45
br label %57
57: ; preds = %56, %28
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"pipe(pipes)\00"
@.str.1 = [20 x i8] c"(child) Give num > \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [13 x i8] c"positive.txt\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [8 x i8] c"fopen()\00"
@.str.6 = [19 x i8] c"(parent) Num = %d\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = load i32, i32* %5
%16 = call i32 @close(i32 %15)
%17 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %19
19: ; preds = %14, %19
%20 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%22 = load i32, i32* %18
%23 = call i32 @write(i32 %22, i32* %2, i32 4)
%24 = load i32, i32* %2
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %19
26: ; preds = %19
br label %53
27: ; preds = %11
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = bitcast i32* %3 to i8*
%32 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %34, %27
%37 = load i32, i32* %5
%38 = call i32 @read(i32 %37, i32* %3, i32 4)
%39 = load i32, i32* %3
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %48, %36
%42 = call i32 @fclose(i32* %32)
br label %53
43: ; preds = %36, %48
%44 = phi i32 [ %51, %48 ], [ %39, %36 ]
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %44)
br label %48
48: ; preds = %46, %43
%49 = load i32, i32* %5
%50 = call i32 @read(i32 %49, i32* %3, i32 4)
%51 = load i32, i32* %3
%52 = icmp eq i32 %51, -1
br i1 %52, label %41, label %43
53: ; preds = %41, %26
ret i32 0
}