#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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);
int main(int argc, char **argv) {
  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);
    exit(0);
  } else if (pid > 0) {
    sleep(1);
    close(pipe_fd[0]);
    w_buf = "13121215";
    printf("FILE: %s,LINE: %d\r\n",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/nicesu/wiki/refs/heads/master/OS/pt/IPC/"
           "pipe_fifo/pipe/pipetest_second.c",
           37);
    if ((writenum = write(pipe_fd[1], w_buf, sizeof(w_buf))) == -1)
      printf("write to pipe error\n");
    else
      printf("the bytes writ to pipe is %d\n", writenum);
    printf("FILE: %s,LINE: %d\r\n",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/nicesu/wiki/refs/heads/master/OS/pt/IPC/"
           "pipe_fifo/pipe/pipetest_second.c",
           43);
    close(pipe_fd[1]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11d3 <main+0x53>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <main+0x143>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
sete   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1c(%rbp)
test   $0x1,%al
jne    11f9 <main+0x79>
jmp    121a <main+0x9a>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    12be <main+0x13e>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xdd6(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xdd4(%rip),%rdi
lea    0xde1(%rip),%rsi
mov    $0x25,%edx
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    128b <main+0x10b>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129c <main+0x11c>
mov    -0x2c(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd79(%rip),%rdi
lea    0xd86(%rip),%rsi
mov    $0x2b,%edx
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    12c3 <main+0x143>
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    %ebx,%ebx
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    0x4(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     11c6 <main+0x56>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11d7 <main+0x67>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    $0xa,%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"pipe create error\0A\00"
@.str.1 = [9 x i8] c"13121215\00"
@.str.2 = [20 x i8] c"FILE: %s,LINE: %d\0D\0A\00"
@.str.3 = [163 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/nicesu/wiki/refs/heads/master/OS/pt/IPC/pipe_fifo/pipe/pipetest_second.c\00"
@.str.4 = [21 x i8] c"write to pipe error\0A\00"
@.str.5 = [30 x i8] c"the bytes writ 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 i32
%8 = alloca [4 x i8]
%9 = alloca 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 [4 x i8], [4 x i8]* %8, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 4)
%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 %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %57
19: ; preds = %2
%20 = call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
%22 = zext i1 %21 to i32
store i32 %22, i32* %7
br i1 %21, label %23, label %32
23: ; preds = %19
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = call i32 @sleep(i32 10)
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %19
%33 = load i32, i32* %7
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %56
35: ; preds = %32
%36 = call i32 @sleep(i32 1)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %9
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([163 x i8], [163 x i8]* @.str.3, i64 0, i64 0), i32 37)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%42 = load i32, i32* %41
%43 = load i8*, i8** %9
%44 = call i32 @write(i32 %42, i8* %43, i32 8)
store i32 %44, i32* %10
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %35
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %51
48: ; preds = %35
%49 = load i32, i32* %10
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %48, %46
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([163 x i8], [163 x i8]* @.str.3, i64 0, i64 0), i32 43)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %51, %32
br label %57
57: ; preds = %17, %56
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@str = [18 x i8] c"pipe create error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [4 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 4)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %24
13: ; preds = %2
%14 = call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = load i32, i32* %8
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = call i32 @sleep(i32 10)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %13, %11
%25 = phi i32 [ -1, %11 ], [ 0, %13 ]
ret i32 %25
}