#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 *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ write(int, char *, int);
main() {
  int pipe_fd[2];
  pid_t pid;
  char r_buf[4];
  char *w_buf;
  int writenum;
  int cmd;
  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[0]);
    close(pipe_fd[1]);
    sleep(10);
    exit(0);
  } else if (pid > 0) {
    sleep(1);
    close(pipe_fd[0]);
    printf("Now\n");
    w_buf = "111";
    if ((writenum = (int)write(pipe_fd[1], w_buf, 4)) == -1) {
      printf("write to pipe error\n");
    } else {
      printf("the bytes write to pipe is %d \n", writenum);
    }
    printf("Now\n");
    close(pipe_fd[1]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11ca <main+0x4a>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1200 <main+0x80>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    128d <main+0x10d>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde6(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1266 <main+0xe6>
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1277 <main+0xf7>
mov    -0x2c(%rbp),%esi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    1292 <main+0x112>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x14(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0xc(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     123d <main+0xad>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1253 <main+0xc3>
jle    1236 <main+0xa6>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%rsp),%edi
lea    0xe0c(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1215 <main+0x85>
lea    0xdfa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1221 <main+0x91>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"pipe create error\0A\00"
@.str.1 = [5 x i8] c"Now\0A\00"
@.str.2 = [4 x i8] c"111\00"
@.str.3 = [21 x i8] c"write to pipe error\0A\00"
@.str.4 = [32 x i8] c"the bytes write to pipe is %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca [4 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 4)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, 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 = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i64 (...) @fork()
store i64 %17, i64* %3
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%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 10)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %16
%29 = load i64, i64* %3
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %53
31: ; preds = %28
%32 = call i32 @sleep(i32 1)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %5
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%38 = load i32, i32* %37
%39 = load i8*, i8** %5
%40 = call i64 @write(i32 %38, i8* %39, i32 4)
%41 = trunc i64 %40 to i32
store i32 %41, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %48
45: ; preds = %31
%46 = load i32, i32* %6
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %45, %43
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %48, %28
br label %54
54: ; preds = %53
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str.2 = [4 x i8] c"111\00"
@.str.4 = [32 x i8] c"the bytes write to pipe is %d \0A\00"
@str.5 = [4 x i8] c"Now\00"
@str.6 = [20 x i8] c"write to pipe error\00"
@str.7 = [18 x i8] c"pipe create error\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [4 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 4)
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, 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 %23
15: ; preds = %12
%16 = load i32, i32* %6
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = call i32 @sleep(i32 10)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %12
%24 = icmp sgt i64 %13, 0
br i1 %24, label %25, label %43
25: ; preds = %23
%26 = call i32 @sleep(i32 1)
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.5, i64 0, i64 0))
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i64 @write(i32 %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 4)
%33 = trunc i64 %32 to i32
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
br label %39
37: ; preds = %25
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %39
39: ; preds = %37, %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.5, i64 0, i64 0))
%41 = load i32, i32* %30
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %23, %39
ret i32 0
}