#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 stat {
  int st_mode;
  scalar_t__ st_uid;
  scalar_t__ st_gid;
};
typedef int /*<<< orphan*/ random;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int S_IRUSR;
int /*<<< orphan*/ S_ISREG(int);
int S_IWUSR;
scalar_t__ getegid();
scalar_t__ geteuid();
int lstat(char *, struct stat *);
int open(char *, int, ...);
int read(int, unsigned int *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int secure_temp_file() {
  static int random_fd = -1;
  unsigned int random;
  char filename[128];
  int fd;
  struct stat stat_buf;
  if (random_fd == -1) {
    random_fd = open("/dev/random", O_RDONLY);
    if (random_fd == -1)
      return -1;
  }
  if (read(random_fd, &random, sizeof(random)) != sizeof(random))
    return -1;
  sprintf(filename, "/tmp/%u", random);
  fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1)
    return -1;
  if (lstat(filename, &stat_buf) == -1)
    return -1;
  if (!S_ISREG(stat_buf.st_mode))
    return -1;
  if (stat_buf.st_uid != geteuid() || stat_buf.st_gid != getegid())
    return -1;
  if ((stat_buf.st_mode & ~(S_IRUSR | S_IWUSR)) != 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<secure_temp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
cmpl   $0xffffffff,0x2ed6(%rip)
jne    11c3 <secure_temp_file+0x53>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,0x2eb3(%rip)
cmpl   $0xffffffff,0x2eac(%rip)
jne    11be <secure_temp_file+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <secure_temp_file+0x1ce>
jmp    11c3 <secure_temp_file+0x53>
mov    0x2e8f(%rip),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cltq
cmp    $0x4,%rax
je     11ef <secure_temp_file+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <secure_temp_file+0x1ce>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe0c(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
mov    0x2d9b(%rip),%rax
or     (%rax),%esi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    0x2d99(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    125f <secure_temp_file+0xef>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <secure_temp_file+0x1ce>
lea    -0x90(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0xffffffff,%eax
jne    1287 <secure_temp_file+0x117>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <secure_temp_file+0x1ce>
mov    -0xb0(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    12a7 <secure_temp_file+0x137>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <secure_temp_file+0x1ce>
mov    -0xa8(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %rax,%rcx
mov    -0xb8(%rbp),%rax
cmp    %rcx,%rax
jne    12f7 <secure_temp_file+0x187>
mov    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    $0x0,%al
call   1070 <getegid@plt>
mov    %rax,%rcx
mov    -0xc0(%rbp),%rax
cmp    %rcx,%rax
je     1303 <secure_temp_file+0x193>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <secure_temp_file+0x1ce>
mov    -0xb0(%rbp),%eax
mov    0x2ca8(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2cb7(%rip),%rdx
or     (%rdx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1335 <secure_temp_file+0x1c5>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <secure_temp_file+0x1ce>
mov    -0x94(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secure_temp_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    0x2ed1(%rip),%edi
cmp    $0xffffffff,%edi
jne    11b4 <secure_temp_file+0x44>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%edi
mov    %eax,0x2ead(%rip)
cmp    $0xffffffff,%eax
je     1286 <secure_temp_file+0x116>
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    $0xffffffff,%ebx
cmp    $0x4,%eax
jne    128b <secure_temp_file+0x11b>
mov    0x4(%rsp),%edx
lea    0xe30(%rip),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <sprintf@plt>
mov    0x2df0(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
or     (%rax),%esi
mov    0x2dad(%rip),%r15
mov    0x2dbe(%rip),%r12
mov    (%r12),%edx
or     (%r15),%edx
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     128b <secure_temp_file+0x11b>
mov    %eax,%ebp
lea    0x20(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <lstat@plt>
cmp    $0xffffffff,%eax
je     128b <secure_temp_file+0x11b>
mov    0x8(%rsp),%r14d
mov    %r14d,%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     128b <secure_temp_file+0x11b>
mov    0x10(%rsp),%r13
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    %rax,%r13
jne    128b <secure_temp_file+0x11b>
mov    0x18(%rsp),%r13
xor    %eax,%eax
call   1070 <getegid@plt>
cmp    %rax,%r13
jne    128b <secure_temp_file+0x11b>
mov    (%r12),%eax
or     (%r15),%eax
not    %eax
and    %eax,%r14d
neg    %r14d
sbb    %ebx,%ebx
or     %ebp,%ebx
jmp    128b <secure_temp_file+0x11b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i64, i64 }
@secure_temp_file.random_fd = internal global i32 -1
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"/tmp/%u\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @secure_temp_file() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca %struct.stat
%6 = load i32, i32* @secure_temp_file.random_fd
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %15
8: ; preds = %0
%9 = load i32, i32* @O_RDONLY
%10 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* @secure_temp_file.random_fd
%11 = load i32, i32* @secure_temp_file.random_fd
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %8
store i32 -1, i32* %1
br label %72
14: ; preds = %8
br label %15
15: ; preds = %14, %0
%16 = load i32, i32* @secure_temp_file.random_fd
%17 = call i32 @read(i32 %16, i32* %2, i32 4)
%18 = sext i32 %17 to i64
%19 = icmp ne i64 %18, 4
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 -1, i32* %1
br label %72
21: ; preds = %15
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%23 = load i32, i32* %2
%24 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = load i32, i32* @O_EXCL
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %31, %32
%34 = call i32 (i8*, i32, ...) @open(i8* %25, i32 %30, i32 %33)
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %21
store i32 -1, i32* %1
br label %72
38: ; preds = %21
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%40 = call i32 @lstat(i8* %39, %struct.stat* %5)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 -1, i32* %1
br label %72
43: ; preds = %38
%44 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @S_ISREG(i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %43
store i32 -1, i32* %1
br label %72
49: ; preds = %43
%50 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%51 = load i64, i64* %50
%52 = call i64 (...) @geteuid()
%53 = icmp ne i64 %51, %52
br i1 %53, label %59, label %54
54: ; preds = %49
%55 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 2
%56 = load i64, i64* %55
%57 = call i64 (...) @getegid()
%58 = icmp ne i64 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %54, %49
store i32 -1, i32* %1
br label %72
60: ; preds = %54
%61 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%62 = load i32, i32* %61
%63 = load i32, i32* @S_IRUSR
%64 = load i32, i32* @S_IWUSR
%65 = or i32 %63, %64
%66 = xor i32 %65, -1
%67 = and i32 %62, %66
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %60
store i32 -1, i32* %1
br label %72
70: ; preds = %60
%71 = load i32, i32* %4
store i32 %71, i32* %1
br label %72
72: ; preds = %70, %69, %59, %48, %42, %37, %20, %13
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
%struct.stat = type { i32, i64, i64 }
@secure_temp_file.random_fd = internal unnamed_addr global i32 -1
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"/tmp/%u\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @secure_temp_file() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca %struct.stat
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.stat* %3 to i8*
%7 = load i32, i32* @secure_temp_file.random_fd
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* @secure_temp_file.random_fd
%12 = icmp eq i32 %11, -1
br i1 %12, label %56, label %13
13: ; preds = %9, %0
%14 = phi i32 [ %11, %9 ], [ %7, %0 ]
%15 = call i32 @read(i32 %14, i32* %1, i32 4)
%16 = icmp eq i32 %15, 4
br i1 %16, label %17, label %56
17: ; preds = %13
%18 = load i32, i32* %1
%19 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = load i32, i32* @O_EXCL
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %26, %25
%28 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %24, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %56, label %30
30: ; preds = %17
%31 = call i32 @lstat(i8* %5, %struct.stat* %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %56, label %33
33: ; preds = %30
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%35 = load i32, i32* %34
%36 = tail call i32 @S_ISREG(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %56, label %38
38: ; preds = %33
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%40 = load i64, i64* %39
%41 = tail call i64 (...) @geteuid()
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %56
43: ; preds = %38
%44 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%45 = load i64, i64* %44
%46 = tail call i64 (...) @getegid()
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %56
48: ; preds = %43
%49 = load i32, i32* @S_IRUSR
%50 = load i32, i32* @S_IWUSR
%51 = or i32 %50, %49
%52 = xor i32 %51, -1
%53 = and i32 %35, %52
%54 = icmp eq i32 %53, 0
%55 = select i1 %54, i32 %28, i32 -1
br label %56
56: ; preds = %48, %38, %43, %33, %30, %17, %13, %9
%57 = phi i32 [ -1, %9 ], [ -1, %13 ], [ -1, %17 ], [ -1, %30 ], [ -1, %33 ], [ -1, %43 ], [ -1, %38 ], [ %55, %48 ]
ret i32 %57
}