#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);
char *getenv(char *);
int mkstemp(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int emu_tmpnam(char *buffer) {
  const char *tmpdir = getenv("TMPDIR");
  if (!tmpdir)
    tmpdir = "/tmp";
  strcpy(buffer, tmpdir);
  strcat(buffer, "/");
  strcat(buffer, "wg_XXXXXX");
  int fd = mkstemp(buffer);
  if (fd != -1)
    close(fd);
  return fd == -1;
}


x86 assembly (clang, -O0):
<emu_tmpnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <emu_tmpnam+0x32>
lea    0xe89(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x8(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x8(%rbp),%rdi
call   1060 <mkstemp@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     11cd <emu_tmpnam+0x7d>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0xffffffff,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<emu_tmpnam>:
push   %rbx
mov    %rdi,%rbx
lea    0xea5(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe9d(%rip),%rsi
cmovne %rax,%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
lea    0xe8f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
lea    0xe82(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    %rbx,%rdi
call   1060 <mkstemp@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11aa <emu_tmpnam+0x5a>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebx
sete   %al
pop    %rbx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"wg_XXXXXX\00"
define i32 @emu_tmpnam(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %9, label %8
8: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %9
9: ; preds = %8, %1
%10 = load i8*, i8** %2
%11 = load i8*, i8** %3
%12 = call i32 @strcpy(i8* %10, i8* %11)
%13 = load i8*, i8** %2
%14 = call i32 @strcat(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = load i8*, i8** %2
%16 = call i32 @strcat(i8* %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%17 = load i8*, i8** %2
%18 = call i32 @mkstemp(i8* %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %9
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
br label %24
24: ; preds = %21, %9
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, -1
%27 = zext i1 %26 to i32
ret i32 %27
}

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [10 x i8] c"wg_XXXXXX\00"
define i32 @emu_tmpnam(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
%4 = select i1 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %2
%5 = tail call i32 @strcpy(i8* %0, i8* %4)
%6 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%8 = tail call i32 @mkstemp(i8* %0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = tail call i32 @close(i32 %8)
br label %12
12: ; preds = %10, %1
%13 = zext i1 %9 to i32
ret i32 %13
}