#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*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(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();
  }
  fputc('\n', file);
  fclose(file);
  FILE *read = fopen(name, "r");
  char buf[200];
  while (fgets(buf, 200, read) != NULL) {
    printf("%s\n", buf);
  }
  fclose(read);
  unlink(name);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e47(%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,-0xf8(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
call   10a0 <tmpnam@plt>
mov    %rax,%rdi
lea    0xe30(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f9 <main+0x79>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rsi
mov    $0xa,%edi
call   1070 <fputc@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0xf8(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
lea    -0xf0(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0xc8,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1260 <main+0xe0>
lea    -0xf0(%rbp),%rsi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1227 <main+0xa7>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0xf8(%rbp),%rdi
call   1040 <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
sub    $0xc8,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
call   10a0 <tmpnam@plt>
lea    0xe47(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     125a <main+0xda>
mov    %rax,%rbx
mov    $0xa,%edi
mov    %rax,%rsi
call   1070 <fputc@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe19(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%r15
lea    -0xe0(%rbp),%rdi
mov    $0xc8,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     123d <main+0xbd>
lea    -0xe0(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1220 <main+0xa0>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1040 <unlink@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda3(%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 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [200 x i8]
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 (i8*, ...) @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 @fputc(i8 signext 10, 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.2, i64 0, i64 0))
store i32* %23, i32** %5
br label %24
24: ; preds = %29, %18
%25 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%26 = load i32*, i32** %5
%27 = call i32* @fgets(i8* %25, i32 200, i32* %26)
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %24
32: ; preds = %24
%33 = load i32*, i32** %5
%34 = call i32 @fclose(i32* %33)
%35 = call i32 @unlink(i8* %10)
%36 = load i8*, i8** %2
%37 = load i32, i32* %1
ret i32 %37
}

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