#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);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int 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");
    return -1;
  }
  if ((pid = fork()) == 0) {
    close(pipe_fd[0]);
    close(pipe_fd[1]);
    sleep(10);
    return 0;
  } else if (pid > 0) {
    sleep(1);
    close(pipe_fd[0]);
    w_buf = "111";
    if ((writenum = 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);
    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    11bc <main+0x4c>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <main+0xfd>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f7 <main+0x87>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
movl   $0x0,-0x4(%rbp)
jmp    126d <main+0xfd>
cmpq   $0x0,-0x18(%rbp)
jle    1268 <main+0xf8>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xdf8(%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    124f <main+0xdf>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1260 <main+0xf0>
mov    -0x2c(%rbp),%esi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    126d <main+0xfd>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xc(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0x4(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     11f8 <main+0x78>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     120b <main+0x8b>
jle    123e <main+0xbe>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0xe29(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1229 <main+0xa9>
lea    0xe17(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1235 <main+0xb5>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    123e <main+0xbe>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
jmp    123e <main+0xbe>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"pipe create error\0A\00"
@.str.1 = [4 x i8] c"111\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() {
%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 %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %49
15: ; preds = %0
%16 = call i64 (...) @fork()
store i64 %16, i64* %3
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = call i32 @sleep(i32 10)
store i32 0, i32* %1
br label %49
26: ; preds = %15
%27 = load i64, i64* %3
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %26
%30 = call i32 @sleep(i32 1)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %5
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%35 = load i32, i32* %34
%36 = load i8*, i8** %5
%37 = call i32 @write(i32 %35, i8* %36, i32 4)
store i32 %37, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %44
41: ; preds = %29
%42 = load i32, i32* %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41, %39
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %44, %26
br label %49
49: ; preds = %13, %18, %48
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"111\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() {
%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 %11
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
br label %38
11: ; preds = %0
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = load i32, i32* %6
%16 = call i32 @close(i32 %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = call i32 @sleep(i32 10)
br label %38
21: ; preds = %11
%22 = icmp sgt i64 %12, 0
br i1 %22, label %23, label %38
23: ; preds = %21
%24 = call i32 @sleep(i32 1)
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %35
33: ; preds = %23
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %35
35: ; preds = %33, %31
%36 = load i32, i32* %27
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %21, %14, %9
%39 = phi i32 [ -1, %9 ], [ 0, %14 ], [ 0, %21 ], [ 0, %35 ]
ret i32 %39
}