#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 O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ err_sys(char *);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int fd;
  pid_t pid;
  fd = open("test.data", O_RDWR | O_CREAT | O_TRUNC, 0666);
  if (fd < 0)
    err_sys("open error");
  dup2(fd, 1);
  pid = fork();
  if (pid < 0)
    err_sys("fork error");
  else if (pid == 0) {
    printf("hello");
  } else {
    sleep(1);
    printf("world");
    wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
lea    0xe5f(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c4 <main+0x54>
lea    0xe4b(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11f8 <main+0x88>
lea    0xe27(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1238 <main+0xc8>
cmpq   $0x0,-0x10(%rbp)
jne    1214 <main+0xa4>
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
jmp    1233 <main+0xc3>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe01(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
jmp    1238 <main+0xc8>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e60(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
lea    0xe6d(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11af <main+0x3f>
lea    0xe60(%rip),%rdi
call   1030 <err_sys@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11e8 <main+0x78>
je     11f6 <main+0x86>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe4c(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <wait@plt>
jmp    1202 <main+0x92>
lea    0xe26(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1202 <main+0x92>
lea    0xe23(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"test.data\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [6 x i8] c"hello\00"
@.str.4 = [6 x i8] c"world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8, i32 438)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = load i32, i32* %2
%16 = call i32 @dup2(i32 %15, i32 1)
%17 = call i64 (...) @fork()
store i64 %17, i64* %3
%18 = load i64, i64* %3
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %32
22: ; preds = %14
%23 = load i64, i64* %3
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %31
27: ; preds = %22
%28 = call i32 @sleep(i32 1)
%29 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @wait(i32* null)
br label %31
31: ; preds = %27, %25
br label %32
32: ; preds = %31, %20
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"test.data\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [6 x i8] c"hello\00"
@.str.4 = [6 x i8] c"world\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5, i32 438)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = tail call i32 @dup2(i32 %6, i32 1)
%12 = tail call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %24
16: ; preds = %10
%17 = icmp eq i64 %12, 0
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %24
20: ; preds = %16
%21 = tail call i32 @sleep(i32 1)
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i32 @wait(i32* null)
br label %24
24: ; preds = %18, %20, %14
ret i32 0
}