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

/* Variables and functions */
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ get_tmp_path(char *, int);
int mkstemps(char *, int);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int gen_tmp_file(const char *templ, char *path_out, FILE **fp_out) {
  int rc = -1;
  int fd, i_XXXXXX, suffix_len;
  char path[64];
  get_tmp_path(path, sizeof(path));
  strcat(path, "/");
  strcat(path, templ);
  for (i_XXXXXX = 0; i_XXXXXX < strlen(path) - 5; i_XXXXXX++) {
    if (0 == strncmp(path + i_XXXXXX, "XXXXXX", 6)) {
      break;
    }
  }
  if (i_XXXXXX >= strlen(path) - 5) {
    goto cleanup;
  }
  suffix_len = strlen(path) - (i_XXXXXX + 6);
  fd = mkstemps(path, suffix_len);
  if (fd < 0) {
    goto cleanup;
  }
  strcpy(path_out, path);
  *fp_out = fdopen(fd, "w");
  if (*fp_out == NULL) {
    goto cleanup;
  }
  rc = 0;
cleanup:
  return rc;
}


x86 assembly (clang, -O0):
<gen_tmp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x70(%rbp),%rdi
mov    $0x40,%esi
call   1070 <get_tmp_path@plt>
lea    -0x70(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1090 <strcat@plt>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1090 <strcat@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x74(%rbp)
lea    -0x70(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x74(%rbp),%eax
sub    $0x5,%ecx
cmp    %ecx,%eax
jge    1221 <gen_tmp_file+0xb1>
lea    -0x70(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
lea    0xe11(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    120e <gen_tmp_file+0x9e>
jmp    1221 <gen_tmp_file+0xb1>
jmp    1213 <gen_tmp_file+0xa3>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c0 <gen_tmp_file+0x50>
mov    -0x24(%rbp),%eax
mov    %eax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%eax
sub    $0x5,%ecx
cmp    %ecx,%eax
jl     1245 <gen_tmp_file+0xd5>
jmp    12b7 <gen_tmp_file+0x147>
lea    -0x70(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x24(%rbp),%ecx
add    $0x6,%ecx
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1030 <mkstemps@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1277 <gen_tmp_file+0x107>
jmp    12b7 <gen_tmp_file+0x147>
mov    -0x10(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%edi
lea    0xd7b(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12b0 <gen_tmp_file+0x140>
jmp    12b7 <gen_tmp_file+0x147>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_tmp_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
call   1070 <get_tmp_path@plt>
lea    0xe64(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strcat@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
xor    %ebp,%ebp
cmp    $0x6,%eax
jl     11fc <gen_tmp_file+0x8c>
xor    %ebp,%ebp
lea    0xe3b(%rip),%r12
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
lea    (%rsp,%rbp,1),%rdi
mov    %r12,%rsi
mov    $0x6,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11fc <gen_tmp_file+0x8c>
add    $0x1,%rbp
mov    %rbx,%rdi
call   1060 <strlen@plt>
add    $0xfffffffb,%eax
cltq
cmp    %rax,%rbp
jl     11d0 <gen_tmp_file+0x60>
mov    %rsp,%rdi
call   1060 <strlen@plt>
add    $0xfffffffb,%eax
mov    $0xffffffff,%ebx
cmp    %eax,%ebp
jge    1250 <gen_tmp_file+0xe0>
mov    %rsp,%r12
mov    %r12,%rdi
call   1060 <strlen@plt>
sub    %ebp,%eax
lea    -0x6(%rax),%esi
mov    %r12,%rdi
call   1030 <mkstemps@plt>
test   %eax,%eax
js     1250 <gen_tmp_file+0xe0>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r15,%rdi
call   1050 <strcpy@plt>
lea    0xdc9(%rip),%rsi
mov    %ebp,%edi
call   1080 <fdopen@plt>
mov    %rax,(%r14)
cmp    $0x1,%rax
sbb    %ebx,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [7 x i8] c"XXXXXX\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @gen_tmp_file(i8* %0, i8* %1, i32** %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [64 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32** %2, i32*** %6
store i32 -1, i32* %7
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%13 = call i32 @get_tmp_path(i8* %12, i32 64)
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%15 = call i32 @strcat(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%17 = load i8*, i8** %4
%18 = call i32 @strcat(i8* %16, i8* %17)
store i32 0, i32* %9
br label %19
19: ; preds = %34, %3
%20 = load i32, i32* %9
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 5
%24 = icmp slt i32 %20, %23
br i1 %24, label %25, label %37
25: ; preds = %19
%26 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = call i64 @strncmp(i8* %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%31 = icmp eq i64 0, %30
br i1 %31, label %32, label %33
32: ; preds = %25
br label %37
33: ; preds = %25
br label %34
34: ; preds = %33
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
store i32 %36, i32* %9
br label %19
37: ; preds = %32, %19
%38 = load i32, i32* %9
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
%41 = sub nsw i32 %40, 5
%42 = icmp sge i32 %38, %41
br i1 %42, label %43, label %44
43: ; preds = %37
br label %68
44: ; preds = %37
%45 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 6
%49 = sub nsw i32 %46, %48
store i32 %49, i32* %10
%50 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%51 = load i32, i32* %10
%52 = call i32 @mkstemps(i8* %50, i32 %51)
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %44
br label %68
56: ; preds = %44
%57 = load i8*, i8** %5
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%59 = call i32 @strcpy(i8* %57, i8* %58)
%60 = load i32, i32* %8
%61 = call i32* @fdopen(i32 %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%62 = load i32**, i32*** %6
store i32* %61, i32** %62
%63 = load i32**, i32*** %6
%64 = load i32*, i32** %63
%65 = icmp eq i32* %64, null
br i1 %65, label %66, label %67
66: ; preds = %56
br label %68
67: ; preds = %56
store i32 0, i32* %7
br label %68
68: ; preds = %67, %66, %55, %43
%69 = load i32, i32* %7
ret i32 %69
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [7 x i8] c"XXXXXX\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @gen_tmp_file(i8* %0, i8* %1, i32** nocapture writeonly %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i32 @get_tmp_path(i8* %5, i32 64)
%7 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @strcat(i8* %5, i8* %0)
%9 = call i32 @strlen(i8* %5)
%10 = icmp sgt i32 %9, 5
br i1 %10, label %11, label %25
11: ; preds = %3, %16
%12 = phi i64 [ %17, %16 ], [ 0, %3 ]
%13 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %12
%14 = call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %11
%17 = add nuw nsw i64 %12, 1
%18 = call i32 @strlen(i8* %5)
%19 = add nsw i32 %18, -5
%20 = sext i32 %19 to i64
%21 = icmp slt i64 %17, %20
br i1 %21, label %11, label %22
22: ; preds = %11, %16
%23 = phi i64 [ %17, %16 ], [ %12, %11 ]
%24 = trunc i64 %23 to i32
br label %25
25: ; preds = %22, %3
%26 = phi i32 [ 0, %3 ], [ %24, %22 ]
%27 = call i32 @strlen(i8* %5)
%28 = add nsw i32 %27, -5
%29 = icmp slt i32 %26, %28
br i1 %29, label %30, label %41
30: ; preds = %25
%31 = call i32 @strlen(i8* %5)
%32 = sub i32 -6, %26
%33 = add i32 %32, %31
%34 = call i32 @mkstemps(i8* %5, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %30
%37 = call i32 @strcpy(i8* %1, i8* %5)
%38 = call i32* @fdopen(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %38, i32** %2
%39 = icmp eq i32* %38, null
%40 = sext i1 %39 to i32
br label %41
41: ; preds = %36, %30, %25
%42 = phi i32 [ -1, %25 ], [ -1, %30 ], [ %40, %36 ]
ret i32 %42
}