#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__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ arc4random_buf(unsigned char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, 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, fd;
  struct stat sb;
  if (stat(file, &sb) != -1 && (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode))) {
    return (1);
  }
  fd = open(file, O_WRONLY | O_CREAT, 0600);
  if (fd == -1)
    return (1);
  out = fdopen(fd, "wb");
  if (out == NULL) {
    close(fd);
    return (1);
  }
  n = 1024;
  for (;;) {
    i = (n > 1024) ? 1024 : n;
    n -= 1024;
    arc4random_buf(buf, i);
    i = fwrite(buf, 1, i, out);
    if (i <= 0) {
      ret = 0;
      break;
    }
    ret += i;
    if (n <= 0)
      break;
  }
  fclose(out);
  OPENSSL_cleanse(buf, 1024);
  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    -0x438(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     121d <RAND_write_file+0x7d>
mov    -0x438(%rbp),%edi
call   1090 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    1211 <RAND_write_file+0x71>
mov    -0x438(%rbp),%edi
call   1080 <S_ISCHR@plt>
cmp    $0x0,%rax
je     121d <RAND_write_file+0x7d>
movl   $0x1,-0x4(%rbp)
jmp    13b7 <RAND_write_file+0x217>
mov    -0x10(%rbp),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0xffffffff,-0x430(%rbp)
jne    125c <RAND_write_file+0xbc>
movl   $0x1,-0x4(%rbp)
jmp    13b7 <RAND_write_file+0x217>
mov    -0x430(%rbp),%edi
lea    0xd97(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    129a <RAND_write_file+0xfa>
mov    -0x430(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b7 <RAND_write_file+0x217>
movl   $0x400,-0x42c(%rbp)
cmpl   $0x400,-0x42c(%rbp)
jle    12c4 <RAND_write_file+0x124>
mov    $0x400,%eax
mov    %eax,-0x43c(%rbp)
jmp    12d0 <RAND_write_file+0x130>
mov    -0x42c(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x43c(%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 <arc4random_buf@plt>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%edx
mov    -0x428(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     133f <RAND_write_file+0x19f>
movl   $0x0,-0x418(%rbp)
jmp    1368 <RAND_write_file+0x1c8>
mov    -0x414(%rbp),%eax
add    -0x418(%rbp),%eax
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jg     1363 <RAND_write_file+0x1c3>
jmp    1368 <RAND_write_file+0x1c8>
jmp    12a4 <RAND_write_file+0x104>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1060 <OPENSSL_cleanse@plt>
cmpl   $0x0,-0x41c(%rbp)
je     13a2 <RAND_write_file+0x202>
mov    $0xffffffff,%eax
mov    %eax,-0x440(%rbp)
jmp    13ae <RAND_write_file+0x20e>
mov    -0x418(%rbp),%eax
mov    %eax,-0x440(%rbp)
mov    -0x440(%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,%rbx
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     11e9 <RAND_write_file+0x49>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1090 <S_ISBLK@plt>
mov    $0x1,%r15d
test   %rax,%rax
jne    1279 <RAND_write_file+0xd9>
mov    %ebp,%edi
call   1080 <S_ISCHR@plt>
test   %rax,%rax
jne    1279 <RAND_write_file+0xd9>
mov    0x2de0(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <open@plt>
mov    $0x1,%r15d
cmp    $0xffffffff,%eax
je     1279 <RAND_write_file+0xd9>
mov    %eax,%ebx
lea    0xde4(%rip),%rsi
mov    %eax,%edi
call   10a0 <fdopen@plt>
test   %rax,%rax
je     1272 <RAND_write_file+0xd2>
mov    %rax,%r14
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1040 <arc4random_buf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   10c0 <fwrite@plt>
xor    %r15d,%r15d
test   %eax,%eax
cmovns %eax,%r15d
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
call   1060 <OPENSSL_cleanse@plt>
jmp    1279 <RAND_write_file+0xd9>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15d,%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 i32
%11 = alloca %struct.stat
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* %11)
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %26
15: ; preds = %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @S_ISBLK(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @S_ISCHR(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20, %15
store i32 1, i32* %2
br label %84
26: ; preds = %20, %1
%27 = load i8*, i8** %3
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %27, i32 %30, i32 384)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %26
store i32 1, i32* %2
br label %84
35: ; preds = %26
%36 = load i32, i32* %10
%37 = call i32* @fdopen(i32 %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %37, i32** %8
%38 = load i32*, i32** %8
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* %10
%42 = call i32 @close(i32 %41)
store i32 1, i32* %2
br label %84
43: ; preds = %35
store i32 1024, i32* %9
br label %44
44: ; preds = %71, %43
%45 = load i32, i32* %9
%46 = icmp sgt i32 %45, 1024
br i1 %46, label %47, label %48
47: ; preds = %44
br label %50
48: ; preds = %44
%49 = load i32, i32* %9
br label %50
50: ; preds = %48, %47
%51 = phi i32 [ 1024, %47 ], [ %49, %48 ]
store i32 %51, i32* %5
%52 = load i32, i32* %9
%53 = sub nsw i32 %52, 1024
store i32 %53, i32* %9
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%55 = load i32, i32* %5
%56 = call i32 @arc4random_buf(i8* %54, i32 %55)
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%58 = load i32, i32* %5
%59 = load i32*, i32** %8
%60 = call i32 @fwrite(i8* %57, i32 1, i32 %58, i32* %59)
store i32 %60, i32* %5
%61 = load i32, i32* %5
%62 = icmp sle i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %50
store i32 0, i32* %6
br label %72
64: ; preds = %50
%65 = load i32, i32* %5
%66 = load i32, i32* %6
%67 = add nsw i32 %66, %65
store i32 %67, i32* %6
%68 = load i32, i32* %9
%69 = icmp sle i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %64
br label %72
71: ; preds = %64
br label %44
72: ; preds = %70, %63
%73 = load i32*, i32** %8
%74 = call i32 @fclose(i32* %73)
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%76 = call i32 @OPENSSL_cleanse(i8* %75, i32 1024)
%77 = load i32, i32* %7
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %72
br label %82
80: ; preds = %72
%81 = load i32, i32* %6
br label %82
82: ; preds = %80, %79
%83 = phi i32 [ -1, %79 ], [ %81, %80 ]
store i32 %83, i32* %2
br label %84
84: ; preds = %82, %40, %34, %25
%85 = load i32, i32* %2
ret i32 %85
}

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 %34
13: ; preds = %8
%14 = tail call i64 @S_ISCHR(i32 %10)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %34
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 %34, 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 %27
25: ; preds = %22
%26 = tail call i32 @close(i32 %20)
br label %34
27: ; preds = %22
%28 = call i32 @arc4random_buf(i8* %4, i32 1024)
%29 = call i32 @fwrite(i8* %4, i32 1, i32 1024, i32* %23)
%30 = icmp sgt i32 %29, 0
%31 = select i1 %30, i32 %29, i32 0
%32 = call i32 @fclose(i32* %23)
%33 = call i32 @OPENSSL_cleanse(i8* %4, i32 1024)
br label %34
34: ; preds = %16, %8, %13, %27, %25
%35 = phi i32 [ 1, %25 ], [ %31, %27 ], [ 1, %13 ], [ 1, %8 ], [ 1, %16 ]
ret i32 %35
}