#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ wait(int *);
int main() {
  int fd;
  char buf[2];
  pid_t cpid;
  int childstatus;
  cpid = fork();
  fd = open("./test", O_RDONLY);
  if (cpid == 0) {
    close(1);
    read(fd, buf, 2);
    printf("CHILD:\n");
    printf(" in child %c\n", buf[0]);
    printf(" in child %c\n", buf[1]);
    close(fd);
  } else {
    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);
  }
  exit(EXIT_SUCCESS);
}


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)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0xe66(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1207 <main+0x97>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0xa(%rbp),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x9(%rbp),%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    125b <main+0xeb>
lea    -0x1c(%rbp),%rdi
call   1080 <wait@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0xa(%rbp),%esi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x9(%rbp),%esi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %rbx,%rbx
jne    11de <main+0x5e>
mov    $0x1,%edi
call   1050 <close@plt>
lea    0x2(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1060 <read@plt>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
movsbl 0x2(%rsp),%esi
lea    0xe2b(%rip),%rbx
jmp    1211 <main+0x91>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
lea    0x2(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1060 <read@plt>
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
movsbl 0x2(%rsp),%esi
lea    0xe04(%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>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [7 x i8] c"./test\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c" in child %c\0A\00"
@.str.4 = [15 x i8] c" in parent %c\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [7 x i8] c"PARENT\00"
@str.5 = [7 x i8] c"CHILD:\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 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i64 %5, 0
br i1 %8, label %9, label %21
9: ; preds = %0
%10 = tail call i32 @close(i32 1)
%11 = call i32 @read(i32 %7, i8* %3, i32 2)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.5, i64 0, i64 0))
%13 = load i8, i8* %3
%14 = sext i8 %13 to i32
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = tail call i32 @close(i32 %7)
br label %33
21: ; preds = %0
%22 = call i32 @wait(i32* %2)
%23 = call i32 @read(i32 %7, i8* %3, i32 2)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%25 = load i8, i8* %3
%26 = sext i8 %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = call i32 @close(i32 %7)
br label %33
33: ; preds = %21, %9
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @exit(i32 %34)
unreachable
}