#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 S_IRWXU;
int /*<<< orphan*/ S_ISREG(int);
int S_ISUID;
int S_IWUSR;
scalar_t__ getegid();
scalar_t__ geteuid();
int lstat(char *, struct stat *);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
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) {
      printf("1\n");
      return -1;
    }
  }
  if (read(random_fd, &random, sizeof(random)) != sizeof(random)) {
    printf("2\n");
    return -1;
  }
  sprintf(filename, "/tmp/%u", random);
  fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("3\n");
    return -1;
  }
  if (lstat(filename, &stat_buf) == -1) {
    printf("4\n");
    return -1;
  }
  if (!S_ISREG(stat_buf.st_mode)) {
    printf("5\n");
    return -1;
  }
  if (stat_buf.st_uid != geteuid() || stat_buf.st_gid != getegid()) {
    printf("6\n");
    return -1;
  }
  if ((stat_buf.st_mode & ~(S_IRUSR | S_IWUSR)) != 0) {
    printf("7, st_mode = %x, r = %x, w = %x, S_ISUID = %d, S_IRWXU %d\n",
           stat_buf.st_mode, S_IRUSR, S_IWUSR, S_ISUID, S_IRWXU);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<secure_temp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
cmpl   $0xffffffff,0x2ece(%rip)
jne    11e1 <secure_temp_file+0x61>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,0x2eab(%rip)
cmpl   $0xffffffff,0x2ea4(%rip)
jne    11dc <secure_temp_file+0x5c>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <secure_temp_file+0x25c>
jmp    11e1 <secure_temp_file+0x61>
mov    0x2e79(%rip),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cltq
cmp    $0x4,%rax
je     121b <secure_temp_file+0x9b>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <secure_temp_file+0x25c>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xde6(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
or     (%rax),%esi
mov    0x2d5f(%rip),%rax
or     (%rax),%esi
mov    0x2d4e(%rip),%rax
mov    (%rax),%edx
mov    0x2d5d(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    1299 <secure_temp_file+0x119>
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <secure_temp_file+0x25c>
lea    -0x90(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0xffffffff,%eax
jne    12cf <secure_temp_file+0x14f>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <secure_temp_file+0x25c>
mov    -0xb0(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    12fd <secure_temp_file+0x17d>
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <secure_temp_file+0x25c>
mov    -0xa8(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %rax,%rcx
mov    -0xb8(%rbp),%rax
cmp    %rcx,%rax
jne    134d <secure_temp_file+0x1cd>
mov    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    $0x0,%al
call   1080 <getegid@plt>
mov    %rax,%rcx
mov    -0xc0(%rbp),%rax
cmp    %rcx,%rax
je     1367 <secure_temp_file+0x1e7>
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <secure_temp_file+0x25c>
mov    -0xb0(%rbp),%eax
mov    0x2c34(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2c43(%rip),%rdx
or     (%rdx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     13d3 <secure_temp_file+0x253>
mov    -0xb0(%rbp),%esi
mov    0x2c0e(%rip),%rax
mov    (%rax),%edx
mov    0x2c1d(%rip),%rax
mov    (%rax),%ecx
mov    0x2c1c(%rip),%rax
mov    (%rax),%r8d
mov    0x2c2a(%rip),%rax
mov    (%rax),%r9d
lea    0xc66(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13dc <secure_temp_file+0x25c>
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   %r12
push   %rbx
sub    $0xa0,%rsp
mov    0x2ec3(%rip),%edi
cmp    $0xffffffff,%edi
jne    11d2 <secure_temp_file+0x42>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%edi
mov    %eax,0x2e9f(%rip)
cmp    $0xffffffff,%eax
je     12df <secure_temp_file+0x14f>
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x4,%eax
jne    12cd <secure_temp_file+0x13d>
mov    0x4(%rsp),%edx
lea    0xe17(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <sprintf@plt>
mov    0x2dcf(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
mov    0x2d84(%rip),%r14
mov    0x2d95(%rip),%r15
mov    (%r15),%edx
or     (%r14),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12d6 <secure_temp_file+0x146>
mov    %eax,%ebx
lea    0x20(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1070 <lstat@plt>
cmp    $0xffffffff,%eax
je     12e8 <secure_temp_file+0x158>
mov    0x8(%rsp),%r12d
mov    %r12d,%edi
call   1080 <S_ISREG@plt>
test   %eax,%eax
je     12fa <secure_temp_file+0x16a>
mov    0x10(%rsp),%rbp
xor    %eax,%eax
call   1050 <geteuid@plt>
cmp    %rax,%rbp
jne    12f1 <secure_temp_file+0x161>
mov    0x18(%rsp),%rbp
xor    %eax,%eax
call   1090 <getegid@plt>
cmp    %rax,%rbp
jne    12f1 <secure_temp_file+0x161>
mov    (%r14),%edx
mov    (%r15),%ecx
mov    %ecx,%eax
or     %edx,%eax
not    %eax
test   %eax,%r12d
je     130b <secure_temp_file+0x17b>
mov    0x2d1b(%rip),%rax
mov    (%rax),%r8d
mov    0x2d29(%rip),%rax
mov    (%rax),%r9d
lea    0xd53(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1306 <secure_temp_file+0x176>
lea    0xd83(%rip),%rdi
jmp    1301 <secure_temp_file+0x171>
lea    0xd78(%rip),%rdi
jmp    1301 <secure_temp_file+0x171>
lea    0xd73(%rip),%rdi
jmp    1301 <secure_temp_file+0x171>
lea    0xd64(%rip),%rdi
jmp    1301 <secure_temp_file+0x171>
lea    0xd59(%rip),%rdi
jmp    1301 <secure_temp_file+0x171>
lea    0xd4e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r12
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 = [3 x i8] c"1\0A\00"
@.str.2 = [3 x i8] c"2\0A\00"
@.str.3 = [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
@.str.4 = [3 x i8] c"3\0A\00"
@.str.5 = [3 x i8] c"4\0A\00"
@.str.6 = [3 x i8] c"5\0A\00"
@.str.7 = [3 x i8] c"6\0A\00"
@.str.8 = [59 x i8] c"7, st_mode = %x, r = %x, w = %x, S_ISUID = %d, S_IRWXU %d\0A\00"
@S_ISUID = global i32 0
@S_IRWXU = 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 %16
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 %15
13: ; preds = %8
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %85
15: ; preds = %8
br label %16
16: ; preds = %15, %0
%17 = load i32, i32* @secure_temp_file.random_fd
%18 = call i32 @read(i32 %17, i32* %2, i32 4)
%19 = sext i32 %18 to i64
%20 = icmp ne i64 %19, 4
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %85
23: ; preds = %16
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%25 = load i32, i32* %2
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = load i32, i32* @O_EXCL
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %33, %34
%36 = call i32 (i8*, i32, ...) @open(i8* %27, i32 %32, i32 %35)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %23
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %85
41: ; preds = %23
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%43 = call i32 @lstat(i8* %42, %struct.stat* %5)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %85
47: ; preds = %41
%48 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @S_ISREG(i32 %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %47
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %1
br label %85
54: ; preds = %47
%55 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%56 = load i64, i64* %55
%57 = call i64 (...) @geteuid()
%58 = icmp ne i64 %56, %57
br i1 %58, label %64, label %59
59: ; preds = %54
%60 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 2
%61 = load i64, i64* %60
%62 = call i64 (...) @getegid()
%63 = icmp ne i64 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %59, %54
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %1
br label %85
66: ; preds = %59
%67 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load i32, i32* @S_IRUSR
%70 = load i32, i32* @S_IWUSR
%71 = or i32 %69, %70
%72 = xor i32 %71, -1
%73 = and i32 %68, %72
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %83
75: ; preds = %66
%76 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%77 = load i32, i32* %76
%78 = load i32, i32* @S_IRUSR
%79 = load i32, i32* @S_IWUSR
%80 = load i32, i32* @S_ISUID
%81 = load i32, i32* @S_IRWXU
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.8, i64 0, i64 0), i32 %77, i32 %78, i32 %79, i32 %80, i32 %81)
store i32 -1, i32* %1
br label %85
83: ; preds = %66
%84 = load i32, i32* %4
store i32 %84, i32* %1
br label %85
85: ; preds = %83, %75, %64, %52, %45, %39, %21, %13
%86 = load i32, i32* %1
ret i32 %86
}

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.3 = [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
@.str.8 = [59 x i8] c"7, st_mode = %x, r = %x, w = %x, S_ISUID = %d, S_IRWXU %d\0A\00"
@S_ISUID = global i32 0
@S_IRWXU = global i32 0
@str = [2 x i8] c"5\00"
@str.9 = [2 x i8] c"6\00"
@str.10 = [2 x i8] c"4\00"
@str.11 = [2 x i8] c"3\00"
@str.12 = [2 x i8] c"2\00"
@str.13 = [2 x i8] c"1\00"
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 %15
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 %13, label %15
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.13, i64 0, i64 0))
br label %71
15: ; preds = %9, %0
%16 = phi i32 [ %11, %9 ], [ %7, %0 ]
%17 = call i32 @read(i32 %16, i32* %1, i32 4)
%18 = icmp eq i32 %17, 4
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.12, i64 0, i64 0))
br label %71
21: ; preds = %15
%22 = load i32, i32* %1
%23 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %25, %24
%27 = load i32, i32* @O_EXCL
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IWUSR
%31 = or i32 %30, %29
%32 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %28, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %21
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.11, i64 0, i64 0))
br label %71
36: ; preds = %21
%37 = call i32 @lstat(i8* %5, %struct.stat* %3)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.10, i64 0, i64 0))
br label %71
41: ; preds = %36
%42 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%43 = load i32, i32* %42
%44 = tail call i32 @S_ISREG(i32 %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %71
48: ; preds = %41
%49 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%50 = load i64, i64* %49
%51 = tail call i64 (...) @geteuid()
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %58
53: ; preds = %48
%54 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%55 = load i64, i64* %54
%56 = tail call i64 (...) @getegid()
%57 = icmp eq i64 %55, %56
br i1 %57, label %60, label %58
58: ; preds = %53, %48
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.9, i64 0, i64 0))
br label %71
60: ; preds = %53
%61 = load i32, i32* @S_IRUSR
%62 = load i32, i32* @S_IWUSR
%63 = or i32 %62, %61
%64 = xor i32 %63, -1
%65 = and i32 %43, %64
%66 = icmp eq i32 %65, 0
br i1 %66, label %71, label %67
67: ; preds = %60
%68 = load i32, i32* @S_ISUID
%69 = load i32, i32* @S_IRWXU
%70 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.8, i64 0, i64 0), i32 %43, i32 %61, i32 %62, i32 %68, i32 %69)
br label %71
71: ; preds = %60, %67, %58, %46, %39, %34, %19, %13
%72 = phi i32 [ -1, %13 ], [ -1, %19 ], [ -1, %34 ], [ -1, %39 ], [ -1, %58 ], [ -1, %67 ], [ -1, %46 ], [ %32, %60 ]
ret i32 %72
}