#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*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
char *tmpnam(char *);
int /*<<< orphan*/ unlink(char *);
int main() {
  char name[L_tmpnam];
  FILE *file = fopen(tmpnam(name), "w");
  if (file == NULL) {
    printf("Failed to open file\n");
    abort();
  }
  fputs("a asd a xdfasdf abn asdfasdf asdfdfaa", file);
  fclose(file);
  FILE *read = fopen(name, "r");
  if (read == NULL) {
    printf("Failed to open file\n");
    abort();
  }
  while (1) {
    int c = fgetc(read);
    if (feof(read)) {
      break;
    }
    putchar(c);
  }
  putchar('\n');
  fclose(read);
  unlink(name);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
call   10c0 <tmpnam@plt>
mov    %rax,%rdi
lea    0xe16(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1211 <main+0x71>
lea    0xdfd(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x20(%rbp),%rsi
lea    0xdfb(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rdi
lea    0xe08(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    125c <main+0xbc>
lea    0xdb2(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    1261 <main+0xc1>
jmp    1266 <main+0xc6>
mov    -0x28(%rbp),%rdi
call   1090 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%rax
je     128a <main+0xea>
jmp    129a <main+0xfa>
mov    -0x2c(%rbp),%eax
movsbl %al,%edi
call   1030 <putchar@plt>
jmp    1261 <main+0xc1>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1050 <unlink@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
call   10c0 <tmpnam@plt>
lea    0xe2d(%rip),%rsi
mov    %rax,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1268 <main+0xc8>
mov    %rax,%rbx
lea    0xe14(%rip),%rdi
mov    $0x25,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
lea    0xe19(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1268 <main+0xc8>
mov    %rax,%r15
nop
mov    %r15,%rdi
call   1080 <fgetc@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1090 <feof@plt>
test   %rax,%rax
jne    1241 <main+0xa1>
movsbl %bl,%edi
call   1030 <putchar@plt>
jmp    1220 <main+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    %r14,%rdi
call   1050 <unlink@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbb(%rip),%rdi
call   1060 <puts@plt>
xor    %eax,%eax
call   1040 <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 = [38 x i8] c"a asd a xdfasdf abn asdfasdf asdfdfaa\00"
@.str.3 = [2 x i8] c"r\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @L_tmpnam
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %2
%10 = alloca i8, i64 %8
store i64 %8, i64* %3
%11 = call i8* @tmpnam(i8* %10)
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %0
%19 = load i32*, i32** %4
%20 = call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32* %19)
%21 = load i32*, i32** %4
%22 = call i32 @fclose(i32* %21)
%23 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %23, i32** %5
%24 = load i32*, i32** %5
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %18
br label %30
30: ; preds = %38, %29
br label %31
31: ; preds = %30
%32 = load i32*, i32** %5
%33 = call i32 @fgetc(i32* %32)
store i32 %33, i32* %6
%34 = load i32*, i32** %5
%35 = call i64 @feof(i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
br label %42
38: ; preds = %31
%39 = load i32, i32* %6
%40 = trunc i32 %39 to i8
%41 = call i32 @putchar(i8 signext %40)
br label %30
42: ; preds = %37
%43 = call i32 @putchar(i8 signext 10)
%44 = load i32*, i32** %5
%45 = call i32 @fclose(i32* %44)
%46 = call i32 @unlink(i8* %10)
%47 = load i8*, i8** %2
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@L_tmpnam = global i32 0
@.str = [2 x i8] c"w\00"
@.str.2 = [38 x i8] c"a asd a xdfasdf abn asdfasdf asdfdfaa\00"
@.str.3 = [2 x i8] c"r\00"
@str.4 = [20 x i8] c"Failed to open file\00"
define i32 @main() {
%1 = load i32, i32* @L_tmpnam
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = call i8* @tmpnam(i8* %3)
%5 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%9 = call i32 (...) @abort()
unreachable
10: ; preds = %0
%11 = call i64 @fwrite(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i64 37, i64 1, i32* %5)
%12 = call i32 @fclose(i32* %5)
%13 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = call i32 @fgetc(i32* %13)
%17 = call i64 @feof(i32* %13)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %29
19: ; preds = %10
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%21 = call i32 (...) @abort()
unreachable
22: ; preds = %15, %22
%23 = phi i32 [ %26, %22 ], [ %16, %15 ]
%24 = trunc i32 %23 to i8
%25 = call i32 @putchar(i8 signext %24)
%26 = call i32 @fgetc(i32* %13)
%27 = call i64 @feof(i32* %13)
%28 = icmp eq i64 %27, 0
br i1 %28, label %22, label %29
29: ; preds = %22, %15
%30 = call i32 @putchar(i8 signext 10)
%31 = call i32 @fclose(i32* %13)
%32 = call i32 @unlink(i8* %3)
ret i32 0
}