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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fds[2];
  pid_t pid;
  if (pipe(fds) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day09/pipe.c",
              20, "pipe", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  if ((pid = fork()) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day09/pipe.c",
              23, "fork", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  if (pid == 0) {
    close(fds[0]);
    write(fds[1], "hello", 6);
    close(fds[1]);
    exit(EXIT_SUCCESS);
  }
  char buf[1024] = {};
  close(fds[1]);
  read(fds[0], buf, 1024);
  close(fds[0]);
  printf("buf=%s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    121c <main+0x7c>
jmp    11c9 <main+0x29>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x414(%rbp),%edi
mov    %rax,%r9
lea    0xe0a(%rip),%rsi
lea    0xe13(%rip),%rdx
mov    $0x14,%ecx
lea    0xe9a(%rip),%r8
call   1090 <fprintf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1287 <main+0xe7>
jmp    1234 <main+0x94>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x418(%rbp),%edi
mov    %rax,%r9
lea    0xd9f(%rip),%rsi
lea    0xda8(%rip),%rdx
mov    $0x17,%ecx
lea    0xe34(%rip),%r8
call   1090 <fprintf@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jne    12c3 <main+0x123>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xe0a(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    -0x410(%rbp),%rsi
lea    0xdab(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x410,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     1220 <main+0x80>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1255 <main+0xb5>
cmp    $0xffffffff,%eax
je     128a <main+0xea>
lea    0x10(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xea6(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
ret
mov    0x2db9(%rip),%rax
mov    (%rax),%ebx
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdc2(%rip),%rsi
lea    0xdcb(%rip),%rdx
lea    0xe57(%rip),%r8
mov    %ebx,%edi
mov    $0x14,%ecx
jmp    12bd <main+0x11d>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe44(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd58(%rip),%rsi
lea    0xd61(%rip),%rdx
lea    0xdf2(%rip),%r8
mov    %ebx,%edi
mov    $0x17,%ecx
mov    %rax,%r9
call   1090 <fprintf@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day09/pipe.c\00"
@.str.2 = [5 x i8] c"pipe\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [6 x i8] c"hello\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [8 x i8] c"buf=%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %17
8: ; preds = %0
br label %9
9: ; preds = %8
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; No predecessors!
br label %17
17: ; preds = %16, %0
%18 = call i32 (...) @fork()
store i32 %18, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %29
20: ; preds = %17
br label %21
21: ; preds = %20
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; No predecessors!
br label %29
29: ; preds = %28, %17
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %44
32: ; preds = %29
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @write(i32 %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 6)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %29
%45 = bitcast [1024 x i8]* %4 to i8*
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%50 = load i32, i32* %49
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 1024)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%57 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %56)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day09/pipe.c\00"
@.str.2 = [5 x i8] c"pipe\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [6 x i8] c"hello\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [8 x i8] c"buf=%s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1024 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = call i32 (...) @fork()
switch i32 %15, label %33 [
i32 -1, label %16
i32 0, label %23
]
16: ; preds = %14
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 6)
%29 = load i32, i32* %26
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @EXIT_SUCCESS
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %14
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %4
%39 = call i32 @read(i32 %38, i8* %34, i32 1024)
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
%42 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %34)
ret i32 0
}