#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_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ *tmpnam(char *);
int main(int argc, char *argv[]) {
  int lose = 0;
  char filename[L_tmpnam];
  FILE *fp;
  if (tmpnam(filename) == NULL) {
    printf("tmpnam failed\n");
    lose = 1;
  } else {
    fp = fopen(filename, "w+");
    fprintf(fp, "Hello world!\n");
    fflush(fp);
    fseek(fp, 5L, SEEK_SET);
    if (fseek(fp, -1L, SEEK_CUR) < 0) {
      printf("seek failed\n");
      lose = 1;
    }
    fclose(fp);
    remove(filename);
  }
  {
    FILE *file1;
    FILE *file2;
    char filename1[L_tmpnam];
    char filename2[L_tmpnam];
    int ch;
    if (tmpnam(filename1) == NULL || tmpnam(filename2) == NULL) {
      printf("tmpnam failed\n");
      lose = 1;
    } else {
      file1 = fopen(filename1, "w");
      fclose(file1);
      file2 = fopen(filename2, "w");
      fputc('x', file2);
      fclose(file2);
      file1 = fopen(filename1, "r");
      file2 = freopen(filename2, "r", file1);
      if ((ch = fgetc(file2)) != 'x') {
        printf("wrong character in reopened file, value = %d\n", ch);
        lose = 1;
      }
      fclose(file2);
      remove(filename1);
      remove(filename2);
    }
  }
  puts(lose ? "Test FAILED!" : "Test succeeded.");
  return lose;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
call   10e0 <tmpnam@plt>
cmp    $0x0,%rax
jne    1235 <main+0x75>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    12c3 <main+0x103>
mov    -0x68(%rbp),%rdi
lea    0xdcf(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xdbe(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <fflush@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   10c0 <fseek@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffffffffffff,%rsi
call   10c0 <fseek@plt>
cmp    $0x0,%rax
jge    12b1 <main+0xf1>
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x68(%rbp),%rdi
call   1030 <remove@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x78(%rbp)
mov    %rdi,%rsp
mov    %rcx,-0x50(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
call   10e0 <tmpnam@plt>
cmp    $0x0,%rax
je     132f <main+0x16f>
mov    -0x70(%rbp),%rdi
call   10e0 <tmpnam@plt>
cmp    $0x0,%rax
jne    1349 <main+0x189>
lea    0xcca(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    1405 <main+0x245>
mov    -0x78(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x70(%rbp),%rdi
lea    0xcbc(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
mov    $0x78,%edi
call   1090 <fputc@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x78(%rbp),%rdi
lea    0xc93(%rip),%rsi
call   10d0 <fopen@plt>
mov    -0x70(%rbp),%rdi
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdx
lea    0xc7b(%rip),%rsi
call   1070 <freopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1080 <fgetc@plt>
mov    %eax,-0x5c(%rbp)
cmp    $0x78,%eax
je     13ea <main+0x22a>
mov    -0x5c(%rbp),%esi
lea    0xc55(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x78(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x70(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x14(%rbp),%ecx
lea    0xc56(%rip),%rdi
lea    0xc42(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdi
call   1040 <puts@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x20(%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   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e03(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   10e0 <tmpnam@plt>
test   %rax,%rax
je     1288 <main+0xc8>
lea    0xdfe(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
mov    %rax,%r15
lea    0xdef(%rip),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
mov    %r15,%rdi
call   10a0 <fflush@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
mov    %r15,%rdi
call   10b0 <fseek@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    $0xffffffffffffffff,%rsi
call   10b0 <fseek@plt>
xor    %r14d,%r14d
test   %rax,%rax
jns    1276 <main+0xb6>
lea    0xdf5(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1030 <remove@plt>
jmp    129a <main+0xda>
lea    0xddd(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %rsp,%r13
mov    (%r12),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r15,%rdi
call   10e0 <tmpnam@plt>
test   %rax,%rax
je     139e <main+0x1de>
mov    %rbx,%rdi
call   10e0 <tmpnam@plt>
test   %rax,%rax
je     139e <main+0x1de>
lea    0xd21(%rip),%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   10c0 <fopen@plt>
mov    %rax,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10c0 <fopen@plt>
mov    %rax,%r12
mov    $0x78,%edi
mov    %rax,%rsi
call   1090 <fputc@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
lea    0xce6(%rip),%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   10c0 <fopen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   1070 <freopen@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <fgetc@plt>
cmp    $0x78,%eax
je     136c <main+0x1ac>
lea    0xcb8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%r14d
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <remove@plt>
mov    %rbx,%rdi
call   1030 <remove@plt>
mov    %r13,%rsp
test   %r14d,%r14d
lea    0xcbf(%rip),%rax
lea    0xcab(%rip),%rdi
cmove  %rax,%rdi
jmp    13ba <main+0x1fa>
lea    0xcc7(%rip),%rdi
call   1040 <puts@plt>
lea    0xc92(%rip),%rdi
mov    $0x1,%r14d
mov    %r13,%rsp
call   1040 <puts@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@L_tmpnam = global i32 0
@.str = [15 x i8] c"tmpnam failed\0A\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [14 x i8] c"Hello world!\0A\00"
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@.str.3 = [13 x i8] c"seek failed\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [46 x i8] c"wrong character in reopened file, value = %d\0A\00"
@.str.7 = [13 x i8] c"Test FAILED!\00"
@.str.8 = [16 x i8] c"Test succeeded.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%16 = load i32, i32* @L_tmpnam
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %7
%19 = alloca i8, i64 %17
store i64 %17, i64* %8
%20 = call i32* @tmpnam(i8* %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %6
br label %43
24: ; preds = %2
%25 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %9
%26 = load i32*, i32** %9
%27 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32*, i32** %9
%29 = call i32 @fflush(i32* %28)
%30 = load i32*, i32** %9
%31 = load i32, i32* @SEEK_SET
%32 = call i64 @fseek(i32* %30, i64 5, i32 %31)
%33 = load i32*, i32** %9
%34 = load i32, i32* @SEEK_CUR
%35 = call i64 @fseek(i32* %33, i64 -1, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %24
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %6
br label %39
39: ; preds = %37, %24
%40 = load i32*, i32** %9
%41 = call i32 @fclose(i32* %40)
%42 = call i32 @remove(i8* %19)
br label %43
43: ; preds = %39, %22
%44 = load i32, i32* @L_tmpnam
%45 = zext i32 %44 to i64
%46 = call i8* @llvm.stacksave()
store i8* %46, i8** %12
%47 = alloca i8, i64 %45
store i64 %45, i64* %13
%48 = load i32, i32* @L_tmpnam
%49 = zext i32 %48 to i64
%50 = alloca i8, i64 %49
store i64 %49, i64* %14
%51 = call i32* @tmpnam(i8* %47)
%52 = icmp eq i32* %51, null
br i1 %52, label %56, label %53
53: ; preds = %43
%54 = call i32* @tmpnam(i8* %50)
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %58
56: ; preds = %53, %43
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %6
br label %81
58: ; preds = %53
%59 = call i32* @fopen(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %59, i32** %10
%60 = load i32*, i32** %10
%61 = call i32 @fclose(i32* %60)
%62 = call i32* @fopen(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %62, i32** %11
%63 = load i32*, i32** %11
%64 = call i32 @fputc(i8 signext 120, i32* %63)
%65 = load i32*, i32** %11
%66 = call i32 @fclose(i32* %65)
%67 = call i32* @fopen(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %67, i32** %10
%68 = load i32*, i32** %10
%69 = call i32* @freopen(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32* %68)
store i32* %69, i32** %11
%70 = load i32*, i32** %11
%71 = call i32 @fgetc(i32* %70)
store i32 %71, i32* %15
%72 = icmp ne i32 %71, 120
br i1 %72, label %73, label %76
73: ; preds = %58
%74 = load i32, i32* %15
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i32 %74)
store i32 1, i32* %6
br label %76
76: ; preds = %73, %58
%77 = load i32*, i32** %11
%78 = call i32 @fclose(i32* %77)
%79 = call i32 @remove(i8* %47)
%80 = call i32 @remove(i8* %50)
br label %81
81: ; preds = %76, %56
%82 = load i8*, i8** %12
%83 = load i32, i32* %6
%84 = icmp ne i32 %83, 0
%85 = zext i1 %84 to i64
%86 = select i1 %84, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0)
%87 = call i32 @puts(i8* %86)
%88 = load i32, i32* %6
store i32 %88, i32* %3
%89 = load i8*, i8** %7
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
@L_tmpnam = global i32 0
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [14 x i8] c"Hello world!\0A\00"
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [46 x i8] c"wrong character in reopened file, value = %d\0A\00"
@.str.7 = [13 x i8] c"Test FAILED!\00"
@.str.8 = [16 x i8] c"Test succeeded.\00"
@str.9 = [12 x i8] c"seek failed\00"
@str.10 = [14 x i8] c"tmpnam failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @L_tmpnam
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32* @tmpnam(i8* %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
br label %25
10: ; preds = %2
%11 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = call i64 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i64 13, i64 1, i32* %11)
%13 = call i32 @fflush(i32* %11)
%14 = load i32, i32* @SEEK_SET
%15 = call i64 @fseek(i32* %11, i64 5, i32 %14)
%16 = load i32, i32* @SEEK_CUR
%17 = call i64 @fseek(i32* %11, i64 -1, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0))
br label %21
21: ; preds = %19, %10
%22 = phi i32 [ 1, %19 ], [ 0, %10 ]
%23 = call i32 @fclose(i32* %11)
%24 = call i32 @remove(i8* %5)
br label %25
25: ; preds = %21, %8
%26 = phi i32 [ 1, %8 ], [ %22, %21 ]
%27 = load i32, i32* @L_tmpnam
%28 = zext i32 %27 to i64
%29 = call i8* @llvm.stacksave()
%30 = alloca i8, i64 %28
%31 = load i32, i32* @L_tmpnam
%32 = zext i32 %31 to i64
%33 = alloca i8, i64 %32
%34 = call i32* @tmpnam(i8* %30)
%35 = icmp eq i32* %34, null
br i1 %35, label %39, label %36
36: ; preds = %25
%37 = call i32* @tmpnam(i8* %33)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %25, %36
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
br label %62
41: ; preds = %36
%42 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @fclose(i32* %42)
%44 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @fputc(i8 signext 120, i32* %44)
%46 = call i32 @fclose(i32* %44)
%47 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32* @freopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32* %47)
%49 = call i32 @fgetc(i32* %48)
%50 = icmp eq i32 %49, 120
br i1 %50, label %53, label %51
51: ; preds = %41
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i32 %49)
br label %53
53: ; preds = %41, %51
%54 = phi i32 [ 1, %51 ], [ %26, %41 ]
%55 = call i32 @fclose(i32* %48)
%56 = call i32 @remove(i8* %30)
%57 = call i32 @remove(i8* %33)
%58 = icmp eq i32 %54, 0
%59 = xor i1 %58, true
%60 = zext i1 %59 to i32
%61 = select i1 %58, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0)
br label %62
62: ; preds = %53, %39
%63 = phi i32 [ 1, %39 ], [ %60, %53 ]
%64 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), %39 ], [ %61, %53 ]
%65 = call i32 @puts(i8* dereferenceable(1) %64)
ret i32 %63
}