#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*/ free(char *);
char *getenv(char *);
int mkstemp(char *);
int /*<<< orphan*/ unlink(char *);
char *uwsgi_concat2(char *, char *);
int uwsgi_tmpfd() {
  int fd = -1;
  char *tmpdir = getenv("TMPDIR");
  if (!tmpdir) {
    tmpdir = "/tmp";
  }
  char *template = uwsgi_concat2(tmpdir, "/uwsgiXXXXXX");
  fd = mkstemp(template);
  unlink(template);
  free(template);
  return fd;
}


x86 assembly (clang, -O0):
<uwsgi_tmpfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xffffffff,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1185 <uwsgi_tmpfd+0x35>
lea    0xe86(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <uwsgi_concat2@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <mkstemp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <unlink@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uwsgi_tmpfd>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe9e(%rip),%rdi
cmovne %rax,%rdi
lea    0xe98(%rip),%rsi
call   1070 <uwsgi_concat2@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <mkstemp@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <unlink@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [13 x i8] c"/uwsgiXXXXXX\00"
define i32 @uwsgi_tmpfd() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 -1, i32* %1
%4 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %8
8: ; preds = %7, %0
%9 = load i8*, i8** %2
%10 = call i8* @uwsgi_concat2(i8* %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = call i32 @mkstemp(i8* %11)
store i32 %12, i32* %1
%13 = load i8*, i8** %3
%14 = call i32 @unlink(i8* %13)
%15 = load i8*, i8** %3
%16 = call i32 @free(i8* %15)
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [13 x i8] c"/uwsgiXXXXXX\00"
define i32 @uwsgi_tmpfd() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
%3 = select i1 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %1
%4 = tail call i8* @uwsgi_concat2(i8* %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%5 = tail call i32 @mkstemp(i8* %4)
%6 = tail call i32 @unlink(i8* %4)
%7 = tail call i32 @free(i8* %4)
ret i32 %5
}