#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int errno;
char *getenv(char *);
int /*<<< orphan*/ realpath(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int find_tmpdir(char *result) {
  char *temp_candidates[] = {"TMPDIR", "TMP", "TEMP"};
  char *temp;
  int x;
  int exit_code = EXIT_FAILURE;
  for (x = 0; x < 3; x++) {
    temp = getenv(temp_candidates[x]);
    if (temp != NULL) {
      exit_code = EXIT_SUCCESS;
      break;
    }
  }
  if (exit_code == EXIT_SUCCESS) {
    x = errno;
    realpath(temp, result);
    errno = x;
    if (result[0] != '\x00') {
      return EXIT_SUCCESS;
    }
  }
  strcpy(result, "/tmp");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<find_tmpdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2cad(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2caa(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2ca7(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x3,-0x3c(%rbp)
jge    11bb <find_tmpdir+0x8b>
movslq -0x3c(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11a8 <find_tmpdir+0x78>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    11bb <find_tmpdir+0x8b>
jmp    11ad <find_tmpdir+0x7d>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1170 <find_tmpdir+0x40>
mov    -0x40(%rbp),%eax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%eax
jne    1218 <find_tmpdir+0xe8>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <realpath@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2df0(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1213 <find_tmpdir+0xe3>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <find_tmpdir+0x104>
jmp    1218 <find_tmpdir+0xe8>
mov    -0x10(%rbp),%rdi
lea    0xded(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_tmpdir>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e9a(%rip),%rax
mov    (%rax),%ebp
lea    0xeb9(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    1180 <find_tmpdir+0x50>
lea    0xeaf(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    1180 <find_tmpdir+0x50>
lea    0xea2(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    1180 <find_tmpdir+0x50>
mov    0x2e56(%rip),%rax
cmp    (%rax),%ebp
jne    119e <find_tmpdir+0x6e>
xor    %eax,%eax
mov    0x2e59(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <realpath@plt>
mov    %ebx,0x0(%rbp)
cmpb   $0x0,(%r14)
jne    11ad <find_tmpdir+0x7d>
lea    0xe6b(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
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"TEMP\00"
@__const.find_tmpdir.temp_candidates = [3 x i8*] [i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0)]
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@errno = global i32 0
@.str.3 = [5 x i8] c"/tmp\00"
define i32 @find_tmpdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [3 x i8*]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = bitcast [3 x i8*]* %4 to i8*
%9 = load i32, i32* @EXIT_FAILURE
store i32 %9, i32* %7
store i32 0, i32* %6
br label %10
10: ; preds = %24, %1
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 %15
%17 = load i8*, i8** %16
%18 = call i8* @getenv(i8* %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = load i32, i32* @EXIT_SUCCESS
store i32 %22, i32* %7
br label %27
23: ; preds = %13
br label %24
24: ; preds = %23
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
br label %10
27: ; preds = %21, %10
%28 = load i32, i32* %7
%29 = load i32, i32* @EXIT_SUCCESS
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %45
31: ; preds = %27
%32 = load i32, i32* @errno
store i32 %32, i32* %6
%33 = load i8*, i8** %5
%34 = load i8*, i8** %3
%35 = call i32 @realpath(i8* %33, i8* %34)
%36 = load i32, i32* %6
store i32 %36, i32* @errno
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %31
%43 = load i32, i32* @EXIT_SUCCESS
store i32 %43, i32* %2
br label %49
44: ; preds = %31
br label %45
45: ; preds = %44, %27
%46 = load i8*, i8** %3
%47 = call i32 @strcpy(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* @EXIT_SUCCESS
store i32 %48, i32* %2
br label %49
49: ; preds = %45, %42
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [4 x i8] c"TMP\00"
@.str.2 = [5 x i8] c"TEMP\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@errno = global i32 0
@.str.3 = [5 x i8] c"/tmp\00"
define i32 @find_tmpdir(i8* %0) {
%2 = load i32, i32* @EXIT_FAILURE
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %14
5: ; preds = %1
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @EXIT_SUCCESS
%13 = icmp eq i32 %2, %12
br i1 %13, label %14, label %20
14: ; preds = %8, %5, %1, %11
%15 = phi i8* [ null, %11 ], [ %3, %1 ], [ %6, %5 ], [ %9, %8 ]
%16 = load i32, i32* @errno
%17 = tail call i32 @realpath(i8* %15, i8* %0)
store i32 %16, i32* @errno
%18 = load i8, i8* %0
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14, %11
%21 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %14, %20
%23 = load i32, i32* @EXIT_SUCCESS
ret i32 %23
}