#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_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd = open("test.txt", O_RDWR);
  int r;
  char buf[1024];
  if (fork()) {
    write(fd, "hello\n", 6);
    close(fd);
  } else {
    write(fd, "world\n", 6);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
cmp    $0x0,%rax
je     119c <main+0x5c>
mov    -0x8(%rbp),%edi
lea    0xe84(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    11b8 <main+0x78>
mov    -0x8(%rbp),%edi
lea    0xe6a(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
lea    0xea7(%rip),%rax
lea    0xe99(%rip),%rsi
cmove  %rax,%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"hello\0A\00"
@.str.2 = [7 x i8] c"world\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = call i64 (...) @fork()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* %2
%11 = call i32 @write(i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%12 = load i32, i32* %2
%13 = call i32 @close(i32 %12)
br label %19
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
br label %19
19: ; preds = %14, %9
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"hello\0A\00"
@.str.2 = [7 x i8] c"world\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
%5 = select i1 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%6 = tail call i32 @write(i32 %2, i8* %5, i32 6)
%7 = tail call i32 @close(i32 %2)
ret i32 0
}