#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);
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[0]);
    close(fds[1]);
    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   10c0 <fork@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jge    1202 <main+0x62>
lea    0xe08(%rip),%rdi
call   1090 <perror@plt>
jmp    1297 <main+0xf7>
cmpq   $0x0,-0x78(%rbp)
jne    1249 <main+0xa9>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xde8(%rip),%rdi
call   1050 <printf@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdbb(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xdb4(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
movl   $0x0,-0x4(%rbp)
jmp    129e <main+0xfe>
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   10c0 <fork@plt>
test   %rax,%rax
js     1248 <main+0xa8>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     125c <main+0xbc>
lea    0xe0c(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe01(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    %r14d,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdb1(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
jmp    123d <main+0x9d>
lea    0xda2(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10a0 <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 = [16 x i8] c"pid: %d parent\0A\00"
@.str.3 = [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 %43
14: ; preds = %0
%15 = load i64, i64* %4
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 @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 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %14
%30 = call i32 (...) @getpid()
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @write(i32 %36, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 15)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %29
%42 = call i32 @wait(i32* null)
store i32 0, i32* %1
br label %44
43: ; preds = %12
store i32 -1, i32* %1
br label %44
44: ; preds = %43, %41
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [15 x i8] c"pid: %d child\0A\00"
@.str.2 = [16 x i8] c"pid: %d parent\0A\00"
@.str.3 = [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 %34
12: ; preds = %0
%13 = icmp eq i64 %8, 0
%14 = call i32 (...) @getpid()
br i1 %13, label %15, label %24
15: ; preds = %12
%16 = call i32 @printf(i8* 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 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %12
%25 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 15)
%31 = load i32, i32* %28
%32 = call i32 @close(i32 %31)
%33 = call i32 @wait(i32* null)
br label %34
34: ; preds = %10, %24
%35 = phi i32 [ -1, %10 ], [ 0, %24 ]
ret i32 %35
}