#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*/ die(char *);
char *getenv(char *);
char *malloc(int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *preptemplate(const char *template) {
  char *dir = getenv("TMPDIR");
  if (!dir)
    dir = "/tmp";
  int templen = strlen(template);
  int dirlen = strlen(dir);
  char *out = malloc(templen + dirlen + 2);
  if (!out)
    die("Cannot allocate memory: %m\n");
  strncpy(out, dir, dirlen);
  out[dirlen] = '/';
  strncpy(out + dirlen + 1, template, templen);
  out[dirlen + templen + 1] = '\0';
  return out;
}


x86 assembly (clang, -O0):
<preptemplate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <preptemplate+0x32>
lea    0xe89(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
add    -0x18(%rbp),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <preptemplate+0x73>
lea    0xe4e(%rip),%rdi
call   1070 <die@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<preptemplate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe9b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe93(%rip),%r12
cmovne %rax,%r12
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
lea    (%r15,%r13,1),%ebp
lea    (%r15,%r13,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b2 <preptemplate+0x62>
lea    0xe5f(%rip),%rdi
call   1070 <die@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1040 <strncpy@plt>
movslq %r13d,%rax
movb   $0x2f,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,0x1(%rax,%rbx,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [28 x i8] c"Cannot allocate memory: %m\0A\00"
define i8* @preptemplate(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %2
%7 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %11
11: ; preds = %10, %1
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %4
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %5
%16 = load i32, i32* %4
%17 = load i32, i32* %5
%18 = add nsw i32 %16, %17
%19 = add nsw i32 %18, 2
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %11
%24 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %11
%26 = load i8*, i8** %6
%27 = load i8*, i8** %3
%28 = load i32, i32* %5
%29 = call i32 @strncpy(i8* %26, i8* %27, i32 %28)
%30 = load i8*, i8** %6
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 47, i8* %33
%34 = load i8*, i8** %6
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8*, i8** %2
%40 = load i32, i32* %4
%41 = call i32 @strncpy(i8* %38, i8* %39, i32 %40)
%42 = load i8*, i8** %6
%43 = load i32, i32* %5
%44 = load i32, i32* %4
%45 = add nsw i32 %43, %44
%46 = add nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %42, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %6
ret i8* %49
}

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [28 x i8] c"Cannot allocate memory: %m\0A\00"
define i8* @preptemplate(i8* %0) {
%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
%4 = select i1 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %2
%5 = tail call i32 @strlen(i8* %0)
%6 = tail call i32 @strlen(i8* %4)
%7 = add nsw i32 %6, %5
%8 = add nsw i32 %7, 2
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
%14 = tail call i32 @strncpy(i8* %9, i8* %4, i32 %6)
%15 = sext i32 %6 to i64
%16 = getelementptr inbounds i8, i8* %9, i64 %15
store i8 47, i8* %16
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = tail call i32 @strncpy(i8* %17, i8* %0, i32 %5)
%19 = add nsw i32 %7, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %9, i64 %20
store i8 0, i8* %21
ret i8* %9
}