#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 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*/ explicit_bzero(unsigned char *, 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;
  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);
  explicit_bzero(buf, 1024);
  return ret;
}


x86 assembly (clang, -O0):
<RAND_write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x418(%rbp)
movq   $0x0,-0x420(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     1213 <RAND_write_file+0x73>
mov    -0x430(%rbp),%edi
call   1080 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    1207 <RAND_write_file+0x67>
mov    -0x430(%rbp),%edi
call   1070 <S_ISCHR@plt>
cmp    $0x0,%rax
je     1213 <RAND_write_file+0x73>
movl   $0x1,-0x4(%rbp)
jmp    1384 <RAND_write_file+0x1e4>
mov    -0x10(%rbp),%rdi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10a0 <open@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0xffffffff,-0x428(%rbp)
jne    1252 <RAND_write_file+0xb2>
movl   $0x1,-0x4(%rbp)
jmp    1384 <RAND_write_file+0x1e4>
mov    -0x428(%rbp),%edi
lea    0xda1(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    1290 <RAND_write_file+0xf0>
mov    -0x428(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <RAND_write_file+0x1e4>
movl   $0x400,-0x424(%rbp)
cmpl   $0x400,-0x424(%rbp)
jle    12ba <RAND_write_file+0x11a>
mov    $0x400,%eax
mov    %eax,-0x434(%rbp)
jmp    12c6 <RAND_write_file+0x126>
mov    -0x424(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%eax
mov    %eax,-0x414(%rbp)
mov    -0x424(%rbp),%eax
sub    $0x400,%eax
mov    %eax,-0x424(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
call   1040 <arc4random_buf@plt>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%edx
mov    -0x420(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     1335 <RAND_write_file+0x195>
movl   $0x0,-0x418(%rbp)
jmp    135e <RAND_write_file+0x1be>
mov    -0x414(%rbp),%eax
add    -0x418(%rbp),%eax
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x424(%rbp)
jg     1359 <RAND_write_file+0x1b9>
jmp    135e <RAND_write_file+0x1be>
jmp    129a <RAND_write_file+0xfa>
mov    -0x420(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10b0 <explicit_bzero@plt>
mov    -0x418(%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   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11e9 <RAND_write_file+0x49>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1080 <S_ISBLK@plt>
mov    $0x1,%r15d
test   %rax,%rax
jne    1279 <RAND_write_file+0xd9>
mov    %ebp,%edi
call   1070 <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   10a0 <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   1090 <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   10b0 <explicit_bzero@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 %struct.stat
store i8* %0, i8** %3
store i32 0, i32* %6
store i32* null, i32** %7
%11 = load i8*, i8** %3
%12 = call i32 @stat(i8* %11, %struct.stat* %10)
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %25
14: ; preds = %1
%15 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @S_ISBLK(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %14
%20 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @S_ISCHR(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19, %14
store i32 1, i32* %2
br label %77
25: ; preds = %19, %1
%26 = load i8*, i8** %3
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %26, i32 %29, i32 384)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 1, i32* %2
br label %77
34: ; preds = %25
%35 = load i32, i32* %9
%36 = call i32* @fdopen(i32 %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %36, i32** %7
%37 = load i32*, i32** %7
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i32, i32* %9
%41 = call i32 @close(i32 %40)
store i32 1, i32* %2
br label %77
42: ; preds = %34
store i32 1024, i32* %8
br label %43
43: ; preds = %70, %42
%44 = load i32, i32* %8
%45 = icmp sgt i32 %44, 1024
br i1 %45, label %46, label %47
46: ; preds = %43
br label %49
47: ; preds = %43
%48 = load i32, i32* %8
br label %49
49: ; preds = %47, %46
%50 = phi i32 [ 1024, %46 ], [ %48, %47 ]
store i32 %50, i32* %5
%51 = load i32, i32* %8
%52 = sub nsw i32 %51, 1024
store i32 %52, i32* %8
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%54 = load i32, i32* %5
%55 = call i32 @arc4random_buf(i8* %53, i32 %54)
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%57 = load i32, i32* %5
%58 = load i32*, i32** %7
%59 = call i32 @fwrite(i8* %56, i32 1, i32 %57, i32* %58)
store i32 %59, i32* %5
%60 = load i32, i32* %5
%61 = icmp sle i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %49
store i32 0, i32* %6
br label %71
63: ; preds = %49
%64 = load i32, i32* %5
%65 = load i32, i32* %6
%66 = add nsw i32 %65, %64
store i32 %66, i32* %6
%67 = load i32, i32* %8
%68 = icmp sle i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %63
br label %71
70: ; preds = %63
br label %43
71: ; preds = %69, %62
%72 = load i32*, i32** %7
%73 = call i32 @fclose(i32* %72)
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%75 = call i32 @explicit_bzero(i8* %74, i32 1024)
%76 = load i32, i32* %6
store i32 %76, i32* %2
br label %77
77: ; preds = %71, %39, %33, %24
%78 = load i32, i32* %2
ret i32 %78
}

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 @explicit_bzero(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
}