#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int mkstemp(char *);
int /*<<< orphan*/ remove(char *);
size_t strlen(char const *);
char *tmp_file_path(const char *tmp_dir, const char *prefix) {
  char *file_path;
  size_t tmp_dir_len, prefix_len, total_len;
  int fd;
  tmp_dir_len = strlen(tmp_dir);
  prefix_len = strlen(prefix);
  total_len = tmp_dir_len + 1 + prefix_len + sizeof(".XXXXXX");
  file_path = (char *)malloc(total_len);
  if (file_path == NULL) {
    return NULL;
  }
  memcpy(file_path, tmp_dir, tmp_dir_len);
  file_path[tmp_dir_len] = '/';
  memcpy(file_path + tmp_dir_len + 1, prefix, prefix_len);
  memcpy(file_path + tmp_dir_len + 1 + prefix_len, ".XXXXXX",
         sizeof(".XXXXXX"));
  fd = mkstemp(file_path);
  if (fd == -1) {
    free(file_path);
    return NULL;
  }
  close(fd);
  remove(file_path);
  return file_path;
}


x86 assembly (clang, -O0):
<tmp_file_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
add    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d3 <tmp_file_path+0x63>
movq   $0x0,-0x8(%rbp)
jmp    1273 <tmp_file_path+0x103>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
add    -0x30(%rbp),%rdi
lea    0xddc(%rip),%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rdi
call   1090 <mkstemp@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    125a <tmp_file_path+0xea>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1273 <tmp_file_path+0x103>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tmp_file_path>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r13
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
lea    (%rbx,%rax,1),%rdi
add    $0x9,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     120f <tmp_file_path+0x9f>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1070 <memcpy@plt>
lea    (%r12,%rbx,1),%r13
movb   $0x2f,(%r12,%rbx,1)
lea    (%r12,%rbx,1),%rdi
add    $0x1,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1070 <memcpy@plt>
lea    (%r15,%r13,1),%rdi
add    $0x1,%rdi
lea    0xe21(%rip),%rsi
mov    $0x8,%edx
call   1070 <memcpy@plt>
mov    %r12,%rdi
call   1090 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1207 <tmp_file_path+0x97>
mov    %eax,%edi
call   1060 <close@plt>
mov    %r12,%rdi
call   1040 <remove@plt>
jmp    1212 <tmp_file_path+0xa2>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c".XXXXXX\00"
define i8* @tmp_file_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %7
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %8
%15 = load i64, i64* %7
%16 = add i64 %15, 1
%17 = load i64, i64* %8
%18 = add i64 %16, %17
%19 = add i64 %18, 8
store i64 %19, i64* %9
%20 = load i64, i64* %9
%21 = call i64 @malloc(i64 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %2
store i8* null, i8** %3
br label %63
26: ; preds = %2
%27 = load i8*, i8** %6
%28 = load i8*, i8** %4
%29 = load i64, i64* %7
%30 = trunc i64 %29 to i32
%31 = call i32 @memcpy(i8* %27, i8* %28, i32 %30)
%32 = load i8*, i8** %6
%33 = load i64, i64* %7
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 47, i8* %34
%35 = load i8*, i8** %6
%36 = load i64, i64* %7
%37 = getelementptr inbounds i8, i8* %35, i64 %36
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8*, i8** %5
%40 = load i64, i64* %8
%41 = trunc i64 %40 to i32
%42 = call i32 @memcpy(i8* %38, i8* %39, i32 %41)
%43 = load i8*, i8** %6
%44 = load i64, i64* %7
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = load i64, i64* %8
%48 = getelementptr inbounds i8, i8* %46, i64 %47
%49 = call i32 @memcpy(i8* %48, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 8)
%50 = load i8*, i8** %6
%51 = call i32 @mkstemp(i8* %50)
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %26
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
store i8* null, i8** %3
br label %63
57: ; preds = %26
%58 = load i32, i32* %10
%59 = call i32 @close(i32 %58)
%60 = load i8*, i8** %6
%61 = call i32 @remove(i8* %60)
%62 = load i8*, i8** %6
store i8* %62, i8** %3
br label %63
63: ; preds = %57, %54, %25
%64 = load i8*, i8** %3
ret i8* %64
}

LLVM IR (-O2):
@.str = [8 x i8] c".XXXXXX\00"
define i8* @tmp_file_path(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %3, 9
%6 = add i64 %5, %4
%7 = tail call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %26, label %10
10: ; preds = %2
%11 = trunc i64 %3 to i32
%12 = tail call i32 @memcpy(i8* %8, i8* %0, i32 %11)
%13 = getelementptr inbounds i8, i8* %8, i64 %3
store i8 47, i8* %13
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = trunc i64 %4 to i32
%16 = tail call i32 @memcpy(i8* %14, i8* %1, i32 %15)
%17 = getelementptr inbounds i8, i8* %14, i64 %4
%18 = tail call i32 @memcpy(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 8)
%19 = tail call i32 @mkstemp(i8* %8)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = tail call i32 @free(i8* %8)
br label %26
23: ; preds = %10
%24 = tail call i32 @close(i32 %19)
%25 = tail call i32 @remove(i8* %8)
br label %26
26: ; preds = %2, %23, %21
%27 = phi i8* [ null, %21 ], [ %8, %23 ], [ null, %2 ]
ret i8* %27
}