#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 L_tmpnam;
char *malloc(scalar_t__);
char *strcpy(char *, char *);
scalar_t__ strlen(char *);
char *tmpnam(char *);
char *mktempname(void) {
  char tmp[L_tmpnam];
  char *result;
  result = tmpnam(tmp);
  if (!result)
    return NULL;
  result = malloc(strlen(tmp) + 1);
  if (!result)
    return NULL;
  return strcpy(result, tmp);
}


x86 assembly (clang, -O0):
<mktempname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
call   1060 <tmpnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <mktempname+0x5c>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11ec <mktempname+0xac>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d4 <mktempname+0x94>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11ec <mktempname+0xac>
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mktempname>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1060 <tmpnam@plt>
test   %rax,%rax
je     1194 <mktempname+0x54>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1194 <mktempname+0x54>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    1197 <mktempname+0x57>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@L_tmpnam = global i32 0
define i8* @mktempname() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = load i32, i32* @L_tmpnam
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %2
%9 = alloca i8, i64 %7
store i64 %7, i64* %3
%10 = call i8* @tmpnam(i8* %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %0
store i8* null, i8** %1
store i32 1, i32* %5
br label %24
14: ; preds = %0
%15 = call i64 @strlen(i8* %9)
%16 = add nsw i64 %15, 1
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %14
store i8* null, i8** %1
store i32 1, i32* %5
br label %24
21: ; preds = %14
%22 = load i8*, i8** %4
%23 = call i8* @strcpy(i8* %22, i8* %9)
store i8* %23, i8** %1
store i32 1, i32* %5
br label %24
24: ; preds = %21, %20, %13
%25 = load i8*, i8** %2
%26 = load i8*, i8** %1
ret i8* %26
}

LLVM IR (-O2):
@L_tmpnam = global i32 0
define i8* @mktempname() {
%1 = load i32, i32* @L_tmpnam
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = call i8* @tmpnam(i8* %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %13, label %6
6: ; preds = %0
%7 = call i64 @strlen(i8* %3)
%8 = add nsw i64 %7, 1
%9 = call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = call i8* @strcpy(i8* %9, i8* %3)
br label %13
13: ; preds = %6, %0, %11
%14 = phi i8* [ %9, %11 ], [ null, %0 ], [ null, %6 ]
ret i8* %14
}