#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 *);
scalar_t__ isWriteableDir(char *);
const char *getTempDir(void) {
  char *tempdir = getenv("KICKSTART_TMP");
  if (tempdir != NULL && isWriteableDir(tempdir))
    return tempdir;
  tempdir = getenv("GRIDSTART_TMP");
  if (tempdir != NULL && isWriteableDir(tempdir))
    return tempdir;
  tempdir = getenv("TMP");
  if (tempdir != NULL && isWriteableDir(tempdir))
    return tempdir;
  tempdir = getenv("TEMP");
  if (tempdir != NULL && isWriteableDir(tempdir))
    return tempdir;
  tempdir = getenv("TMPDIR");
  if (tempdir != NULL && isWriteableDir(tempdir))
    return tempdir;
  tempdir = "/tmp";
  if (isWriteableDir(tempdir))
    return tempdir;
  tempdir = "/var/tmp";
  if (isWriteableDir(tempdir))
    return tempdir;
  return tempdir;
}


x86 assembly (clang, -O0):
<getTempDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xed1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1163 <getTempDir+0x43>
mov    -0x10(%rbp),%rdi
call   1040 <isWriteableDir@plt>
cmp    $0x0,%rax
je     1163 <getTempDir+0x43>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <getTempDir+0x18d>
lea    0xea4(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119e <getTempDir+0x7e>
mov    -0x10(%rbp),%rdi
call   1040 <isWriteableDir@plt>
cmp    $0x0,%rax
je     119e <getTempDir+0x7e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <getTempDir+0x18d>
lea    0xe73(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d9 <getTempDir+0xb9>
mov    -0x10(%rbp),%rdi
call   1040 <isWriteableDir@plt>
cmp    $0x0,%rax
je     11d9 <getTempDir+0xb9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <getTempDir+0x18d>
lea    0xe3c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1214 <getTempDir+0xf4>
mov    -0x10(%rbp),%rdi
call   1040 <isWriteableDir@plt>
cmp    $0x0,%rax
je     1214 <getTempDir+0xf4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <getTempDir+0x18d>
lea    0xe06(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     124f <getTempDir+0x12f>
mov    -0x10(%rbp),%rdi
call   1040 <isWriteableDir@plt>
cmp    $0x0,%rax
je     124f <getTempDir+0x12f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <getTempDir+0x18d>
lea    0xdd6(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <isWriteableDir@plt>
cmp    $0x0,%rax
je     127a <getTempDir+0x15a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <getTempDir+0x18d>
lea    0xda7(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <isWriteableDir@plt>
cmp    $0x0,%rax
je     12a5 <getTempDir+0x185>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ad <getTempDir+0x18d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getTempDir>:
push   %rbx
lea    0xed8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1146 <getTempDir+0x26>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <isWriteableDir@plt>
test   %rax,%rax
jne    11f1 <getTempDir+0xd1>
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     116b <getTempDir+0x4b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <isWriteableDir@plt>
test   %rax,%rax
jne    11f1 <getTempDir+0xd1>
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     118c <getTempDir+0x6c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <isWriteableDir@plt>
test   %rax,%rax
jne    11f1 <getTempDir+0xd1>
lea    0xe89(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ad <getTempDir+0x8d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <isWriteableDir@plt>
test   %rax,%rax
jne    11f1 <getTempDir+0xd1>
lea    0xe6d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ce <getTempDir+0xae>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <isWriteableDir@plt>
test   %rax,%rax
jne    11f1 <getTempDir+0xd1>
lea    0xe57(%rip),%rbx
mov    %rbx,%rdi
call   1040 <isWriteableDir@plt>
test   %rax,%rax
jne    11f1 <getTempDir+0xd1>
lea    0xe3f(%rip),%rbx
mov    %rbx,%rdi
call   1040 <isWriteableDir@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"KICKSTART_TMP\00"
@.str.1 = [14 x i8] c"GRIDSTART_TMP\00"
@.str.2 = [4 x i8] c"TMP\00"
@.str.3 = [5 x i8] c"TEMP\00"
@.str.4 = [7 x i8] c"TMPDIR\00"
@.str.5 = [5 x i8] c"/tmp\00"
@.str.6 = [9 x i8] c"/var/tmp\00"
define i8* @getTempDir() {
%1 = alloca i8*
%2 = alloca i8*
%3 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %2
%4 = load i8*, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = load i8*, i8** %2
%8 = call i64 @isWriteableDir(i8* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = load i8*, i8** %2
store i8* %11, i8** %1
br label %66
12: ; preds = %6, %0
%13 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i8* %13, i8** %2
%14 = load i8*, i8** %2
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i8*, i8** %2
%18 = call i64 @isWriteableDir(i8* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i8*, i8** %2
store i8* %21, i8** %1
br label %66
22: ; preds = %16, %12
%23 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %23, i8** %2
%24 = load i8*, i8** %2
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i8*, i8** %2
%28 = call i64 @isWriteableDir(i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i8*, i8** %2
store i8* %31, i8** %1
br label %66
32: ; preds = %26, %22
%33 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %33, i8** %2
%34 = load i8*, i8** %2
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i8*, i8** %2
%38 = call i64 @isWriteableDir(i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i8*, i8** %2
store i8* %41, i8** %1
br label %66
42: ; preds = %36, %32
%43 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i8* %43, i8** %2
%44 = load i8*, i8** %2
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i8*, i8** %2
%48 = call i64 @isWriteableDir(i8* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i8*, i8** %2
store i8* %51, i8** %1
br label %66
52: ; preds = %46, %42
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8** %2
%53 = load i8*, i8** %2
%54 = call i64 @isWriteableDir(i8* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = load i8*, i8** %2
store i8* %57, i8** %1
br label %66
58: ; preds = %52
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8** %2
%59 = load i8*, i8** %2
%60 = call i64 @isWriteableDir(i8* %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = load i8*, i8** %2
store i8* %63, i8** %1
br label %66
64: ; preds = %58
%65 = load i8*, i8** %2
store i8* %65, i8** %1
br label %66
66: ; preds = %64, %62, %56, %50, %40, %30, %20, %10
%67 = load i8*, i8** %1
ret i8* %67
}

LLVM IR (-O2):
@.str = [14 x i8] c"KICKSTART_TMP\00"
@.str.1 = [14 x i8] c"GRIDSTART_TMP\00"
@.str.2 = [4 x i8] c"TMP\00"
@.str.3 = [5 x i8] c"TEMP\00"
@.str.4 = [7 x i8] c"TMPDIR\00"
@.str.5 = [5 x i8] c"/tmp\00"
@.str.6 = [9 x i8] c"/var/tmp\00"
define i8* @getTempDir() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i64 @isWriteableDir(i8* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %35
6: ; preds = %3, %0
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = tail call i64 @isWriteableDir(i8* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %9, %6
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = tail call i64 @isWriteableDir(i8* %13)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %35
18: ; preds = %15, %12
%19 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = tail call i64 @isWriteableDir(i8* %19)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %21, %18
%25 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = tail call i64 @isWriteableDir(i8* %25)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27, %24
%31 = tail call i64 @isWriteableDir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = tail call i64 @isWriteableDir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %33, %30, %27, %21, %15, %9, %3
%36 = phi i8* [ %1, %3 ], [ %7, %9 ], [ %13, %15 ], [ %19, %21 ], [ %25, %27 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), %30 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), %33 ]
ret i8* %36
}