#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__ ssize_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ stderr;
int strlen(char const *);
scalar_t__ write(int, void const *, size_t);
int main(int argc, char const *argv[]) {
  const char *data = "Somebody had to put all of this confusion here!\n";
  size_t len = strlen(data) * sizeof(char);
  int fh = open("do-not-exist", O_RDWR);
  pid_t pid = fork();
  if (pid != 0) {
    int fh = open("tmp_posix_test", O_CREAT | O_RDWR,
                  S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
    if (fh < 0) {
      fprintf(stderr, "Error while opening 'tmp_posix_test'\n");
      exit(1);
    }
    ssize_t bytesWritten = write(fh, (const void *)data, len);
    if (bytesWritten < 0) {
      fprintf(stderr, "Error while writing to 'tmp_posix_test'\n");
      exit(1);
    }
    int ret = close(fh);
    if (ret != 0) {
      fprintf(stderr, "Error while closing file 'tmp_posix_test'\n");
    }
  } else {
    int ret = close(4855);
  }
  printf("Hello from %lld\n", (long long int)pid);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12c1 <main+0x141>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%rax
or     (%rax),%esi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
mov    0x2dba(%rip),%rax
or     (%rax),%edx
mov    0x2da9(%rip),%rax
or     (%rax),%edx
mov    0x2dc0(%rip),%rax
or     (%rax),%edx
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1254 <main+0xd4>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    1292 <main+0x112>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     12bc <main+0x13c>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12ce <main+0x14e>
mov    $0x12f7,%edi
call   1060 <close@plt>
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xdee(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe75(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    0x2e46(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe8d(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1236 <main+0xb6>
mov    0x2e37(%rip),%rax
mov    0x0(%rbp),%esi
or     (%rax),%esi
mov    0x2de3(%rip),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd9(%rip),%rax
or     (%rax),%edx
mov    0x2df0(%rip),%rax
or     (%rax),%edx
lea    0xe4d(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1256 <main+0xd6>
mov    %eax,%ebp
movslq %r14d,%rdx
lea    0xdf8(%rip),%rsi
mov    %eax,%edi
call   1030 <write@plt>
test   %rax,%rax
js     1268 <main+0xe8>
mov    %ebp,%edi
call   1060 <close@plt>
test   %eax,%eax
je     1240 <main+0xc0>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1240 <main+0xc0>
mov    $0x12f7,%edi
call   1060 <close@plt>
lea    0xe80(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
jmp    1278 <main+0xf8>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"Somebody had to put all of this confusion here!\0A\00"
@.str.1 = [13 x i8] c"do-not-exist\00"
@O_RDWR = global i32 0
@.str.2 = [15 x i8] c"tmp_posix_test\00"
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str.3 = [38 x i8] c"Error while opening 'tmp_posix_test'\0A\00"
@.str.4 = [41 x i8] c"Error while writing to 'tmp_posix_test'\0A\00"
@.str.5 = [43 x i8] c"Error while closing file 'tmp_posix_test'\0A\00"
@.str.6 = [17 x i8] c"Hello from %lld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8** %6
%14 = load i8*, i8** %6
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 1
store i64 %17, i64* %7
%18 = load i32, i32* @O_RDWR
%19 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %8
%20 = call i64 (...) @fork()
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %61
23: ; preds = %2
%24 = load i32, i32* @O_CREAT
%25 = load i32, i32* @O_RDWR
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IRGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IROTH
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %31, %32
%34 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %23
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %23
%42 = load i32, i32* %10
%43 = load i8*, i8** %6
%44 = load i64, i64* %7
%45 = call i64 @write(i32 %42, i8* %43, i64 %44)
store i64 %45, i64* %11
%46 = load i64, i64* %11
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %41
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %41
%53 = load i32, i32* %10
%54 = call i32 @close(i32 %53)
store i32 %54, i32* %12
%55 = load i32, i32* %12
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
br label %60
60: ; preds = %57, %52
br label %63
61: ; preds = %2
%62 = call i32 @close(i32 4855)
store i32 %62, i32* %13
br label %63
63: ; preds = %61, %60
%64 = load i64, i64* %9
%65 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i64 %64)
ret i32 0
}

LLVM IR (-O2):
@.str = [49 x i8] c"Somebody had to put all of this confusion here!\0A\00"
@.str.1 = [13 x i8] c"do-not-exist\00"
@O_RDWR = global i32 0
@.str.2 = [15 x i8] c"tmp_posix_test\00"
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str.3 = [38 x i8] c"Error while opening 'tmp_posix_test'\0A\00"
@.str.4 = [41 x i8] c"Error while writing to 'tmp_posix_test'\0A\00"
@.str.5 = [43 x i8] c"Error while closing file 'tmp_posix_test'\0A\00"
@.str.6 = [17 x i8] c"Hello from %lld\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%4 = sext i32 %3 to i64
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %39, label %9
9: ; preds = %2
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %14, %13
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %9
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %9
%27 = tail call i64 @write(i32 %20, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i64 %4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = tail call i32 @close(i32 %20)
%35 = icmp eq i32 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
br label %41
39: ; preds = %2
%40 = tail call i32 @close(i32 4855)
br label %41
41: ; preds = %33, %36, %39
%42 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i64 %7)
ret i32 0
}