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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fds[2] = {0};
  pipe(fds);
  char buf[100] = {0};
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    goto err;
  }
  if (pid == 0) {
    printf("pid: %d child\n", getpid());
    memset(buf, 0, sizeof(buf));
    close(fds[1]);
    read(fds[0], buf, sizeof(buf));
    printf("buf:%s\n", buf);
    close(fds[0]);
    exit(0);
  } else {
    printf("pid: %d parent\n", getpid());
    close(fds[0]);
    write(fds[1], "hello,world", 15);
    close(fds[1]);
  }
  wait(NULL);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jge    1212 <main+0x62>
lea    0xdf8(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ce <main+0x11e>
cmpq   $0x0,-0x78(%rbp)
jne    127e <main+0xce>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   1090 <read@plt>
lea    -0x70(%rbp),%rsi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xd85(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d5 <main+0x125>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     125a <main+0xaa>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     126e <main+0xbe>
lea    0xe04(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
lea    0xdf7(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
mov    %r14d,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd9f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    124f <main+0x9f>
lea    0xd90(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1090 <read@plt>
lea    0xd61(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [15 x i8] c"pid: %d child\0A\00"
@.str.2 = [8 x i8] c"buf:%s\0A\00"
@.str.3 = [16 x i8] c"pid: %d parent\0A\00"
@.str.4 = [12 x i8] c"hello,world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [100 x i8]
%4 = alloca i64
store i32 0, i32* %1
%5 = bitcast [2 x i32]* %2 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = bitcast [100 x i8]* %3 to i8*
%9 = call i64 (...) @fork()
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %49
14: ; preds = %0
%15 = load i64, i64* %4
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 100)
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 100)
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%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)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %14
%36 = call i32 (...) @getpid()
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @write(i32 %42, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 15)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %35
%48 = call i32 @wait(i32* null)
store i32 0, i32* %1
br label %50
49: ; preds = %12
store i32 -1, i32* %1
br label %50
50: ; preds = %49, %47
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [15 x i8] c"pid: %d child\0A\00"
@.str.2 = [8 x i8] c"buf:%s\0A\00"
@.str.3 = [16 x i8] c"pid: %d parent\0A\00"
@.str.4 = [12 x i8] c"hello,world\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to [2 x i32]*
%3 = alloca [100 x i8]
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
%5 = bitcast i64* %1 to i32*
%6 = call i32 @pipe(i32* %5)
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %37
12: ; preds = %0
%13 = icmp eq i64 %8, 0
%14 = call i32 (...) @getpid()
br i1 %13, label %15, label %27
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%17 = call i32 @memset(i8* %7, i32 0, i32 100)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %5
%22 = call i32 @read(i32 %21, i8* %7, i32 100)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %12
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @write(i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 15)
%34 = load i32, i32* %31
%35 = call i32 @close(i32 %34)
%36 = call i32 @wait(i32* null)
br label %37
37: ; preds = %10, %27
%38 = phi i32 [ -1, %10 ], [ 0, %27 ]
ret i32 %38
}