#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*/ O_RDONLY;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int vfork();
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd;
  char buf[2];
  fd = open("./test", O_RDONLY);
  switch (vfork()) {
  case -1:
    perror("vfork");
    exit(1);
  case 0:
    read(fd, buf, 2);
    printf("CHILD: \n");
    printf(" in child %c\n", buf[0]);
    printf(" in child %c\n", buf[1]);
    close(fd);
    close(STDOUT_FILENO);
    break;
  default:
    printf("PARENT:\n");
    read(fd, buf, 2);
    printf(" in parent %c\n", buf[0]);
    printf(" in parent %c\n", buf[1]);
    write(STDOUT_FILENO, "Parent executing\n", 17);
    break;
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
call   1090 <vfork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11cf <main+0x4f>
jmp    11bf <main+0x3f>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11e5 <main+0x65>
jmp    1243 <main+0xc3>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0xa(%rbp),%esi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x9(%rbp),%esi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
jmp    12a0 <main+0x120>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
movsbl -0xa(%rbp),%esi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x9(%rbp),%esi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xda6(%rip),%rsi
mov    $0x11,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
call   10a0 <vfork@plt>
test   %eax,%eax
je     11d3 <main+0x43>
cmp    $0xffffffff,%eax
jne    1233 <main+0xa3>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
mov    %ebp,%edi
call   1070 <read@plt>
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
movsbl 0xe(%rsp),%esi
lea    0xe11(%rip),%r14
xor    %eax,%eax
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <printf@plt>
movsbl 0xf(%rsp),%esi
mov    %r14,%rdi
mov    %ebx,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
mov    %ebp,%edi
call   1070 <read@plt>
movsbl 0xe(%rsp),%esi
lea    0xdbf(%rip),%rbp
xor    %eax,%eax
mov    %eax,%ebx
mov    %rbp,%rdi
call   1050 <printf@plt>
movsbl 0xf(%rsp),%esi
mov    %rbp,%rdi
mov    %ebx,%eax
call   1050 <printf@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
mov    $0x11,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"./test\00"
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"vfork\00"
@.str.2 = [9 x i8] c"CHILD: \0A\00"
@.str.3 = [14 x i8] c" in child %c\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [9 x i8] c"PARENT:\0A\00"
@.str.5 = [15 x i8] c" in parent %c\0A\00"
@.str.6 = [18 x i8] c"Parent executing\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i8]
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = call i32 @vfork()
switch i32 %6, label %27 [
i32 -1, label %7
i32 0, label %10
]
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* %2
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 2)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @close(i32 %25)
br label %42
27: ; preds = %0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%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 = 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.5, 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.5, i64 0, i64 0), i32 %38)
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @write(i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 17)
br label %42
42: ; preds = %27, %10
%43 = call i32 @exit(i32 0)
unreachable
}

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