#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int L_tmpnam;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ tmpnam(char *);
int /*<<< orphan*/ unlink(char *);
int main() {
  char name[L_tmpnam];
  char buffer[500];
  tmpnam(name);
  FILE *writeTo = fopen(name, "w");
  if (writeTo == NULL) {
    printf("Failed to open file\n");
    abort();
  }
  fputs("hello world!", writeTo);
  fclose(writeTo);
  FILE *readFrom = fopen(name, "r");
  if (readFrom == NULL) {
    printf("Failed to open file\n");
    abort();
  }
  fgets(buffer, 500, readFrom);
  fclose(readFrom);
  unlink(name);
  puts(buffer);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x8(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x228(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x10(%rbp)
call   10b0 <tmpnam@plt>
mov    -0x228(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    120a <main+0x7a>
lea    0xe04(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x218(%rbp),%rsi
lea    0xdff(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x218(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x228(%rbp),%rdi
lea    0xded(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    1264 <main+0xd4>
lea    0xdaa(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
lea    -0x210(%rbp),%rdi
mov    -0x220(%rbp),%rdx
mov    $0x1f4,%esi
call   1090 <fgets@plt>
mov    -0x220(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x228(%rbp),%rdi
call   1040 <unlink@plt>
lea    -0x210(%rbp),%rdi
call   1050 <puts@plt>
mov    -0x8(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1f8,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
call   10a0 <tmpnam@plt>
lea    0xe47(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     123d <main+0xbd>
mov    %rax,%r14
lea    0xe32(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
lea    0xe1e(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     123d <main+0xbd>
mov    %rax,%rbx
lea    -0x210(%rbp),%r14
mov    %r14,%rdi
mov    $0x1f4,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r15,%rdi
call   1040 <unlink@plt>
mov    %r14,%rdi
call   1050 <puts@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcd(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@L_tmpnam = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [21 x i8] c"Failed to open file\0A\00"
@.str.2 = [13 x i8] c"hello world!\00"
@.str.3 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca [500 x i8]
%4 = alloca i32*
%5 = alloca i32*
%6 = load i32, i32* @L_tmpnam
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %1
%9 = alloca i8, i64 %7
store i64 %7, i64* %2
%10 = call i32 @tmpnam(i8* %9)
%11 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %0
%18 = load i32*, i32** %4
%19 = call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %18)
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
%22 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %17
%29 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%30 = load i32*, i32** %5
%31 = call i32 @fgets(i8* %29, i32 500, i32* %30)
%32 = load i32*, i32** %5
%33 = call i32 @fclose(i32* %32)
%34 = call i32 @unlink(i8* %9)
%35 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%36 = call i32 @puts(i8* %35)
%37 = load i8*, i8** %1
ret i32 0
}

LLVM IR (-O2):
@L_tmpnam = global i32 0
@.str = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"hello world!\00"
@.str.3 = [2 x i8] c"r\00"
@str.4 = [20 x i8] c"Failed to open file\00"
define i32 @main() {
%1 = alloca [500 x i8]
%2 = load i32, i32* @L_tmpnam
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%6 = call i32 @tmpnam(i8* %4)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%11 = call i32 (...) @abort()
unreachable
12: ; preds = %0
%13 = call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 12, i64 1, i32* %7)
%14 = call i32 @fclose(i32* %7)
%15 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%19 = call i32 (...) @abort()
unreachable
20: ; preds = %12
%21 = call i32 @fgets(i8* %5, i32 500, i32* %15)
%22 = call i32 @fclose(i32* %15)
%23 = call i32 @unlink(i8* %4)
%24 = call i32 @puts(i8* %5)
ret i32 0
}