#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*/ buf;
typedef int /*<<< orphan*/ RAND_METHOD;
typedef int /*<<< orphan*/ ENGINE;

/* Variables and functions */
int /*<<< orphan*/ *ENGINE_get_RAND(int /*<<< orphan*/ *);
int /*<<< orphan*/ RAND_bytes(unsigned char *, int);
int /*<<< orphan*/ RAND_seed(char *, int);
int RAND_set_rand_method(int /*<<< orphan*/ const *);
int /*<<< orphan*/ hexdump(char *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int test_rand(ENGINE *eg) {
  unsigned char buf[128];
  const RAND_METHOD *m;
  int r;
  printf("Testing Random Generator\n");
  m = ENGINE_get_RAND(eg);
  if (!m) {
    printf("Failed to get random method\n");
    return -1;
  }
  r = RAND_set_rand_method(m);
  if (r != 1) {
    printf("Failed to set random method\n");
    return -1;
  }
  memset(buf, 0, sizeof(buf));
  RAND_seed("test", 4);
  RAND_bytes(buf, sizeof(buf));
  hexdump("Random Buffer", buf, sizeof(buf));
  return 0;
}


x86 assembly (clang, -O0):
<test_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ENGINE_get_RAND@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11c1 <test_rand+0x51>
lea    0xe6a(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <test_rand+0xdc>
mov    -0x98(%rbp),%rdi
call   1090 <RAND_set_rand_method@plt>
mov    %eax,-0x9c(%rbp)
cmpl   $0x1,-0x9c(%rbp)
je     11f8 <test_rand+0x88>
lea    0xe50(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <test_rand+0xdc>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xe42(%rip),%rdi
mov    $0x4,%esi
call   1030 <RAND_seed@plt>
lea    -0x90(%rbp),%rdi
mov    $0x80,%esi
call   1040 <RAND_bytes@plt>
lea    -0x90(%rbp),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x80,%edx
call   1080 <hexdump@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_rand>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%rbx
lea    0xe8f(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1070 <ENGINE_get_RAND@plt>
test   %rax,%rax
je     11eb <test_rand+0x7b>
mov    %rax,%rdi
call   1090 <RAND_set_rand_method@plt>
cmp    $0x1,%eax
jne    11f4 <test_rand+0x84>
mov    %rsp,%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xe42(%rip),%rdi
mov    $0x4,%esi
call   1030 <RAND_seed@plt>
mov    %r14,%rdi
mov    $0x80,%esi
call   1050 <RAND_bytes@plt>
lea    0xe29(%rip),%rdi
mov    %r14,%rsi
mov    $0x80,%edx
call   1080 <hexdump@plt>
jmp    1205 <test_rand+0x95>
lea    0xe3a(%rip),%rdi
jmp    11fb <test_rand+0x8b>
lea    0xe4d(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Testing Random Generator\0A\00"
@.str.1 = [29 x i8] c"Failed to get random method\0A\00"
@.str.2 = [29 x i8] c"Failed to set random method\0A\00"
@.str.3 = [5 x i8] c"test\00"
@.str.4 = [14 x i8] c"Random Buffer\00"
define i32 @test_rand(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [128 x i8]
%5 = alloca i32*
%6 = alloca i32
store i32* %0, i32** %3
%7 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%8 = load i32*, i32** %3
%9 = call i32* @ENGINE_get_RAND(i32* %8)
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %29
14: ; preds = %1
%15 = load i32*, i32** %5
%16 = call i32 @RAND_set_rand_method(i32* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %29
21: ; preds = %14
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i32 0, i32 128)
%24 = call i32 @RAND_seed(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%26 = call i32 @RAND_bytes(i8* %25, i32 128)
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%28 = call i32 @hexdump(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %27, i32 128)
store i32 0, i32* %2
br label %29
29: ; preds = %21, %19, %12
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str.3 = [5 x i8] c"test\00"
@.str.4 = [14 x i8] c"Random Buffer\00"
@str = [25 x i8] c"Testing Random Generator\00"
@str.5 = [28 x i8] c"Failed to get random method\00"
@str.6 = [28 x i8] c"Failed to set random method\00"
define i32 @test_rand(i32* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%5 = tail call i32* @ENGINE_get_RAND(i32* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.5, i64 0, i64 0))
br label %19
9: ; preds = %1
%10 = tail call i32 @RAND_set_rand_method(i32* %5)
%11 = icmp eq i32 %10, 1
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
br label %19
14: ; preds = %9
%15 = call i32 @memset(i8* %3, i32 0, i32 128)
%16 = call i32 @RAND_seed(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%17 = call i32 @RAND_bytes(i8* %3, i32 128)
%18 = call i32 @hexdump(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 128)
br label %19
19: ; preds = %14, %12, %7
%20 = phi i32 [ -1, %12 ], [ 0, %14 ], [ -1, %7 ]
ret i32 %20
}