#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int _mktemp_error(char *, int);
scalar_t__ close(int);
int /*<<< orphan*/ free(char *);
int getpid();
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int getuid();
int mkstemp(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ srand(int);
char *strdup(char const *);
int _mktemp(char const *template) {
  int ret = 0;
  char *t;
  int fd;
  struct timeval tv;
  if ((t = strdup(template)) == NULL)
    return _mktemp_error("strdup", 1);
  if (gettimeofday(&tv, NULL) != 0) {
    free(t);
    return _mktemp_error("gettimeofday", 1);
  }
  srand(tv.tv_sec ^ tv.tv_usec ^ getuid() ^ (getpid() << 16));
  if ((fd = mkstemp(t)) == -1 || close(fd) != 0) {
    ret = _mktemp_error(t, 1);
    free(t);
    return ret;
  }
  printf("%s\n", t);
  free(t);
  return ret;
}


x86 assembly (clang, -O0):
<_mktemp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e3 <_mktemp+0x43>
lea    0xe2f(%rip),%rdi
mov    $0x1,%esi
call   10b0 <_mktemp_error@plt>
mov    %eax,-0x4(%rbp)
jmp    12b5 <_mktemp+0x115>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
cmp    $0x0,%rax
je     121c <_mktemp+0x7c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xdfd(%rip),%rdi
mov    $0x1,%esi
call   10b0 <_mktemp_error@plt>
mov    %eax,-0x4(%rbp)
jmp    12b5 <_mktemp+0x115>
mov    -0x30(%rbp),%eax
xor    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
xor    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x34(%rbp),%edi
shl    $0x10,%eax
xor    %eax,%edi
call   1090 <srand@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <mkstemp@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1271 <_mktemp+0xd1>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
cmp    $0x0,%rax
je     1296 <_mktemp+0xf6>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   10b0 <_mktemp_error@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <_mktemp+0x115>
mov    -0x20(%rbp),%rsi
lea    0xd73(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mktemp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
call   10c0 <strdup@plt>
test   %rax,%rax
je     11d7 <_mktemp+0x37>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
test   %rax,%rax
je     11f0 <_mktemp+0x50>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe32(%rip),%rdi
jmp    11de <_mktemp+0x3e>
lea    0xe22(%rip),%rdi
mov    $0x1,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    10b0 <_mktemp_error@plt>
mov    0xc(%rsp),%r14d
xor    0x8(%rsp),%r14d
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
shl    $0x10,%eax
xor    %eax,%ebp
xor    %r14d,%ebp
mov    %ebp,%edi
call   1090 <srand@plt>
mov    %rbx,%rdi
call   10a0 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1232 <_mktemp+0x92>
mov    %eax,%edi
call   1080 <close@plt>
test   %rax,%rax
je     124d <_mktemp+0xad>
mov    %rbx,%rdi
mov    $0x1,%esi
call   10b0 <_mktemp_error@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
jmp    1266 <_mktemp+0xc6>
lea    0xdc0(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [7 x i8] c"strdup\00"
@.str.1 = [13 x i8] c"gettimeofday\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @_mktemp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.timeval
store i8* %0, i8** %3
store i32 0, i32* %4
%8 = load i8*, i8** %3
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @_mktemp_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %12, i32* %2
br label %51
13: ; preds = %1
%14 = call i64 @gettimeofday(%struct.timeval* %7, i32* null)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i8*, i8** %5
%18 = call i32 @free(i8* %17)
%19 = call i32 @_mktemp_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 %19, i32* %2
br label %51
20: ; preds = %13
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%24 = load i32, i32* %23
%25 = xor i32 %22, %24
%26 = call i32 (...) @getuid()
%27 = xor i32 %25, %26
%28 = call i32 (...) @getpid()
%29 = shl i32 %28, 16
%30 = xor i32 %27, %29
%31 = call i32 @srand(i32 %30)
%32 = load i8*, i8** %5
%33 = call i32 @mkstemp(i8* %32)
store i32 %33, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %39, label %35
35: ; preds = %20
%36 = load i32, i32* %6
%37 = call i64 @close(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %35, %20
%40 = load i8*, i8** %5
%41 = call i32 @_mktemp_error(i8* %40, i32 1)
store i32 %41, i32* %4
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %51
45: ; preds = %35
%46 = load i8*, i8** %5
%47 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i8*, i8** %5
%49 = call i32 @free(i8* %48)
%50 = load i32, i32* %4
store i32 %50, i32* %2
br label %51
51: ; preds = %45, %39, %16, %11
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [7 x i8] c"strdup\00"
@.str.1 = [13 x i8] c"gettimeofday\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @_mktemp(i8* nocapture %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = tail call i8* @strdup(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @_mktemp_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
br label %37
8: ; preds = %1
%9 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @free(i8* %4)
%13 = tail call i32 @_mktemp_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %37
14: ; preds = %8
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%18 = load i32, i32* %17
%19 = xor i32 %18, %16
%20 = tail call i32 (...) @getuid()
%21 = xor i32 %19, %20
%22 = tail call i32 (...) @getpid()
%23 = shl i32 %22, 16
%24 = xor i32 %21, %23
%25 = tail call i32 @srand(i32 %24)
%26 = tail call i32 @mkstemp(i8* %4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %31, label %28
28: ; preds = %14
%29 = tail call i64 @close(i32 %26)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28, %14
%32 = tail call i32 @_mktemp_error(i8* %4, i32 1)
%33 = tail call i32 @free(i8* %4)
br label %37
34: ; preds = %28
%35 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%36 = tail call i32 @free(i8* %4)
br label %37
37: ; preds = %34, %31, %11, %6
%38 = phi i32 [ %7, %6 ], [ %13, %11 ], [ %32, %31 ], [ 0, %34 ]
ret i32 %38
}