#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int fread(char *, int, int, 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();
  }
  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();
  }
  char buf[4];
  int count;
  while ((count = fread(buf, 1, 3, read)) != 0) {
    buf[count] = '\0';
    printf("%s (%d chars)\n", buf, count);
  }
  fclose(read);
  unlink(name);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%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   10a0 <tmpnam@plt>
mov    %rax,%rdi
lea    0xe36(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f3 <main+0x73>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rsi
lea    0xe19(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1240 <main+0xc0>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1245 <main+0xc5>
lea    -0x2c(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1050 <fread@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     128b <main+0x10b>
movslq -0x30(%rbp),%rax
movb   $0x0,-0x2c(%rbp,%rax,1)
lea    -0x2c(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1245 <main+0xc5>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%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   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
call   10b0 <tmpnam@plt>
lea    0xe38(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     128d <main+0xfd>
mov    %rax,%rbx
lea    0xe1f(%rip),%rdi
mov    $0x25,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
lea    0xe24(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     128d <main+0xfd>
mov    %rax,%r12
lea    -0x24(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rax,%rcx
call   1060 <fread@plt>
test   %eax,%eax
je     126e <main+0xde>
lea    0xdf1(%rip),%r15
lea    -0x24(%rbp),%rbx
nopl   (%rax)
movslq %eax,%rdx
movb   $0x0,-0x24(%rbp,%rdx,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %r12,%rcx
call   1060 <fread@plt>
test   %eax,%eax
jne    1240 <main+0xb0>
mov    %r12,%rdi
call   1070 <fclose@plt>
mov    %r14,%rdi
call   1040 <unlink@plt>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda5(%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 = [38 x i8] c"a asd a xdfasdf abn asdfasdf asdfdfaa\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [15 x i8] c"%s (%d chars)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [4 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @L_tmpnam
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %2
%11 = alloca i8, i64 %9
store i64 %9, i64* %3
%12 = call i8* @tmpnam(i8* %11)
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %0
%20 = load i32*, i32** %4
%21 = call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32* %20)
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
%24 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %24, i32** %5
%25 = load i32*, i32** %5
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %19
br label %31
31: ; preds = %36, %30
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%33 = load i32*, i32** %5
%34 = call i32 @fread(i8* %32, i32 1, i32 3, i32* %33)
store i32 %34, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %31
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%41 = load i32, i32* %7
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %40, i32 %41)
br label %31
43: ; preds = %31
%44 = load i32*, i32** %5
%45 = call i32 @fclose(i32* %44)
%46 = call i32 @unlink(i8* %11)
%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 = [15 x i8] c"%s (%d chars)\0A\00"
@str.5 = [20 x i8] c"Failed to open file\00"
define i32 @main() {
%1 = alloca [4 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.5, i64 0, i64 0))
%10 = call i32 (...) @abort()
unreachable
11: ; preds = %0
%12 = call i64 @fwrite(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i64 37, i64 1, i32* %6)
%13 = call i32 @fclose(i32* %6)
%14 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %11
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%21 = call i32 @fread(i8* %20, i32 1, i32 3, i32* %14)
%22 = icmp eq i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %19, %23
%24 = phi i32 [ %28, %23 ], [ %21, %19 ]
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %20, i32 %24)
%28 = call i32 @fread(i8* %20, i32 1, i32 3, i32* %14)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %23
30: ; preds = %23, %19
%31 = call i32 @fclose(i32* %14)
%32 = call i32 @unlink(i8* %4)
ret i32 0
}