#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*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ print(int /*<<< orphan*/ *, int);
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();
  }
  if (fseek(read, -5, SEEK_END) != 0) {
    abort();
  }
  print(read, 5);
  if (fseek(read, 0, SEEK_CUR) != 0) {
    abort();
  }
  print(read, 5);
  printf("%ld\n", ftell(read));
  fclose(read);
  unlink(name);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e21(%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,-0x28(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x10(%rbp)
call   10c0 <tmpnam@plt>
mov    %rax,%rdi
lea    0xe1d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120c <main+0x6c>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rsi
lea    0xe00(%rip),%rdi
call   1070 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1259 <main+0xb9>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffb,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1281 <main+0xe1>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rdi
mov    $0x5,%esi
call   1080 <print@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     12b4 <main+0x114>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rdi
mov    $0x5,%esi
call   1080 <print@plt>
mov    -0x20(%rbp),%rdi
call   1090 <ftell@plt>
mov    %eax,%esi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <unlink@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   %r14
push   %rbx
mov    0x2e12(%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    0xe20(%rip),%rsi
mov    %rax,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b0 <main+0x100>
mov    %rax,%rbx
lea    0xe07(%rip),%rdi
mov    $0x25,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xe0c(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b0 <main+0x100>
mov    %rax,%rbx
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xfffffffb,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
jne    12bc <main+0x10c>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1080 <print@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
jne    12bc <main+0x10c>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1080 <print@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
lea    0xd9e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1040 <unlink@plt>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd78(%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"
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@.str.4 = [5 x i8] c"%ld\0A\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i32*
%5 = load i32, i32* @L_tmpnam
%6 = zext i32 %5 to i64
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %1
%8 = alloca i8, i64 %6
store i64 %6, i64* %2
%9 = call i8* @tmpnam(i8* %8)
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 (...) @abort()
unreachable
16: ; preds = %0
%17 = load i32*, i32** %3
%18 = call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32* %17)
%19 = load i32*, i32** %3
%20 = call i32 @fclose(i32* %19)
%21 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %21, i32** %4
%22 = load i32*, i32** %4
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %16
%28 = load i32*, i32** %4
%29 = load i32, i32* @SEEK_END
%30 = call i64 @fseek(i32* %28, i32 -5, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 (...) @abort()
unreachable
34: ; preds = %27
%35 = load i32*, i32** %4
%36 = call i32 @print(i32* %35, i32 5)
%37 = load i32*, i32** %4
%38 = load i32, i32* @SEEK_CUR
%39 = call i64 @fseek(i32* %37, i32 0, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 (...) @abort()
unreachable
43: ; preds = %34
%44 = load i32*, i32** %4
%45 = call i32 @print(i32* %44, i32 5)
%46 = load i32*, i32** %4
%47 = call i32 @ftell(i32* %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i32*, i32** %4
%50 = call i32 @fclose(i32* %49)
%51 = call i32 @unlink(i8* %8)
%52 = load i8*, i8** %1
ret i32 0
}

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"
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@.str.4 = [5 x i8] c"%ld\0A\00"
@str.5 = [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.5, 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 %15, label %18
15: ; preds = %10
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %10
%19 = load i32, i32* @SEEK_END
%20 = call i64 @fseek(i32* %13, i32 -5, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %18
%25 = call i32 @print(i32* %13, i32 5)
%26 = load i32, i32* @SEEK_CUR
%27 = call i64 @fseek(i32* %13, i32 0, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %24
%32 = call i32 @print(i32* %13, i32 5)
%33 = call i32 @ftell(i32* %13)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = call i32 @fclose(i32* %13)
%36 = call i32 @unlink(i8* %3)
ret i32 0
}