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

/* Variables and functions */
int /*<<< orphan*/ OPENSSL_cleanse(unsigned char *, int);
int O_CREAT;
int O_WRONLY;
scalar_t__ RAND_bytes(unsigned char *, int);
scalar_t__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ chmod(char const *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int open(char const *, int, int);
int stat(char const *, struct stat *);
int RAND_write_file(const char *file) {
  unsigned char buf[1024];
  int i, ret = 0, rand_err = 0;
  FILE *out = NULL;
  int n;
  struct stat sb;
  i = stat(file, &sb);
  if (i != -1) {
    if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
      return (1);
    }
  }
  {
    int fd = open(file, O_WRONLY | O_CREAT, 0600);
    if (fd != -1)
      out = fdopen(fd, "wb");
  }
  if (out == NULL)
    out = fopen(file, "wb");
  if (out == NULL)
    goto err;
  chmod(file, 0600);
  n = 1024;
  for (;;) {
    i = (n > 1024) ? 1024 : n;
    n -= 1024;
    if (RAND_bytes(buf, i) <= 0)
      rand_err = 1;
    i = fwrite(buf, 1, i, out);
    if (i <= 0) {
      ret = 0;
      break;
    }
    ret += i;
    if (n <= 0)
      break;
  }
  fclose(out);
  OPENSSL_cleanse(buf, 1024);
err:
  return (rand_err ? -1 : ret);
}


x86 assembly (clang, -O0):
<RAND_write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x418(%rbp)
movl   $0x0,-0x41c(%rbp)
movq   $0x0,-0x428(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
je     123c <RAND_write_file+0x8c>
mov    -0x430(%rbp),%edi
call   1080 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    122b <RAND_write_file+0x7b>
mov    -0x430(%rbp),%edi
call   1070 <S_ISCHR@plt>
cmp    $0x0,%rax
je     1237 <RAND_write_file+0x87>
movl   $0x1,-0x4(%rbp)
jmp    13ff <RAND_write_file+0x24f>
jmp    123c <RAND_write_file+0x8c>
mov    -0x10(%rbp),%rdi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0xffffffff,-0x434(%rbp)
je     1288 <RAND_write_file+0xd8>
mov    -0x434(%rbp),%edi
lea    0xd84(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    12ad <RAND_write_file+0xfd>
mov    -0x10(%rbp),%rdi
lea    0xd5f(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    12c0 <RAND_write_file+0x110>
jmp    13cd <RAND_write_file+0x21d>
mov    -0x10(%rbp),%rdi
mov    $0x180,%esi
call   10a0 <chmod@plt>
movl   $0x400,-0x42c(%rbp)
cmpl   $0x400,-0x42c(%rbp)
jle    12f8 <RAND_write_file+0x148>
mov    $0x400,%eax
mov    %eax,-0x438(%rbp)
jmp    1304 <RAND_write_file+0x154>
mov    -0x42c(%rbp),%eax
mov    %eax,-0x438(%rbp)
mov    -0x438(%rbp),%eax
mov    %eax,-0x414(%rbp)
mov    -0x42c(%rbp),%eax
sub    $0x400,%eax
mov    %eax,-0x42c(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
call   1040 <RAND_bytes@plt>
cmp    $0x0,%rax
jg     1347 <RAND_write_file+0x197>
movl   $0x1,-0x41c(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%edx
mov    -0x428(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     1387 <RAND_write_file+0x1d7>
movl   $0x0,-0x418(%rbp)
jmp    13b0 <RAND_write_file+0x200>
mov    -0x414(%rbp),%eax
add    -0x418(%rbp),%eax
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jg     13ab <RAND_write_file+0x1fb>
jmp    13b0 <RAND_write_file+0x200>
jmp    12d8 <RAND_write_file+0x128>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1050 <OPENSSL_cleanse@plt>
cmpl   $0x0,-0x41c(%rbp)
je     13ea <RAND_write_file+0x23a>
mov    $0xffffffff,%eax
mov    %eax,-0x43c(%rbp)
jmp    13f6 <RAND_write_file+0x246>
mov    -0x418(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x43c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RAND_write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r15
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11f8 <RAND_write_file+0x48>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1080 <S_ISBLK@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    12aa <RAND_write_file+0xfa>
mov    %ebx,%edi
call   1070 <S_ISCHR@plt>
test   %rax,%rax
jne    12aa <RAND_write_file+0xfa>
mov    0x2dd1(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1232 <RAND_write_file+0x82>
lea    0xddd(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1249 <RAND_write_file+0x99>
lea    0xdc7(%rip),%rsi
mov    %r15,%rdi
call   10c0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     12a8 <RAND_write_file+0xf8>
mov    %r15,%rdi
mov    $0x180,%esi
call   10a0 <chmod@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x400,%esi
call   1040 <RAND_bytes@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   10d0 <fwrite@plt>
xor    %ebp,%ebp
test   %eax,%eax
cmovns %eax,%ebp
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15,%rdi
mov    $0x400,%esi
call   1050 <OPENSSL_cleanse@plt>
test   %rbx,%rbx
jg     12aa <RAND_write_file+0xfa>
mov    $0xffffffff,%ebp
jmp    12aa <RAND_write_file+0xfa>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [3 x i8] c"wb\00"
define i32 @RAND_write_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
store i32 0, i32* %7
store i32* null, i32** %8
%12 = load i8*, i8** %3
%13 = call i32 @stat(i8* %12, %struct.stat* %10)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %28
16: ; preds = %1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i64 @S_ISBLK(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i64 @S_ISCHR(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21, %16
store i32 1, i32* %2
br label %96
27: ; preds = %21
br label %28
28: ; preds = %27, %1
%29 = load i8*, i8** %3
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %29, i32 %32, i32 384)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = load i32, i32* %11
%38 = call i32* @fdopen(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %38, i32** %8
br label %39
39: ; preds = %36, %28
%40 = load i32*, i32** %8
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i8*, i8** %3
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %44, i32** %8
br label %45
45: ; preds = %42, %39
%46 = load i32*, i32** %8
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %49
48: ; preds = %45
br label %88
49: ; preds = %45
%50 = load i8*, i8** %3
%51 = call i32 @chmod(i8* %50, i32 384)
store i32 1024, i32* %9
br label %52
52: ; preds = %82, %49
%53 = load i32, i32* %9
%54 = icmp sgt i32 %53, 1024
br i1 %54, label %55, label %56
55: ; preds = %52
br label %58
56: ; preds = %52
%57 = load i32, i32* %9
br label %58
58: ; preds = %56, %55
%59 = phi i32 [ 1024, %55 ], [ %57, %56 ]
store i32 %59, i32* %5
%60 = load i32, i32* %9
%61 = sub nsw i32 %60, 1024
store i32 %61, i32* %9
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%63 = load i32, i32* %5
%64 = call i64 @RAND_bytes(i8* %62, i32 %63)
%65 = icmp sle i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %58
store i32 1, i32* %7
br label %67
67: ; preds = %66, %58
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%69 = load i32, i32* %5
%70 = load i32*, i32** %8
%71 = call i32 @fwrite(i8* %68, i32 1, i32 %69, i32* %70)
store i32 %71, i32* %5
%72 = load i32, i32* %5
%73 = icmp sle i32 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %67
store i32 0, i32* %6
br label %83
75: ; preds = %67
%76 = load i32, i32* %5
%77 = load i32, i32* %6
%78 = add nsw i32 %77, %76
store i32 %78, i32* %6
%79 = load i32, i32* %9
%80 = icmp sle i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %75
br label %83
82: ; preds = %75
br label %52
83: ; preds = %81, %74
%84 = load i32*, i32** %8
%85 = call i32 @fclose(i32* %84)
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%87 = call i32 @OPENSSL_cleanse(i8* %86, i32 1024)
br label %88
88: ; preds = %83, %48
%89 = load i32, i32* %7
%90 = icmp ne i32 %89, 0
br i1 %90, label %91, label %92
91: ; preds = %88
br label %94
92: ; preds = %88
%93 = load i32, i32* %6
br label %94
94: ; preds = %92, %91
%95 = phi i32 [ -1, %91 ], [ %93, %92 ]
store i32 %95, i32* %2
br label %96
96: ; preds = %94, %26
%97 = load i32, i32* %2
ret i32 %97
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [3 x i8] c"wb\00"
define i32 @RAND_write_file(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 @stat(i8* %0, %struct.stat* %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %16, label %8
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISBLK(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %40
13: ; preds = %8
%14 = tail call i64 @S_ISCHR(i32 %10)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %40
16: ; preds = %13, %1
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* %0, i32 %19, i32 384)
%21 = icmp eq i32 %20, -1
br i1 %21, label %25, label %22
22: ; preds = %16
%23 = tail call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %16, %22
%26 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %38, label %28
28: ; preds = %22, %25
%29 = phi i32* [ %26, %25 ], [ %23, %22 ]
%30 = tail call i32 @chmod(i8* %0, i32 384)
%31 = call i64 @RAND_bytes(i8* %4, i32 1024)
%32 = icmp slt i64 %31, 1
%33 = call i32 @fwrite(i8* %4, i32 1, i32 1024, i32* %29)
%34 = icmp sgt i32 %33, 0
%35 = select i1 %34, i32 %33, i32 0
%36 = call i32 @fclose(i32* %29)
%37 = call i32 @OPENSSL_cleanse(i8* %4, i32 1024)
br i1 %32, label %40, label %38
38: ; preds = %25, %28
%39 = phi i32 [ %35, %28 ], [ 0, %25 ]
br label %40
40: ; preds = %38, %28, %8, %13
%41 = phi i32 [ 1, %13 ], [ 1, %8 ], [ %39, %38 ], [ -1, %28 ]
ret i32 %41
}