#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*/ something;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ child(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ explain_wait_status(scalar_t__, int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ wait(int *);
int write(int, double *, int);
int main(void) {
  pid_t p;
  int pfd[2];
  int status;
  double something;
  printf("Parent: Creating pipe...\n");
  if (pipe(pfd) < 0) {
    perror("pipe");
    exit(1);
  }
  printf("Parent: Creating child...\n");
  p = fork();
  if (p < 0) {
    perror("fork");
    exit(1);
  }
  if (p == 0) {
    child(pfd[0]);
    assert(0);
  }
  something = 1234.567;
  if (write(pfd[1], &something, sizeof(something)) != sizeof(something)) {
    perror("parent: write to pipe");
    exit(1);
  }
  printf(
      "Parent: Created child with PID = %ld, waiting for it to terminate...\n",
      (long)p);
  p = wait(&status);
  explain_wait_status(p, status);
  printf("Parent: All done, exiting...\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11e6 <main+0x46>
lea    0xe7c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1220 <main+0x80>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    123a <main+0x9a>
mov    -0x18(%rbp),%edi
call   1030 <child@plt>
xor    %edi,%edi
call   1070 <assert@plt>
movsd  0xdbe(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cltq
cmp    $0x8,%rax
je     127a <main+0xda>
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x1c(%rbp),%rdi
call   10a0 <wait@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   10c0 <explain_wait_status@plt>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xea5(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     1267 <main+0xb7>
lea    0xe9f(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1270 <main+0xc0>
mov    %rax,%rbx
jne    1205 <main+0x55>
mov    0x10(%rsp),%edi
call   1030 <child@plt>
xor    %edi,%edi
call   1080 <assert@plt>
movabs $0x40934a449ba5e354,%rax
4a 93 40
mov    %rax,0x18(%rsp)
mov    0x14(%rsp),%edi
lea    0x18(%rsp),%rsi
mov    $0x8,%edx
call   1050 <write@plt>
cmp    $0x8,%eax
jne    1279 <main+0xc9>
lea    0xde8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
mov    0xc(%rsp),%esi
mov    %rax,%rdi
call   10d0 <explain_wait_status@plt>
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xda8(%rip),%rdi
jmp    1280 <main+0xd0>
lea    0xd89(%rip),%rdi
jmp    1280 <main+0xd0>
lea    0xd85(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Parent: Creating pipe...\0A\00"
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [27 x i8] c"Parent: Creating child...\0A\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [22 x i8] c"parent: write to pipe\00"
@.str.5 = [70 x i8] c"Parent: Created child with PID = %ld, waiting for it to terminate...\0A\00"
@.str.6 = [30 x i8] c"Parent: All done, exiting...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca double
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%15 = call i64 (...) @fork()
store i64 %15, i64* %2
%16 = load i64, i64* %2
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = load i64, i64* %2
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @child(i32 %26)
%28 = call i32 @assert(i32 0)
br label %29
29: ; preds = %24, %21
store double 0x40934A449BA5E354, double* %5
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @write(i32 %31, double* %5, i32 8)
%33 = sext i32 %32 to i64
%34 = icmp ne i64 %33, 8
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %29
%39 = load i64, i64* %2
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0), i64 %39)
%41 = call i64 @wait(i32* %4)
store i64 %41, i64* %2
%42 = load i64, i64* %2
%43 = load i32, i32* %4
%44 = call i32 @explain_wait_status(i64 %42, i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"pipe\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [22 x i8] c"parent: write to pipe\00"
@.str.5 = [70 x i8] c"Parent: Created child with PID = %ld, waiting for it to terminate...\0A\00"
@str = [25 x i8] c"Parent: Creating pipe...\00"
@str.7 = [26 x i8] c"Parent: Creating child...\00"
@str.8 = [29 x i8] c"Parent: All done, exiting...\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = alloca double
%4 = bitcast [2 x i32]* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast double* %3 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%16 = call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = icmp eq i64 %16, 0
br i1 %22, label %23, label %27
23: ; preds = %21
%24 = load i32, i32* %8
%25 = call i32 @child(i32 %24)
%26 = call i32 @assert(i32 0)
br label %27
27: ; preds = %23, %21
store double 0x40934A449BA5E354, double* %3
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @write(i32 %29, double* %3, i32 8)
%31 = icmp eq i32 %30, 8
br i1 %31, label %35, label %32
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %27
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0), i64 %16)
%37 = call i64 @wait(i32* %2)
%38 = load i32, i32* %2
%39 = call i32 @explain_wait_status(i64 %37, i32 %38)
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.8, i64 0, i64 0))
ret i32 0
}