#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 */
typedef int /*<<< orphan*/ uid_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getuid();
char *malloc(int);
int mkstemp(char *);
int snprintf(char *, int, char const *, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
char *strdup(char const *);
int strlen(char const *);
int /*<<< orphan*/ *strstr(char const *, char *);
int _get_path(char **out, const char *in) {
  int len;
  uid_t uid;
  int fd;
  char *buffer = NULL;
  if (!in)
    return -1;
  if (strstr(in, "%d") != NULL) {
    uid = getuid();
    len = snprintf(buffer, 0, in, uid);
    if ((buffer = malloc(len + 1)) == NULL)
      return -1;
    snprintf(buffer, len + 1, in, uid);
  } else {
    len = strlen(in);
    if ((buffer = strdup(in)) == NULL)
      return -1;
  }
  if (strcmp(&(buffer[len - 6]), "XXXXXX") != 0) {
    free(buffer);
    buffer = NULL;
    return -1;
  }
  fd = mkstemp(buffer);
  *out = buffer;
  return fd;
}


x86 assembly (clang, -O0):
<_get_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bf <_get_path+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <_get_path+0x13b>
mov    -0x18(%rbp),%rdi
lea    0xe36(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1238 <_get_path+0xa8>
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%ecx
xor    %esi,%esi
call   1060 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    121d <_get_path+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <_get_path+0x13b>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%ecx
call   1060 <snprintf@plt>
jmp    126c <_get_path+0xdc>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1267 <_get_path+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <_get_path+0x13b>
jmp    126c <_get_path+0xdc>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%eax
sub    $0x6,%eax
cltq
add    %rax,%rdi
lea    0xd81(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12ae <_get_path+0x11e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <_get_path+0x13b>
mov    -0x30(%rbp),%rdi
call   1090 <mkstemp@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     123e <_get_path+0xae>
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe4f(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strstr@plt>
test   %rax,%rax
je     11ff <_get_path+0x6f>
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%r13d
xor    %edi,%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    %eax,%r12d
lea    0x1(%r12),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     123e <_get_path+0xae>
mov    %rax,%r15
mov    %rax,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r13d,%ecx
call   1060 <snprintf@plt>
jmp    121a <_get_path+0x8a>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   10a0 <strdup@plt>
test   %rax,%rax
je     123e <_get_path+0xae>
mov    %rax,%r15
movslq %r12d,%rax
lea    (%rax,%r15,1),%rdi
add    $0xfffffffffffffffa,%rdi
lea    0xdd7(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1252 <_get_path+0xc2>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1090 <mkstemp@plt>
mov    %r15,(%r14)
jmp    1243 <_get_path+0xb3>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [7 x i8] c"XXXXXX\00"
define i32 @_get_path(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 -1, i32* %3
br label %60
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i32* @strstr(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %35
17: ; preds = %13
%18 = call i32 (...) @getuid()
store i32 %18, i32* %7
%19 = load i8*, i8** %9
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = call i32 @snprintf(i8* %19, i32 0, i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %9
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %17
store i32 -1, i32* %3
br label %60
28: ; preds = %17
%29 = load i8*, i8** %9
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = load i8*, i8** %5
%33 = load i32, i32* %7
%34 = call i32 @snprintf(i8* %29, i32 %31, i8* %32, i32 %33)
br label %43
35: ; preds = %13
%36 = load i8*, i8** %5
%37 = call i32 @strlen(i8* %36)
store i32 %37, i32* %6
%38 = load i8*, i8** %5
%39 = call i8* @strdup(i8* %38)
store i8* %39, i8** %9
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %42
41: ; preds = %35
store i32 -1, i32* %3
br label %60
42: ; preds = %35
br label %43
43: ; preds = %42, %28
%44 = load i8*, i8** %9
%45 = load i32, i32* %6
%46 = sub nsw i32 %45, 6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %43
%52 = load i8*, i8** %9
%53 = call i32 @free(i8* %52)
store i8* null, i8** %9
store i32 -1, i32* %3
br label %60
54: ; preds = %43
%55 = load i8*, i8** %9
%56 = call i32 @mkstemp(i8* %55)
store i32 %56, i32* %8
%57 = load i8*, i8** %9
%58 = load i8**, i8*** %4
store i8* %57, i8** %58
%59 = load i32, i32* %8
store i32 %59, i32* %3
br label %60
60: ; preds = %54, %51, %41, %27, %12
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [7 x i8] c"XXXXXX\00"
define i32 @_get_path(i8** nocapture writeonly %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %31, label %4
4: ; preds = %2
%5 = tail call i32* @strstr(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %15, label %7
7: ; preds = %4
%8 = tail call i32 (...) @getuid()
%9 = tail call i32 @snprintf(i8* null, i32 0, i8* %1, i32 %8)
%10 = add nsw i32 %9, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %31, label %13
13: ; preds = %7
%14 = tail call i32 @snprintf(i8* %11, i32 %10, i8* %1, i32 %8)
br label %19
15: ; preds = %4
%16 = tail call i32 @strlen(i8* %1)
%17 = tail call i8* @strdup(i8* %1)
%18 = icmp eq i8* %17, null
br i1 %18, label %31, label %19
19: ; preds = %15, %13
%20 = phi i32 [ %9, %13 ], [ %16, %15 ]
%21 = phi i8* [ %11, %13 ], [ %17, %15 ]
%22 = add nsw i32 %20, -6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = tail call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %19
%28 = tail call i32 @free(i8* %21)
br label %31
29: ; preds = %19
%30 = tail call i32 @mkstemp(i8* %21)
store i8* %21, i8** %0
br label %31
31: ; preds = %15, %7, %2, %29, %27
%32 = phi i32 [ -1, %27 ], [ %30, %29 ], [ -1, %2 ], [ -1, %7 ], [ -1, %15 ]
ret i32 %32
}