#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 */
char *getenv(char *);
char *mkdtemp(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *get_tmp_folder(void) {
  char *path = getenv("TMPDIR");
  if (!path) {
    path = getenv("TMP");
    if (!path) {
      path = "/tmp";
    }
  }
  char template[1024];
  sprintf(template, "%s/mwXXXXXX", path);
  return mkdtemp(template);
}


x86 assembly (clang, -O0):
<get_tmp_folder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
lea    0xebe(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1181 <get_tmp_folder+0x51>
lea    0xeaa(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117c <get_tmp_folder+0x4c>
lea    0xe93(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1181 <get_tmp_folder+0x51>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe7d(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x410(%rbp),%rdi
call   1040 <mkdtemp@plt>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_tmp_folder>:
push   %rbx
sub    $0x400,%rsp
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    1166 <get_tmp_folder+0x36>
lea    0xeb7(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
test   %rax,%rax
lea    0xea9(%rip),%rax
cmovne %rcx,%rax
lea    0xea3(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <mkdtemp@plt>
add    $0x400,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [4 x i8] c"TMP\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [12 x i8] c"%s/mwXXXXXX\00"
define i8* @get_tmp_folder() {
%1 = alloca [1024 x i8]
%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
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i8* %5, null
%7 = select i1 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %5
br label %8
8: ; preds = %4, %0
%9 = phi i8* [ %2, %0 ], [ %7, %4 ]
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%11 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%12 = call i8* @mkdtemp(i8* %10)
ret i8* %12
}