#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*/ *);
char *ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
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 asdfdf\n", file);
  fclose(file);
  FILE *read = fopen(name, "r");
  if (read == NULL) {
    printf("Failed to open file\n");
    abort();
  }
  char buf[20];
  while (fgets(buf, 20, read) != NULL) {
    printf("%s\n", buf);
  }
  rewind(read);
  while (fgets(buf, 20, read) != NULL) {
    printf("%s\n", buf);
  }
  printf("%ld\n", ftell(read));
  fclose(read);
  unlink(name);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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,-0x48(%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    1213 <main+0x73>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rsi
lea    0xdf9(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x48(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1260 <main+0xc0>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1265 <main+0xc5>
lea    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x14,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     1298 <main+0xf8>
lea    -0x40(%rbp),%rsi
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1265 <main+0xc5>
mov    -0x28(%rbp),%rdi
call   1070 <rewind@plt>
lea    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x14,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     12d4 <main+0x134>
lea    -0x40(%rbp),%rsi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12a1 <main+0x101>
mov    -0x28(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,%rsi
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x48(%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    $0x18,%rsp
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
call   10d0 <tmpnam@plt>
lea    0xe1a(%rip),%rsi
mov    %rax,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12e3 <main+0x133>
mov    %rax,%rbx
lea    0xe01(%rip),%rdi
mov    $0x24,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe05(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12e3 <main+0x133>
mov    %rax,%r15
lea    -0x30(%rbp),%rdi
mov    $0x14,%esi
mov    %rax,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     126d <main+0xbd>
lea    -0x30(%rbp),%rbx
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
mov    %r15,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
jne    1250 <main+0xa0>
mov    %r15,%rdi
call   1080 <rewind@plt>
lea    -0x30(%rbp),%rdi
mov    $0x14,%esi
mov    %r15,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     12ad <main+0xfd>
lea    -0x30(%rbp),%rbx
nop
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
mov    %r15,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
jne    1290 <main+0xe0>
mov    %r15,%rdi
call   10a0 <ftell@plt>
lea    0xd6d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
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    0xd44(%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 = [37 x i8] c"a asd a xdfasdf abn asdfasdf asdfdf\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [5 x i8] c"%ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [20 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 @fputs(i8* getelementptr inbounds ([37 x i8], [37 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 (i8*, ...) @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 = %35, %29
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%32 = load i32*, i32** %5
%33 = call i32* @fgets(i8* %31, i32 20, i32* %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %30
38: ; preds = %30
%39 = load i32*, i32** %5
%40 = call i32 @rewind(i32* %39)
br label %41
41: ; preds = %46, %38
%42 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%43 = load i32*, i32** %5
%44 = call i32* @fgets(i8* %42, i32 20, i32* %43)
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %47)
br label %41
49: ; preds = %41
%50 = load i32*, i32** %5
%51 = call i8* @ftell(i32* %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %51)
%53 = load i32*, i32** %5
%54 = call i32 @fclose(i32* %53)
%55 = call i32 @unlink(i8* %10)
%56 = load i8*, i8** %2
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@L_tmpnam = global i32 0
@.str = [2 x i8] c"w\00"
@.str.2 = [37 x i8] c"a asd a xdfasdf abn asdfasdf asdfdf\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.5 = [5 x i8] c"%ld\0A\00"
@str.6 = [20 x i8] c"Failed to open file\00"
define i32 @main() {
%1 = alloca [20 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.6, i64 0, i64 0))
%10 = call i32 (...) @abort()
unreachable
11: ; preds = %0
%12 = call i64 @fwrite(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i64 36, 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.6, i64 0, i64 0))
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %11
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%21 = call i32* @fgets(i8* %20, i32 20, i32* %14)
%22 = icmp eq i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %19, %23
%24 = call i32 @puts(i8* %20)
%25 = call i32* @fgets(i8* %20, i32 20, i32* %14)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %23
27: ; preds = %23, %19
%28 = call i32 @rewind(i32* %14)
%29 = call i32* @fgets(i8* %20, i32 20, i32* %14)
%30 = icmp eq i32* %29, null
br i1 %30, label %35, label %31
31: ; preds = %27, %31
%32 = call i32 @puts(i8* %20)
%33 = call i32* @fgets(i8* %20, i32 20, i32* %14)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %31
35: ; preds = %31, %27
%36 = call i8* @ftell(i32* %14)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %36)
%38 = call i32 @fclose(i32* %14)
%39 = call i32 @unlink(i8* %4)
ret i32 0
}