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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ write(int, char *, int);
main(int argc, char **argv) {
  int pipe_fd[2];
  pid_t pid;
  char r_buf[4096];
  char w_buf[4096 * 2];
  int write_num;
  int r_num;
  memset(r_buf, 0, sizeof(r_buf));
  if (pipe(pipe_fd) < 0) {
    printf("pipe create error\n");
    exit(1);
  }
  if ((pid = fork()) == 0) {
    close(pipe_fd[1]);
    while (1) {
      sleep(1);
      r_num = (int)read(pipe_fd[0], r_buf, 1000);
      printf("child: read num is %d\n", r_num);
    }
  } else if (pid > 0) {
    close(pipe_fd[0]);
    memset(r_buf, 0, sizeof(r_buf));
    if ((write_num = (int)write(pipe_fd[1], w_buf, 1024)) == -1)
      printf("write to pipe error\n");
    else
      printf("the bytes write to pipe is %d \n", write_num);
    write_num = (int)write(pipe_fd[1], w_buf, 4096);
    close(pipe_fd[1]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11e7 <main+0x57>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1241 <main+0xb1>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x3e8,%edx
call   1080 <read@plt>
mov    %eax,-0x3028(%rbp)
mov    -0x3028(%rbp),%esi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1204 <main+0x74>
cmpq   $0x0,-0x20(%rbp)
jle    12d3 <main+0x143>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x3020(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    %eax,-0x3024(%rbp)
cmp    $0xffffffff,%eax
jne    129d <main+0x10d>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b1 <main+0x121>
mov    -0x3024(%rbp),%esi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x3020(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x3024(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    12d8 <main+0x148>
mov    -0x4(%rbp),%eax
add    $0x3030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x3018,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     12a1 <main+0x101>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     1227 <main+0x87>
jle    1294 <main+0xf4>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
lea    0x1010(%rsp),%rsi
mov    $0x400,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1269 <main+0xc9>
lea    0xdfb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1275 <main+0xd5>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0x10(%rsp),%r14
lea    0xdc4(%rip),%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x3e8,%edx
call   1090 <read@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1240 <main+0xa0>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
lea    0x1010(%rsp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x3018,%rsp
pop    %rbx
pop    %r14
ret
lea    0xda3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"pipe create error\0A\00"
@.str.1 = [23 x i8] c"child: read num is %d\0A\00"
@.str.2 = [21 x i8] c"write to pipe error\0A\00"
@.str.3 = [32 x i8] c"the bytes write to pipe is %d \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 i64
%8 = alloca [4096 x i8]
%9 = alloca [8192 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 4096)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%15 = call i64 @pipe(i32* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i64 (...) @fork()
store i64 %21, i64* %7
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %36
23: ; preds = %20
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %23, %27
%28 = call i32 @sleep(i32 1)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%32 = call i64 @read(i32 %30, i8* %31, i32 1000)
%33 = trunc i64 %32 to i32
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %34)
br label %27
36: ; preds = %20
%37 = load i64, i64* %7
%38 = icmp sgt i64 %37, 0
br i1 %38, label %39, label %65
39: ; preds = %36
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i32 0, i32 4096)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%46 = load i32, i32* %45
%47 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%48 = call i64 @write(i32 %46, i8* %47, i32 1024)
%49 = trunc i64 %48 to i32
store i32 %49, i32* %10
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %39
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %56
53: ; preds = %39
%54 = load i32, i32* %10
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %53, %51
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%58 = load i32, i32* %57
%59 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%60 = call i64 @write(i32 %58, i8* %59, i32 4096)
%61 = trunc i64 %60 to i32
store i32 %61, i32* %10
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %56, %36
br label %66
66: ; preds = %65
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str.1 = [23 x i8] c"child: read num is %d\0A\00"
@.str.3 = [32 x i8] c"the bytes write to pipe is %d \0A\00"
@str = [20 x i8] c"write to pipe error\00"
@str.4 = [18 x i8] c"pipe create error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [4096 x i8]
%5 = alloca [8192 x i8]
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%9 = call i32 @memset(i8* %7, i32 0, i32 4096)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %23, %19
%24 = call i32 @sleep(i32 1)
%25 = load i32, i32* %10
%26 = call i64 @read(i32 %25, i8* %7, i32 1000)
%27 = trunc i64 %26 to i32
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %23
29: ; preds = %16
%30 = icmp sgt i64 %17, 0
br i1 %30, label %31, label %49
31: ; preds = %29
%32 = load i32, i32* %10
%33 = call i32 @close(i32 %32)
%34 = call i32 @memset(i8* %7, i32 0, i32 4096)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i64 @write(i32 %36, i8* %8, i32 1024)
%38 = trunc i64 %37 to i32
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %44
42: ; preds = %31
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %44
44: ; preds = %42, %40
%45 = load i32, i32* %35
%46 = call i64 @write(i32 %45, i8* %8, i32 4096)
%47 = load i32, i32* %35
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %29, %44
ret i32 0
}