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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ wait(int *);
main() {
  int fd;
  char buf[2];
  pid_t pid;
  int childstatus;
  pid = fork();
  if (pid == 0) {
    read(fd, buf, 2);
    perror("child:\n");
    printf(" in child %c\n", buf[0]);
    printf(" in child %c\n", buf[1]);
    close(fd);
  } else {
    fd = open("./test", O_RDONLY);
    wait(&childstatus);
    read(fd, buf, 2);
    printf("parent:\n");
    printf(" in parent %c\n", buf[0]);
    printf(" in parent %c\n", buf[1]);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e3 <main+0x73>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
movsbl -0xa(%rbp),%esi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x9(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    124f <main+0xdf>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0xe23(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
lea    -0x1c(%rbp),%rdi
call   1080 <wait@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0xa(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x9(%rbp),%esi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1204 <main+0x84>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
lea    0x2(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1060 <read@plt>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
movsbl 0x2(%rsp),%esi
lea    0xe43(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x3(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0x2(%rsp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
movsbl 0x2(%rsp),%esi
lea    0xddd(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x3(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f5 <main+0x75>

LLVM IR (-O0):
@.str = [8 x i8] c"child:\0A\00"
@.str.1 = [14 x i8] c" in child %c\0A\00"
@.str.2 = [7 x i8] c"./test\00"
@O_RDONLY = global i32 0
@.str.3 = [9 x i8] c"parent:\0A\00"
@.str.4 = [15 x i8] c" in parent %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i8]
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
%6 = call i64 (...) @fork()
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %0
%10 = load i32, i32* %2
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 2)
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
br label %42
24: ; preds = %0
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %25)
store i32 %26, i32* %2
%27 = call i32 @wait(i32* %5)
%28 = load i32, i32* %2
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 2)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %24, %9
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@.str = [8 x i8] c"child:\0A\00"
@.str.1 = [14 x i8] c" in child %c\0A\00"
@.str.2 = [7 x i8] c"./test\00"
@O_RDONLY = global i32 0
@.str.4 = [15 x i8] c" in parent %c\0A\00"
@str = [8 x i8] c"parent:\00"
define i32 @main() {
%1 = alloca [2 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %0
%8 = call i32 @read(i32 undef, i8* %3, i32 2)
%9 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = load i8, i8* %3
%11 = sext i8 %10 to i32
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = tail call i32 @close(i32 undef)
br label %32
18: ; preds = %0
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i32 @wait(i32* %2)
%22 = call i32 @read(i32 %20, i8* %3, i32 2)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%24 = load i8, i8* %3
%25 = sext i8 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = call i32 @close(i32 %20)
br label %32
32: ; preds = %18, %7
ret i32 0
}