#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
size_t write(int, char *, int);
int main() {
  int fd[2], result;
  size_t size;
  char resstring[14];
  if (pipe(fd) < 0) {
    printf("Can\'t create pipe\n");
    exit(-1);
  }
  result = fork();
  if (result < 0) {
    printf("Can\'t fork child\n");
    exit(-1);
  } else if (result > 0) {
    close(fd[0]);
    size = write(fd[1], "Hello, world!", 14);
    if (size != 14) {
      printf("Can\'t write all string\n");
      exit(-1);
    }
    close(fd[1]);
    printf("Parent exit\n");
  } else {
    close(fd[1]);
    size = read(fd[0], resstring, 14);
    if (size < 0) {
      printf("Can\'t read string\n");
      exit(-1);
    }
    printf("%s\n", resstring);
    close(fd[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11aa <main+0x3a>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11d6 <main+0x66>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jle    123e <main+0xce>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xe33(%rip),%rsi
mov    $0xe,%edx
call   1030 <write@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xe,-0x18(%rbp)
je     1223 <main+0xb3>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1298 <main+0x128>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x26(%rbp),%rsi
mov    $0xe,%edx
call   1070 <read@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jae    127e <main+0x10e>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    -0x26(%rbp),%rsi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    129d <main+0x12d>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     1208 <main+0x98>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     1211 <main+0xa1>
je     11d0 <main+0x60>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe57(%rip),%rsi
mov    $0xe,%edx
call   1040 <write@plt>
cmp    $0xe,%rax
jne    121a <main+0xaa>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
jmp    1200 <main+0x90>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x12(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xe,%edx
call   1070 <read@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe33(%rip),%rdi
jmp    1221 <main+0xb1>
lea    0xe19(%rip),%rdi
jmp    1221 <main+0xb1>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Can't create pipe\0A\00"
@.str.1 = [18 x i8] c"Can't fork child\0A\00"
@.str.2 = [14 x i8] c"Hello, world!\00"
@.str.3 = [24 x i8] c"Can't write all string\0A\00"
@.str.4 = [13 x i8] c"Parent exit\0A\00"
@.str.5 = [19 x i8] c"Can't read string\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i64
%5 = alloca [14 x i8]
store i32 0, i32* %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i32 (...) @fork()
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %12
%20 = load i32, i32* %3
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %39
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i64 @write(i32 %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 14)
store i64 %28, i64* %4
%29 = load i64, i64* %4
%30 = icmp ne i64 %29, 14
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %22
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %58
39: ; preds = %19
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 0
%46 = call i64 @read(i32 %44, i8* %45, i32 14)
store i64 %46, i64* %4
%47 = load i64, i64* %4
%48 = icmp ult i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %39
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %39
%53 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %52, %34
br label %59
59: ; preds = %58
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [14 x i8] c"Hello, world!\00"
@str = [12 x i8] c"Parent exit\00"
@str.7 = [23 x i8] c"Can't write all string\00"
@str.8 = [17 x i8] c"Can't fork child\00"
@str.9 = [18 x i8] c"Can't create pipe\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [14 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = call i32 (...) @fork()
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.8, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %11
%18 = icmp eq i32 %12, 0
br i1 %18, label %33, label %19
19: ; preds = %17
%20 = load i32, i32* %5
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i64 @write(i32 %23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 14)
%25 = icmp eq i64 %24, 14
br i1 %25, label %29, label %26
26: ; preds = %19
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %19
%30 = load i32, i32* %22
%31 = call i32 @close(i32 %30)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %42
33: ; preds = %17
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %5
%38 = call i64 @read(i32 %37, i8* %4, i32 14)
%39 = call i32 @puts(i8* %4)
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %29, %33
ret i32 0
}