#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 MAXPATHLEN;
scalar_t__ close(int);
int /*<<< orphan*/ free(char *);
char *getcwd(char *, int);
int /*<<< orphan*/ isp_dbgfail(char *, ...);
int mkstemp(char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
int /*<<< orphan*/ unlink(char *);
int util_mktmp(int *fdp, char **pathp) {
  char buf[MAXPATHLEN + 1];
  char *path;
  int fd;
  int res = 0;
  path = getcwd(buf, MAXPATHLEN + 1);
  if (path == NULL) {
    isp_dbgfail("util_mktmp: getcwd: %m");
    res = 31;
    goto done;
  }
  strcat(path, "/isptmpXXXXXX");
  if ((fd = mkstemp(path)) < 0) {
    isp_dbgfail("util_mktmp: mkstemp %s: %m", path);
    res = 20;
    goto done;
  }
  if (pathp) {
    *pathp = strdup(path);
    if (!*pathp) {
      res = 14;
      (void)close(fd);
      (void)unlink(path);
      goto done;
    }
  }
  if (fdp)
    *fdp = fd;
  else {
    if (close(fd) < 0) {
      isp_dbgfail("util_mktmp: close %s: %m", path);
      res = 20;
      if (pathp)
        free(*pathp);
      (void)close(fd);
      (void)unlink(path);
      goto done;
    }
  }
done:
  return res;
}


x86 assembly (clang, -O0):
<util_mktmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
call   1050 <getcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fc <util_mktmp+0x7c>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1070 <isp_dbgfail@plt>
movl   $0x1f,-0x30(%rbp)
jmp    1308 <util_mktmp+0x188>
mov    -0x28(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    123f <util_mktmp+0xbf>
mov    -0x28(%rbp),%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1070 <isp_dbgfail@plt>
movl   $0x14,-0x30(%rbp)
jmp    1308 <util_mktmp+0x188>
cmpq   $0x0,-0x10(%rbp)
je     128d <util_mktmp+0x10d>
mov    -0x28(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1288 <util_mktmp+0x108>
movl   $0xe,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rdi
call   1040 <unlink@plt>
jmp    1308 <util_mktmp+0x188>
jmp    128d <util_mktmp+0x10d>
cmpq   $0x0,-0x8(%rbp)
je     12a6 <util_mktmp+0x126>
mov    -0x2c(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    1303 <util_mktmp+0x183>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    12fe <util_mktmp+0x17e>
mov    -0x28(%rbp),%rsi
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1070 <isp_dbgfail@plt>
movl   $0x14,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12e8 <util_mktmp+0x168>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rdi
call   1040 <unlink@plt>
jmp    1308 <util_mktmp+0x188>
jmp    1303 <util_mktmp+0x183>
jmp    1308 <util_mktmp+0x188>
mov    -0x30(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_mktmp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    %rsp,%rdi
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdi
mov    %rdi,%rsp
call   1050 <getcwd@plt>
test   %rax,%rax
je     11fd <util_mktmp+0x7d>
mov    %rax,%rbx
lea    0xe55(%rip),%rsi
mov    %rax,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
call   1080 <mkstemp@plt>
test   %eax,%eax
js     1212 <util_mktmp+0x92>
mov    %eax,%r12d
test   %r14,%r14
je     11ee <util_mktmp+0x6e>
mov    %rbx,%rdi
call   10a0 <strdup@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     126c <util_mktmp+0xec>
test   %r15,%r15
je     122a <util_mktmp+0xaa>
mov    %r12d,(%r15)
xor    %eax,%eax
jmp    1281 <util_mktmp+0x101>
lea    0xdfc(%rip),%rdi
xor    %eax,%eax
call   1070 <isp_dbgfail@plt>
mov    $0x1f,%eax
jmp    1281 <util_mktmp+0x101>
lea    0xe0c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <isp_dbgfail@plt>
mov    $0x14,%eax
jmp    1281 <util_mktmp+0x101>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1281 <util_mktmp+0x101>
lea    0xdfd(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <isp_dbgfail@plt>
test   %r14,%r14
je     125a <util_mktmp+0xda>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1040 <unlink@plt>
jmp    1223 <util_mktmp+0xa3>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1040 <unlink@plt>
mov    $0xe,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@.str = [23 x i8] c"util_mktmp: getcwd: %m\00"
@.str.1 = [14 x i8] c"/isptmpXXXXXX\00"
@.str.2 = [27 x i8] c"util_mktmp: mkstemp %s: %m\00"
@.str.3 = [25 x i8] c"util_mktmp: close %s: %m\00"
define i32 @util_mktmp(i32* %0, i8** %1) {
%3 = alloca i32*
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
store i8** %1, i8*** %4
%10 = load i32, i32* @MAXPATHLEN
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
store i32 0, i32* %9
%15 = load i32, i32* @MAXPATHLEN
%16 = add nsw i32 %15, 1
%17 = call i8* @getcwd(i8* %14, i32 %16)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 31, i32* %9
br label %73
22: ; preds = %2
%23 = load i8*, i8** %7
%24 = call i32 @strcat(i8* %23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%25 = load i8*, i8** %7
%26 = call i32 @mkstemp(i8* %25)
store i32 %26, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i8*, i8** %7
%30 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store i32 20, i32* %9
br label %73
31: ; preds = %22
%32 = load i8**, i8*** %4
%33 = icmp ne i8** %32, null
br i1 %33, label %34, label %47
34: ; preds = %31
%35 = load i8*, i8** %7
%36 = call i8* @strdup(i8* %35)
%37 = load i8**, i8*** %4
store i8* %36, i8** %37
%38 = load i8**, i8*** %4
%39 = load i8*, i8** %38
%40 = icmp ne i8* %39, null
br i1 %40, label %46, label %41
41: ; preds = %34
store i32 14, i32* %9
%42 = load i32, i32* %8
%43 = call i64 @close(i32 %42)
%44 = load i8*, i8** %7
%45 = call i32 @unlink(i8* %44)
br label %73
46: ; preds = %34
br label %47
47: ; preds = %46, %31
%48 = load i32*, i32** %3
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %8
%52 = load i32*, i32** %3
store i32 %51, i32* %52
br label %72
53: ; preds = %47
%54 = load i32, i32* %8
%55 = call i64 @close(i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %71
57: ; preds = %53
%58 = load i8*, i8** %7
%59 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %58)
store i32 20, i32* %9
%60 = load i8**, i8*** %4
%61 = icmp ne i8** %60, null
br i1 %61, label %62, label %66
62: ; preds = %57
%63 = load i8**, i8*** %4
%64 = load i8*, i8** %63
%65 = call i32 @free(i8* %64)
br label %66
66: ; preds = %62, %57
%67 = load i32, i32* %8
%68 = call i64 @close(i32 %67)
%69 = load i8*, i8** %7
%70 = call i32 @unlink(i8* %69)
br label %73
71: ; preds = %53
br label %72
72: ; preds = %71, %50
br label %73
73: ; preds = %72, %66, %41, %28, %20
%74 = load i32, i32* %9
%75 = load i8*, i8** %5
ret i32 %74
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@.str = [23 x i8] c"util_mktmp: getcwd: %m\00"
@.str.1 = [14 x i8] c"/isptmpXXXXXX\00"
@.str.2 = [27 x i8] c"util_mktmp: mkstemp %s: %m\00"
@.str.3 = [25 x i8] c"util_mktmp: close %s: %m\00"
define i32 @util_mktmp(i32* writeonly %0, i8** %1) {
%3 = load i32, i32* @MAXPATHLEN
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i8* @getcwd(i8* %6, i32 %4)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %39
11: ; preds = %2
%12 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @mkstemp(i8* %7)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %7)
br label %39
17: ; preds = %11
%18 = icmp eq i8** %1, null
br i1 %18, label %25, label %19
19: ; preds = %17
%20 = call i8* @strdup(i8* %7)
store i8* %20, i8** %1
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i64 @close(i32 %13)
%24 = call i32 @unlink(i8* %7)
br label %39
25: ; preds = %19, %17
%26 = icmp eq i32* %0, null
br i1 %26, label %28, label %27
27: ; preds = %25
store i32 %13, i32* %0
br label %39
28: ; preds = %25
%29 = call i64 @close(i32 %13)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br i1 %18, label %36, label %33
33: ; preds = %31
%34 = load i8*, i8** %1
%35 = call i32 @free(i8* %34)
br label %36
36: ; preds = %33, %31
%37 = call i64 @close(i32 %13)
%38 = call i32 @unlink(i8* %7)
br label %39
39: ; preds = %27, %28, %36, %22, %15, %9
%40 = phi i32 [ 31, %9 ], [ 20, %15 ], [ 0, %27 ], [ 20, %36 ], [ 0, %28 ], [ 14, %22 ]
ret i32 %40
}