#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*/ printf(char *, 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) {
    read(fd, buf, 2);
    printf(" child:  %c\n", buf[0]);
    printf(" child: %c\n", buf[1]);
    close(fd);
  } else {
    wait(&childstatus);
    read(fd, buf, 2);
    printf(" parent: %c\n", buf[0]);
    printf(" parent: %c\n", buf[1]);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11db <main+0x7b>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
lea    0xe52(%rip),%rdi
movsbl -0xa(%rbp),%esi
call   1030 <printf@plt>
lea    0xe4f(%rip),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    121d <main+0xbd>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
lea    0xe24(%rip),%rdi
movsbl -0xa(%rbp),%esi
call   1030 <printf@plt>
lea    0xe14(%rip),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %rbx,%rbx
je     11c2 <main+0x62>
lea    0x4(%rsp),%rdi
call   1070 <wait@plt>
lea    0x2(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <read@plt>
movsbl 0x2(%rsp),%esi
lea    0xe70(%rip),%rbx
mov    %rbx,%rdi
call   1030 <printf@plt>
movsbl 0x3(%rsp),%esi
mov    %rbx,%rdi
jmp    11f0 <main+0x90>
lea    0x2(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <read@plt>
movsbl 0x2(%rsp),%esi
lea    0xe28(%rip),%rdi
call   1030 <printf@plt>
movsbl 0x3(%rsp),%esi
lea    0xe24(%rip),%rdi
call   1030 <printf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"./test\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c" child: %c\0A\00"
@.str.2 = [12 x i8] c" child: %c\0A\00"
@.str.3 = [13 x i8] c" 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 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 %23
11: ; preds = %0
%12 = load i32, i32* %2
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 2)
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%16 = load i8, i8* %15
%17 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8 signext %16)
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%19 = load i8, i8* %18
%20 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8 signext %19)
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
br label %36
23: ; preds = %0
%24 = call i32 @wait(i32* %5)
%25 = load i32, i32* %2
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 2)
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%29 = load i8, i8* %28
%30 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8 signext %29)
%31 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%32 = load i8, i8* %31
%33 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8 signext %32)
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %23, %11
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"./test\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c" child: %c\0A\00"
@.str.2 = [12 x i8] c" child: %c\0A\00"
@.str.3 = [13 x i8] c" parent: %c\0A\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 %17
9: ; preds = %0
%10 = call i32 @read(i32 %7, i8* %3, i32 2)
%11 = load i8, i8* %3
%12 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8 signext %11)
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
%14 = load i8, i8* %13
%15 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8 signext %14)
%16 = tail call i32 @close(i32 %7)
br label %26
17: ; preds = %0
%18 = call i32 @wait(i32* %2)
%19 = call i32 @read(i32 %7, i8* %3, i32 2)
%20 = load i8, i8* %3
%21 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8 signext %20)
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
%23 = load i8, i8* %22
%24 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8 signext %23)
%25 = call i32 @close(i32 %7)
br label %26
26: ; preds = %17, %9
ret i32 0
}