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

/* Variables and functions */
int atoi(char *);
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);
int /*<<< orphan*/ strcpy(char *, char *);
int write(int, char *, int);
main() {
  int pipe_fd[2];
  pid_t pid;
  char r_buf[100];
  char w_buf[4];
  int r_num;
  int cmd;
  memset(r_buf, 0, sizeof(r_buf));
  memset(w_buf, 0, sizeof(w_buf));
  if (pipe(pipe_fd) < 0) {
    printf("pipe create error\n");
    exit(1);
  }
  if ((pid = fork()) == 0) {
    printf("\n");
    close(pipe_fd[1]);
    sleep(3);
    r_num = (int)read(pipe_fd[0], r_buf, 100);
    printf("read num is %d   the data read from the pipe is %d\n", r_num,
           atoi(r_buf));
    close(pipe_fd[0]);
    exit(0);
  } else if (pid > 0) {
    close(pipe_fd[0]);
    strcpy(w_buf, "111");
    if (write(pipe_fd[1], w_buf, 4) != -1) {
      printf("parent write over\n");
    }
    close(pipe_fd[1]);
    printf("parent close fd[1] over\n");
    sleep(10);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
lea    -0x84(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jge    1210 <main+0x60>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1296 <main+0xe6>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1090 <read@plt>
mov    %eax,-0x88(%rbp)
mov    -0x88(%rbp),%eax
mov    %eax,-0x90(%rbp)
lea    -0x80(%rbp),%rdi
call   10a0 <atoi@plt>
mov    -0x90(%rbp),%esi
mov    %eax,%edx
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1307 <main+0x157>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
lea    -0x84(%rbp),%rdi
lea    0xd90(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x84(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12e7 <main+0x137>
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
jmp    130c <main+0x15c>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1080 <memset@plt>
lea    0xc(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
lea    0x4(%rsp),%rdi
call   10a0 <pipe@plt>
test   %rax,%rax
js     1282 <main+0xb2>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     1298 <main+0xc8>
jle    1278 <main+0xa8>
mov    0x4(%rsp),%edi
call   1090 <close@plt>
lea    0xe0a(%rip),%rsi
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <write@plt>
cmp    $0xffffffff,%eax
je     1259 <main+0x89>
lea    0xdfc(%rip),%rdi
call   1050 <puts@plt>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
lea    0xdcf(%rip),%rdi
call   1050 <puts@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd9(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
mov    0x4(%rsp),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
mov    $0x64,%edx
call   10b0 <read@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   10c0 <atoi@plt>
lea    0xd23(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x4(%rsp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

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

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