#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*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *bmalloc(size_t);
char *getenv(char *);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ sysbail(char *, char *);
char *test_tmpdir(void) {
  const char *build;
  char *path = NULL;
  size_t length;
  build = getenv("BUILD");
  if (build == NULL)
    build = ".";
  length = strlen(build) + strlen("/tmp") + 1;
  path = bmalloc(length);
  snprintf(path, length, "%s/tmp", build);
  if (access(path, X_OK) < 0)
    if (mkdir(path, 0777) < 0)
      sysbail("error creating temporary directory %s", path);
  return path;
}


x86 assembly (clang, -O0):
<test_tmpdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
lea    0xe79(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a6 <test_tmpdir+0x36>
lea    0xe64(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe51(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <bmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe1a(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jge    123c <test_tmpdir+0xcc>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
cmp    $0x0,%rax
jge    1237 <test_tmpdir+0xc7>
mov    -0x10(%rbp),%rsi
lea    0xddd(%rip),%rdi
call   1080 <sysbail@plt>
jmp    123c <test_tmpdir+0xcc>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_tmpdir>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe7b(%rip),%rbx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
lea    0xe69(%rip),%rdi
call   1060 <strlen@plt>
add    %r14d,%eax
add    $0x1,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1040 <bmalloc@plt>
mov    %rax,%r14
lea    0xe47(%rip),%rdx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <access@plt>
test   %rax,%rax
jns    1206 <test_tmpdir+0x96>
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1050 <mkdir@plt>
test   %rax,%rax
jns    1206 <test_tmpdir+0x96>
lea    0xe11(%rip),%rdi
mov    %r14,%rsi
call   1080 <sysbail@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"BUILD\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [7 x i8] c"%s/tmp\00"
@X_OK = global i32 0
@.str.4 = [38 x i8] c"error creating temporary directory %s\00"
define i8* @test_tmpdir() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
store i8* null, i8** %2
%4 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %1
br label %8
8: ; preds = %7, %0
%9 = load i8*, i8** %1
%10 = call i32 @strlen(i8* %9)
%11 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%12 = add nsw i32 %10, %11
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = call i8* @bmalloc(i64 %15)
store i8* %16, i8** %2
%17 = load i8*, i8** %2
%18 = load i64, i64* %3
%19 = load i8*, i8** %1
%20 = call i32 @snprintf(i8* %17, i64 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %2
%22 = load i32, i32* @X_OK
%23 = call i64 @access(i8* %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %8
%26 = load i8*, i8** %2
%27 = call i64 @mkdir(i8* %26, i32 511)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %2
%31 = call i32 @sysbail(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %25
br label %33
33: ; preds = %32, %8
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@.str = [6 x i8] c"BUILD\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [7 x i8] c"%s/tmp\00"
@X_OK = global i32 0
@.str.4 = [38 x i8] c"error creating temporary directory %s\00"
define i8* @test_tmpdir() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
%3 = select i1 %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %1
%4 = tail call i32 @strlen(i8* %3)
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%6 = add i32 %4, 1
%7 = add i32 %6, %5
%8 = sext i32 %7 to i64
%9 = tail call i8* @bmalloc(i64 %8)
%10 = tail call i32 @snprintf(i8* %9, i64 %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%11 = load i32, i32* @X_OK
%12 = tail call i64 @access(i8* %9, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %0
%15 = tail call i64 @mkdir(i8* %9, i32 511)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @sysbail(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %9)
br label %19
19: ; preds = %14, %17, %0
ret i8* %9
}