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

/* Variables and functions */
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ *tmpfile();
void GenerateTmpFile() {
  char path_buf[255];
  char file_path[255];
  ssize_t rv = 0;
  FILE *fp = tmpfile();
  sprintf(path_buf, "/proc/self/fd/%d", fileno(fp));
  readlink(path_buf, file_path, 255);
  if (rv == -1) {
    printf("Failed to read tmp file path");
    return;
  }
  printf("Create a tmp file at %s", file_path);
}


x86 assembly (clang, -O0):
<GenerateTmpFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x204(%rbp)
mov    $0x0,%al
call   1050 <tmpfile@plt>
mov    %rax,-0x210(%rbp)
lea    -0x100(%rbp),%rax
mov    %rax,-0x218(%rbp)
mov    -0x210(%rbp),%rdi
call   1060 <fileno@plt>
mov    -0x218(%rbp),%rdi
mov    %eax,%edx
lea    0xe63(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x100(%rbp),%rdi
lea    -0x200(%rbp),%rsi
mov    $0xff,%edx
call   1030 <readlink@plt>
cmpl   $0xffffffff,-0x204(%rbp)
jne    11da <GenerateTmpFile+0x8a>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ef <GenerateTmpFile+0x9f>
lea    -0x200(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GenerateTmpFile>:
push   %r14
push   %rbx
sub    $0x208,%rsp
xor    %eax,%eax
call   1050 <tmpfile@plt>
mov    %rax,%rdi
call   1060 <fileno@plt>
lea    0xe90(%rip),%rsi
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    %rsp,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xff,%edx
call   1030 <readlink@plt>
lea    0xe75(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [29 x i8] c"Failed to read tmp file path\00"
@.str.2 = [24 x i8] c"Create a tmp file at %s\00"
define void @GenerateTmpFile() {
%1 = alloca [255 x i8]
%2 = alloca [255 x i8]
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %3
%5 = call i32* (...) @tmpfile()
store i32* %5, i32** %4
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%7 = load i32*, i32** %4
%8 = call i32 @fileno(i32* %7)
%9 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%12 = call i32 @readlink(i8* %10, i8* %11, i32 255)
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %20
17: ; preds = %0
%18 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %15
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.2 = [24 x i8] c"Create a tmp file at %s\00"
define void @GenerateTmpFile() {
%1 = alloca [255 x i8]
%2 = alloca [255 x i8]
%3 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%5 = tail call i32* (...) @tmpfile()
%6 = tail call i32 @fileno(i32* %5)
%7 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 @readlink(i8* %3, i8* %4, i32 255)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %4)
ret void
}