#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 */
scalar_t__ getpid();
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *tmpnam(char *);
char *my_tmpnam(char *base) {
  char *tmp;
  char *result;
  tmp = tmpnam((char *)NULL);
  if (tmp == NULL) {
    char str[40];
    sprintf(str, "/tmp/%s.%d", base, (int)getpid());
    result = (char *)malloc((strlen(str) + 1) * sizeof(char));
    strcpy(result, str);
    return result;
  }
  result = (char *)malloc((strlen(tmp) + 1) * sizeof(char));
  strcpy(result, tmp);
  return result;
}


x86 assembly (clang, -O0):
<my_tmpnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <tmpnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <my_tmpnam+0x88>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    %eax,%ecx
lea    0xe54(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121a <my_tmpnam+0xba>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_tmpnam>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
xor    %edi,%edi
call   1080 <tmpnam@plt>
test   %rax,%rax
je     117b <my_tmpnam+0x1b>
mov    %rax,%rbx
jmp    1199 <my_tmpnam+0x39>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe77(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %eax,%ecx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/tmp/%s.%d\00"
define i8* @my_tmpnam(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [40 x i8]
store i8* %0, i8** %3
%7 = call i8* @tmpnam(i8* null)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %28
10: ; preds = %1
%11 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = call i64 (...) @getpid()
%14 = trunc i64 %13 to i32
%15 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %14)
%16 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 1
%21 = trunc i64 %20 to i32
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = getelementptr inbounds [40 x i8], [40 x i8]* %6, i64 0, i64 0
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = load i8*, i8** %5
store i8* %27, i8** %2
br label %41
28: ; preds = %1
%29 = load i8*, i8** %4
%30 = call i32 @strlen(i8* %29)
%31 = add nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = mul i64 %32, 1
%34 = trunc i64 %33 to i32
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %5
%37 = load i8*, i8** %5
%38 = load i8*, i8** %4
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %5
store i8* %40, i8** %2
br label %41
41: ; preds = %28, %10
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
@.str = [11 x i8] c"/tmp/%s.%d\00"
define i8* @my_tmpnam(i8* %0) {
%2 = alloca [40 x i8]
%3 = tail call i8* @tmpnam(i8* null)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %15
5: ; preds = %1
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%7 = tail call i64 (...) @getpid()
%8 = trunc i64 %7 to i32
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %8)
%10 = call i32 @strlen(i8* %6)
%11 = add nsw i32 %10, 1
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = call i32 @strcpy(i8* %13, i8* %6)
br label %21
15: ; preds = %1
%16 = tail call i32 @strlen(i8* %3)
%17 = add nsw i32 %16, 1
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 @strcpy(i8* %19, i8* %3)
br label %21
21: ; preds = %15, %5
%22 = phi i8* [ %13, %5 ], [ %19, %15 ]
ret i8* %22
}