#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*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, int *, int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int padlock_xstore_rng(int *);
int nist_rbg_get_entropy(void *entropy, int min_length, int buffer_length) {
  int count = 0, rv, len;
  int buffer[2];
  char *p = (char *)entropy;
  for (;;) {
    rv = padlock_xstore_rng(buffer);
    len = rv & 0x0f;
    if (len < 1) {
      if (count >= min_length) {
        memset((buffer), 0, (sizeof(buffer)));
        return count;
      }
      continue;
    }
    if (len > buffer_length - count) {
      len = buffer_length - count;
      if (!len) {
        memset((buffer), 0, (sizeof(buffer)));
        return count;
      }
    }
    memcpy(p, buffer, len);
    p += len;
    count += len;
    if (count >= buffer_length) {
      memset((buffer), 0, (sizeof(buffer)));
      return count;
    }
  }
}


x86 assembly (clang, -O0):
<nist_rbg_get_entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x2c(%rbp),%rdi
call   1050 <padlock_xstore_rng@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    119c <nist_rbg_get_entropy+0x6c>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1197 <nist_rbg_get_entropy+0x67>
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <nist_rbg_get_entropy+0x106>
jmp    1151 <nist_rbg_get_entropy+0x21>
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
cmp    %ecx,%eax
jle    11e0 <nist_rbg_get_entropy+0xb0>
mov    -0x18(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11db <nist_rbg_get_entropy+0xab>
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <nist_rbg_get_entropy+0x106>
jmp    11e0 <nist_rbg_get_entropy+0xb0>
mov    -0x38(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     1231 <nist_rbg_get_entropy+0x101>
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <nist_rbg_get_entropy+0x106>
jmp    1151 <nist_rbg_get_entropy+0x21>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nist_rbg_get_entropy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r13d
mov    %rdi,%r15
xor    %ebp,%ebp
mov    %rsp,%r12
nopl   0x0(%rax)
mov    %r12,%rdi
call   1050 <padlock_xstore_rng@plt>
mov    %eax,%ebx
and    $0xf,%ebx
jne    1166 <nist_rbg_get_entropy+0x36>
cmp    %r13d,%ebp
jl     1150 <nist_rbg_get_entropy+0x20>
jmp    1193 <nist_rbg_get_entropy+0x63>
mov    %r14d,%eax
sub    %ebp,%eax
cmp    %eax,%ebx
jle    1175 <nist_rbg_get_entropy+0x45>
mov    %eax,%ebx
test   %eax,%eax
je     1193 <nist_rbg_get_entropy+0x63>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movslq %ebx,%rbx
add    %rbx,%r15
add    %ebp,%ebx
mov    %ebx,%ebp
cmp    %r14d,%ebx
jl     1150 <nist_rbg_get_entropy+0x20>
jmp    1195 <nist_rbg_get_entropy+0x65>
mov    %ebp,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nist_rbg_get_entropy(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2 x i32]
%12 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%13 = load i8*, i8** %5
store i8* %13, i8** %12
br label %14
14: ; preds = %66, %29, %3
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%16 = call i32 @padlock_xstore_rng(i32* %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = and i32 %17, 15
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %30
21: ; preds = %14
%22 = load i32, i32* %8
%23 = load i32, i32* %6
%24 = icmp sge i32 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%27 = call i32 @memset(i32* %26, i32 0, i32 8)
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %67
29: ; preds = %21
br label %14
30: ; preds = %14
%31 = load i32, i32* %10
%32 = load i32, i32* %7
%33 = load i32, i32* %8
%34 = sub nsw i32 %32, %33
%35 = icmp sgt i32 %31, %34
br i1 %35, label %36, label %47
36: ; preds = %30
%37 = load i32, i32* %7
%38 = load i32, i32* %8
%39 = sub nsw i32 %37, %38
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %36
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%44 = call i32 @memset(i32* %43, i32 0, i32 8)
%45 = load i32, i32* %8
store i32 %45, i32* %4
br label %67
46: ; preds = %36
br label %47
47: ; preds = %46, %30
%48 = load i8*, i8** %12
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%50 = load i32, i32* %10
%51 = call i32 @memcpy(i8* %48, i32* %49, i32 %50)
%52 = load i32, i32* %10
%53 = load i8*, i8** %12
%54 = sext i32 %52 to i64
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8* %55, i8** %12
%56 = load i32, i32* %10
%57 = load i32, i32* %8
%58 = add nsw i32 %57, %56
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = load i32, i32* %7
%61 = icmp sge i32 %59, %60
br i1 %61, label %62, label %66
62: ; preds = %47
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%64 = call i32 @memset(i32* %63, i32 0, i32 8)
%65 = load i32, i32* %8
store i32 %65, i32* %4
br label %67
66: ; preds = %47
br label %14
67: ; preds = %62, %42, %25
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
define i32 @nist_rbg_get_entropy(i8* %0, i32 %1, i32 %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
br label %7
7: ; preds = %21, %3
%8 = phi i32 [ %26, %21 ], [ 0, %3 ]
%9 = phi i8* [ %25, %21 ], [ %0, %3 ]
%10 = icmp slt i32 %8, %1
br label %11
11: ; preds = %7, %15
%12 = call i32 @padlock_xstore_rng(i32* %6)
%13 = and i32 %12, 15
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
br i1 %10, label %11, label %28
16: ; preds = %11
%17 = sub nsw i32 %2, %8
%18 = icmp sgt i32 %13, %17
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = icmp eq i32 %17, 0
br i1 %20, label %28, label %21
21: ; preds = %19, %16
%22 = phi i32 [ %17, %19 ], [ %13, %16 ]
%23 = call i32 @memcpy(i8* %9, i32* %6, i32 %22)
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %9, i64 %24
%26 = add nsw i32 %22, %8
%27 = icmp slt i32 %26, %2
br i1 %27, label %7, label %28
28: ; preds = %21, %19, %15
%29 = phi i32 [ %8, %15 ], [ %8, %19 ], [ %26, %21 ]
%30 = call i32 @memset(i32* %6, i32 0, i32 8)
ret i32 %29
}