#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 char *fpos_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int EXIT_SUCCESS;
char *FILENAME_MAX;
char *L_tmpnam;
char *TMP_MAX;
char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetpos(int /*<<< orphan*/ *, char **);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int getchar();
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *tmpnam(int /*<<< orphan*/ *);
int /*<<< orphan*/ tranfile(int /*<<< orphan*/ *, char **);
int main() {
  char *filename;
  char *content;
  fpos_t curr;
  int c;
  filename = tmpnam(NULL);
  FILE *tmp;
  FILE *etc;
  printf("the max number of uniq tmpfile name we can create is: %ld\n",
         TMP_MAX);
  printf("the longest file name you can create is: %ld\n", FILENAME_MAX);
  printf("the longest tmp file name you can create is: %ld\n", L_tmpnam);
  printf("the max number of uniq tmpfile name we can create is: %ld\n",
         TMP_MAX);
  printf("file name allocated is :%s\n", filename);
  etc = fopen("/etc/passwd", "r");
  while ((c = fgetc(etc)) != EOF) {
    fputc(c, stderr);
    putchar(c);
    fflush(stdout);
  }
  tmp = fopen(filename, "w+b");
  content = calloc(1024, sizeof(char));
  memset(content, 'w', 1024);
  fwrite(content, 1024, 1, tmp);
  fgetpos(tmp, &curr);
  printf("current position is: %ld\n", curr);
  freopen("/etc/passwd", "r", tmp);
  tranfile(tmp, &curr);
  c = getchar();
  puts("close all file");
  fclose(tmp);
  fclose(etc);
  free(content);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1130 <tmpnam@plt>
mov    %rax,-0x10(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%rsi
lea    0xdc3(%rip),%rdi
call   1080 <printf@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%rsi
lea    0xde8(%rip),%rdi
call   1080 <printf@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%rsi
lea    0xe00(%rip),%rdi
call   1080 <printf@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%rsi
lea    0xd81(%rip),%rdi
call   1080 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe0c(%rip),%rdi
call   1080 <printf@plt>
lea    0xe1c(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10c0 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d2a(%rip),%rcx
cmp    (%rcx),%eax
je     12f2 <main+0xe2>
mov    -0x24(%rbp),%edi
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
call   10d0 <fputc@plt>
mov    -0x24(%rbp),%edi
call   1040 <putchar@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   1100 <fflush@plt>
jmp    12ab <main+0x9b>
mov    -0x10(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    $0x400,%edi
mov    $0x1,%esi
call   10e0 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x77,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x400,%esi
mov    $0x1,%edx
call   1120 <fwrite@plt>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <fgetpos@plt>
mov    -0x20(%rbp),%rsi
lea    0xd6e(%rip),%rdi
call   1080 <printf@plt>
mov    -0x30(%rbp),%rdx
lea    0xd4c(%rip),%rdi
lea    0xd51(%rip),%rsi
call   10b0 <freopen@plt>
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <tranfile@plt>
mov    $0x0,%al
call   10f0 <getchar@plt>
mov    %eax,-0x24(%rbp)
lea    0xd4e(%rip),%rdi
call   1050 <puts@plt>
mov    -0x30(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2bfc(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   1130 <tmpnam@plt>
mov    %rax,%r14
mov    0x2d84(%rip),%rbp
mov    0x0(%rbp),%rsi
lea    0xdc9(%rip),%rbx
mov    %rbx,%rdi
call   1080 <printf@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%rsi
lea    0xdeb(%rip),%rdi
call   1080 <printf@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%rsi
lea    0xe03(%rip),%rdi
call   1080 <printf@plt>
mov    0x0(%rbp),%rsi
mov    %rbx,%rdi
call   1080 <printf@plt>
lea    0xe1d(%rip),%rdi
mov    %r14,%rsi
call   1080 <printf@plt>
lea    0xe2a(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   1110 <fopen@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   10c0 <fgetc@plt>
mov    0x2d3d(%rip),%r15
cmp    (%r15),%eax
je     12e8 <main+0xd8>
mov    %eax,%ebp
mov    0x2d0f(%rip),%r13
mov    0x2d00(%rip),%rbx
mov    0x0(%r13),%esi
mov    %ebp,%edi
call   10d0 <fputc@plt>
mov    %ebp,%edi
call   1040 <putchar@plt>
mov    (%rbx),%edi
call   1100 <fflush@plt>
mov    %r12,%rdi
call   10c0 <fgetc@plt>
mov    %eax,%ebp
cmp    (%r15),%eax
jne    12c0 <main+0xb0>
lea    0xdd6(%rip),%rsi
mov    %r14,%rdi
call   1110 <fopen@plt>
mov    %rax,%r14
mov    $0x400,%edi
mov    $0x1,%esi
call   10e0 <calloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x77,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
mov    %r15,%rdi
mov    $0x400,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1120 <fwrite@plt>
mov    %rsp,%rbp
mov    %r14,%rdi
mov    %rbp,%rsi
call   1090 <fgetpos@plt>
mov    (%rsp),%rsi
lea    0xd7d(%rip),%rdi
call   1080 <printf@plt>
lea    0xd5f(%rip),%rdi
lea    0xd64(%rip),%rsi
mov    %r14,%rdx
call   10b0 <freopen@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <tranfile@plt>
xor    %eax,%eax
call   10f0 <getchar@plt>
lea    0xd63(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    %r12,%rdi
call   1070 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2c14(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [59 x i8] c"the max number of uniq tmpfile name we can create is: %ld\0A\00"
@TMP_MAX = global i8* null
@.str.1 = [46 x i8] c"the longest file name you can create is: %ld\0A\00"
@FILENAME_MAX = global i8* null
@.str.2 = [50 x i8] c"the longest tmp file name you can create is: %ld\0A\00"
@L_tmpnam = global i8* null
@.str.3 = [28 x i8] c"file name allocated is :%s\0A\00"
@.str.4 = [12 x i8] c"/etc/passwd\00"
@.str.5 = [2 x i8] c"r\00"
@EOF = global i32 0
@stderr = global i32 0
@stdout = global i32 0
@.str.6 = [4 x i8] c"w+b\00"
@.str.7 = [26 x i8] c"current position is: %ld\0A\00"
@.str.8 = [15 x i8] c"close all file\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
store i32 0, i32* %1
%8 = call i8* @tmpnam(i32* null)
store i8* %8, i8** %2
%9 = load i8*, i8** @TMP_MAX
%10 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** @FILENAME_MAX
%12 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = load i8*, i8** @L_tmpnam
%14 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** @TMP_MAX
%16 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %2
%18 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %19, i32** %7
br label %20
20: ; preds = %25, %0
%21 = load i32*, i32** %7
%22 = call i32 @fgetc(i32* %21)
store i32 %22, i32* %5
%23 = load i32, i32* @EOF
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i32, i32* %5
%27 = load i32, i32* @stderr
%28 = call i32 @fputc(i32 %26, i32 %27)
%29 = load i32, i32* %5
%30 = call i32 @putchar(i32 %29)
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
br label %20
33: ; preds = %20
%34 = load i8*, i8** %2
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
store i32* %35, i32** %6
%36 = call i8* @calloc(i32 1024, i32 1)
store i8* %36, i8** %3
%37 = load i8*, i8** %3
%38 = call i32 @memset(i8* %37, i8 signext 119, i32 1024)
%39 = load i8*, i8** %3
%40 = load i32*, i32** %6
%41 = call i32 @fwrite(i8* %39, i32 1024, i32 1, i32* %40)
%42 = load i32*, i32** %6
%43 = call i32 @fgetpos(i32* %42, i8** %4)
%44 = load i8*, i8** %4
%45 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %44)
%46 = load i32*, i32** %6
%47 = call i32 @freopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32* %46)
%48 = load i32*, i32** %6
%49 = call i32 @tranfile(i32* %48, i8** %4)
%50 = call i32 (...) @getchar()
store i32 %50, i32* %5
%51 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = load i32*, i32** %7
%55 = call i32 @fclose(i32* %54)
%56 = load i8*, i8** %3
%57 = call i32 @free(i8* %56)
%58 = load i32, i32* @EXIT_SUCCESS
ret i32 %58
}

LLVM IR (-O2):
@.str = [59 x i8] c"the max number of uniq tmpfile name we can create is: %ld\0A\00"
@TMP_MAX = global i8* null
@.str.1 = [46 x i8] c"the longest file name you can create is: %ld\0A\00"
@FILENAME_MAX = global i8* null
@.str.2 = [50 x i8] c"the longest tmp file name you can create is: %ld\0A\00"
@L_tmpnam = global i8* null
@.str.3 = [28 x i8] c"file name allocated is :%s\0A\00"
@.str.4 = [12 x i8] c"/etc/passwd\00"
@.str.5 = [2 x i8] c"r\00"
@EOF = global i32 0
@stderr = global i32 0
@stdout = global i32 0
@.str.6 = [4 x i8] c"w+b\00"
@.str.7 = [26 x i8] c"current position is: %ld\0A\00"
@.str.8 = [15 x i8] c"close all file\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call i8* @tmpnam(i32* null)
%4 = load i8*, i8** @TMP_MAX
%5 = tail call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = load i8*, i8** @FILENAME_MAX
%7 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = load i8*, i8** @L_tmpnam
%9 = tail call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** @TMP_MAX
%11 = tail call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%14 = tail call i32 @fgetc(i32* %13)
%15 = load i32, i32* @EOF
%16 = icmp eq i32 %14, %15
br i1 %16, label %27, label %17
17: ; preds = %0, %17
%18 = phi i32 [ %24, %17 ], [ %14, %0 ]
%19 = load i32, i32* @stderr
%20 = tail call i32 @fputc(i32 %18, i32 %19)
%21 = tail call i32 @putchar(i32 %18)
%22 = load i32, i32* @stdout
%23 = tail call i32 @fflush(i32 %22)
%24 = tail call i32 @fgetc(i32* %13)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %17
27: ; preds = %17, %0
%28 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%29 = tail call dereferenceable_or_null(1024) i8* @calloc(i32 1024, i32 1)
%30 = tail call i32 @memset(i8* %29, i8 signext 119, i32 1024)
%31 = tail call i32 @fwrite(i8* %29, i32 1024, i32 1, i32* %28)
%32 = call i32 @fgetpos(i32* %28, i8** %1)
%33 = load i8*, i8** %1
%34 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %33)
%35 = tail call i32 @freopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32* %28)
%36 = call i32 @tranfile(i32* %28, i8** %1)
%37 = call i32 (...) @getchar()
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%39 = call i32 @fclose(i32* %28)
%40 = call i32 @fclose(i32* %13)
%41 = call i32 @free(i8* %29)
%42 = load i32, i32* @EXIT_SUCCESS
ret i32 %42
}