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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
unsigned char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ printf(char *, char const *, ...);
int /*<<< orphan*/ stdout;
int strlen(char const *);
int crypto_file_rng(const char *device, const char *msg, unsigned char *buf,
                    const int count) {
  const char spinner[] = "|/-\\";
  const int size = strlen(spinner);
  int i;
  FILE *f;
  f = fopen(device, "r");
  if (!f) {
    return 1;
  }
  for (i = 0; i < count; i++) {
    buf[i] = fgetc(f);
    if (msg && i % 8 == 0) {
      printf("\r%s %3d%%  %c ", msg, i * 100 / count, spinner[i / 11 % size]);
      fflush(stdout);
    }
  }
  fclose(f);
  if (msg)
    printf("\r%s OK!       \n", msg);
  return 0;
}


x86 assembly (clang, -O0):
<crypto_file_rng>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0xe83(%rip),%eax
mov    %eax,-0x29(%rbp)
mov    0xe7e(%rip),%al
mov    %al,-0x25(%rbp)
lea    -0x29(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11c0 <crypto_file_rng+0x60>
movl   $0x1,-0x4(%rbp)
jmp    128e <crypto_file_rng+0x12e>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1261 <crypto_file_rng+0x101>
mov    -0x40(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     124e <crypto_file_rng+0xee>
mov    -0x34(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    124e <crypto_file_rng+0xee>
mov    -0x18(%rbp),%rsi
imul   $0x64,-0x34(%rbp),%eax
cltd
idivl  -0x24(%rbp)
mov    %eax,-0x44(%rbp)
mov    -0x34(%rbp),%eax
mov    $0xb,%ecx
cltd
idiv   %ecx
cltd
idivl  -0x30(%rbp)
mov    %edx,%eax
mov    -0x44(%rbp),%edx
cltq
movsbl -0x29(%rbp,%rax,1),%ecx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    1253 <crypto_file_rng+0xf3>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11c7 <crypto_file_rng+0x67>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     1287 <crypto_file_rng+0x127>
mov    -0x18(%rbp),%rsi
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypto_file_rng>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,0x10(%rsp)
mov    %rsi,%r12
mov    %rdi,%rbp
movb   $0x0,0x8(%rsp)
movl   $0x5c2d2f7c,0x4(%rsp)
5c
lea    0x4(%rsp),%rdi
call   1040 <strlen@plt>
mov    %eax,0xc(%rsp)
lea    0xe68(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1252 <crypto_file_rng+0xf2>
mov    %rax,%r13
test   %ebx,%ebx
jle    1230 <crypto_file_rng+0xd0>
mov    %ebx,%r15d
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    11cc <crypto_file_rng+0x6c>
nop
add    $0x1,%r14
add    $0x64,%ebp
cmp    %r14,%r15
je     1230 <crypto_file_rng+0xd0>
mov    %r13,%rdi
call   1060 <fgetc@plt>
mov    0x10(%rsp),%rcx
mov    %al,(%rcx,%r14,1)
test   %r12,%r12
je     11c0 <crypto_file_rng+0x60>
mov    %r14d,%eax
and    $0x7,%eax
jne    11c0 <crypto_file_rng+0x60>
mov    %ebp,%eax
cltd
idiv   %r15d
mov    %eax,%ebx
mov    %r14d,%eax
mov    $0xba2e8ba3,%ecx
imul   %rcx,%rax
shr    $0x23,%rax
xor    %edx,%edx
idivl  0xc(%rsp)
movsbl 0x4(%rsp,%rdx,1),%ecx
lea    0xdf3(%rip),%rdi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    11c0 <crypto_file_rng+0x60>
mov    %r13,%rdi
call   1030 <fclose@plt>
test   %r12,%r12
je     1259 <crypto_file_rng+0xf9>
lea    0xdd2(%rip),%rdi
xor    %ebp,%ebp
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    125b <crypto_file_rng+0xfb>
mov    $0x1,%ebp
jmp    125b <crypto_file_rng+0xfb>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.crypto_file_rng.spinner = [5 x i8] c"|/-\\\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [15 x i8] c"\0D%s %3d%% %c \00"
@stdout = global i32 0
@.str.2 = [16 x i8] c"\0D%s OK! \0A\00"
define i32 @crypto_file_rng(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [5 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = bitcast [5 x i8]* %10 to i8*
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %11
%17 = load i8*, i8** %6
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %13
%19 = load i32*, i32** %13
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %4
store i32 1, i32* %5
br label %70
22: ; preds = %4
store i32 0, i32* %12
br label %23
23: ; preds = %58, %22
%24 = load i32, i32* %12
%25 = load i32, i32* %9
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %61
27: ; preds = %23
%28 = load i32*, i32** %13
%29 = call zeroext i8 @fgetc(i32* %28)
%30 = load i8*, i8** %8
%31 = load i32, i32* %12
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 %29, i8* %33
%34 = load i8*, i8** %7
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %57
36: ; preds = %27
%37 = load i32, i32* %12
%38 = srem i32 %37, 8
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %57
40: ; preds = %36
%41 = load i8*, i8** %7
%42 = load i32, i32* %12
%43 = mul nsw i32 %42, 100
%44 = load i32, i32* %9
%45 = sdiv i32 %43, %44
%46 = load i32, i32* %12
%47 = sdiv i32 %46, 11
%48 = load i32, i32* %11
%49 = srem i32 %47, %48
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %41, i32 %45, i32 %53)
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
br label %57
57: ; preds = %40, %36, %27
br label %58
58: ; preds = %57
%59 = load i32, i32* %12
%60 = add nsw i32 %59, 1
store i32 %60, i32* %12
br label %23
61: ; preds = %23
%62 = load i32*, i32** %13
%63 = call i32 @fclose(i32* %62)
%64 = load i8*, i8** %7
%65 = icmp ne i8* %64, null
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i8*, i8** %7
%68 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %66, %61
store i32 0, i32* %5
br label %70
70: ; preds = %69, %21
%71 = load i32, i32* %5
ret i32 %71
}

LLVM IR (-O2):
@__const.crypto_file_rng.spinner = [5 x i8] c"|/-\\\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [15 x i8] c"\0D%s %3d%% %c \00"
@stdout = global i32 0
@.str.2 = [16 x i8] c"\0D%s OK! \0A\00"
define i32 @crypto_file_rng(i8* nocapture %0, i8* %1, i8* nocapture writeonly %2, i32 %3) {
%5 = alloca [5 x i8]
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%7 = call i32 @strlen(i8* %6)
%8 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %43, label %10
10: ; preds = %4
%11 = icmp sgt i32 %3, 0
br i1 %11, label %12, label %38
12: ; preds = %10
%13 = icmp ne i8* %1, null
%14 = zext i32 %3 to i64
br label %15
15: ; preds = %12, %35
%16 = phi i64 [ 0, %12 ], [ %36, %35 ]
%17 = call zeroext i8 @fgetc(i32* %8)
%18 = getelementptr inbounds i8, i8* %2, i64 %16
store i8 %17, i8* %18
%19 = trunc i64 %16 to i32
%20 = and i32 %19, 7
%21 = icmp eq i32 %20, 0
%22 = select i1 %13, i1 %21, i1 false
br i1 %22, label %23, label %35
23: ; preds = %15
%24 = mul nsw i32 %19, 100
%25 = sdiv i32 %24, %3
%26 = udiv i32 %19, 11
%27 = srem i32 %26, %7
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %25, i32 %31)
%33 = load i32, i32* @stdout
%34 = call i32 @fflush(i32 %33)
br label %35
35: ; preds = %15, %23
%36 = add nuw nsw i64 %16, 1
%37 = icmp eq i64 %36, %14
br i1 %37, label %38, label %15
38: ; preds = %35, %10
%39 = call i32 @fclose(i32* %8)
%40 = icmp eq i8* %1, null
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %43
43: ; preds = %38, %41, %4
%44 = phi i32 [ 1, %4 ], [ 0, %41 ], [ 0, %38 ]
ret i32 %44
}