#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int secp256k1_rand32();
void secp256k1_rand256_test(unsigned char *b32) {
  int bits = 0;
  memset(b32, 0, 32);
  while (bits < 256) {
    uint32_t ent = secp256k1_rand32();
    int now = 1 + ((ent % 64) * ((ent >> 6) % 32) + 16) / 31;
    uint32_t val = 1 & (ent >> 11);
    while (now > 0 && bits < 256) {
      b32[bits / 8] |= val << (bits % 8);
      now--;
      bits++;
    }
  }
}


x86 assembly (clang, -O0):
<secp256k1_rand256_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
cmpl   $0x100,-0xc(%rbp)
jge    1218 <secp256k1_rand256_test+0xf8>
mov    $0x0,%al
call   1040 <secp256k1_rand32@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x40,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
sar    $0x6,%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    -0x1c(%rbp),%eax
imul   %edx,%eax
add    $0x10,%eax
mov    $0x1f,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
sar    $0xb,%eax
and    $0x1,%eax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x1d(%rbp)
jle    11b5 <secp256k1_rand256_test+0x95>
cmpl   $0x100,-0xc(%rbp)
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11c5 <secp256k1_rand256_test+0xa5>
jmp    1213 <secp256k1_rand256_test+0xf3>
mov    -0x18(%rbp),%esi
mov    -0xc(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
shl    %cl,%esi
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1199 <secp256k1_rand256_test+0x79>
jmp    1143 <secp256k1_rand256_test+0x23>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secp256k1_rand256_test>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
xor    %ebx,%ebx
jmp    1150 <secp256k1_rand256_test+0x30>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%ecx
mov    %ecx,%ebx
cmp    $0xff,%ecx
jg     11e9 <secp256k1_rand256_test+0xc9>
xor    %eax,%eax
call   1040 <secp256k1_rand32@plt>
lea    0x3f(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffc0,%ecx
mov    %eax,%edx
sub    %ecx,%edx
mov    %eax,%ecx
sar    $0x6,%ecx
lea    0x1f(%rcx),%esi
test   %ecx,%ecx
cmovns %ecx,%esi
and    $0xffe0,%esi
sub    %esi,%ecx
imul   %edx,%ecx
cmp    $0xffd2,%cx
jl     1140 <secp256k1_rand256_test+0x20>
shr    $0xb,%eax
and    $0x1,%eax
add    $0x10,%ecx
movswl %cx,%ecx
imul   $0x4211,%ecx,%ecx
mov    %ecx,%edx
sar    $0x13,%edx
shr    $0x1f,%ecx
add    %edx,%ecx
add    $0x1,%ecx
movzwl %cx,%edx
nopl   0x0(%rax)
mov    %ebx,%ecx
and    $0x7,%cl
mov    %eax,%esi
shl    %cl,%esi
lea    0x7(%rbx),%ecx
test   %ebx,%ebx
cmovns %ebx,%ecx
sar    $0x3,%ecx
movslq %ecx,%rcx
or     %sil,(%r14,%rcx,1)
lea    0x1(%rbx),%ecx
cmp    $0x2,%edx
jb     1142 <secp256k1_rand256_test+0x22>
add    $0xffffffff,%edx
cmp    $0xff,%ebx
mov    %ecx,%ebx
jl     11b0 <secp256k1_rand256_test+0x90>
jmp    1142 <secp256k1_rand256_test+0x22>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @secp256k1_rand256_test(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
%7 = load i8*, i8** %2
%8 = call i32 @memset(i8* %7, i32 0, i32 32)
br label %9
9: ; preds = %52, %1
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 256
br i1 %11, label %12, label %53
12: ; preds = %9
%13 = call i32 (...) @secp256k1_rand32()
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = srem i32 %14, 64
%16 = load i32, i32* %4
%17 = ashr i32 %16, 6
%18 = srem i32 %17, 32
%19 = mul nsw i32 %15, %18
%20 = add nsw i32 %19, 16
%21 = sdiv i32 %20, 31
%22 = add nsw i32 1, %21
store i32 %22, i32* %5
%23 = load i32, i32* %4
%24 = ashr i32 %23, 11
%25 = and i32 1, %24
store i32 %25, i32* %6
br label %26
26: ; preds = %34, %12
%27 = load i32, i32* %5
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 256
br label %32
32: ; preds = %29, %26
%33 = phi i1 [ false, %26 ], [ %31, %29 ]
br i1 %33, label %34, label %52
34: ; preds = %32
%35 = load i32, i32* %6
%36 = load i32, i32* %3
%37 = srem i32 %36, 8
%38 = shl i32 %35, %37
%39 = load i8*, i8** %2
%40 = load i32, i32* %3
%41 = sdiv i32 %40, 8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = or i32 %45, %38
%47 = trunc i32 %46 to i8
store i8 %47, i8* %43
%48 = load i32, i32* %5
%49 = add nsw i32 %48, -1
store i32 %49, i32* %5
%50 = load i32, i32* %3
%51 = add nsw i32 %50, 1
store i32 %51, i32* %3
br label %26
52: ; preds = %32
br label %9
53: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @secp256k1_rand256_test(i8* %0) {
%2 = tail call i32 @memset(i8* %0, i32 0, i32 32)
br label %6
3: ; preds = %22, %6
%4 = phi i32 [ %7, %6 ], [ %34, %22 ]
%5 = icmp slt i32 %4, 256
br i1 %5, label %6, label %38
6: ; preds = %1, %3
%7 = phi i32 [ 0, %1 ], [ %4, %3 ]
%8 = tail call i32 (...) @secp256k1_rand32()
%9 = srem i32 %8, 64
%10 = ashr i32 %8, 6
%11 = srem i32 %10, 32
%12 = mul nsw i32 %11, %9
%13 = trunc i32 %12 to i16
%14 = lshr i32 %8, 11
%15 = and i32 %14, 1
%16 = icmp sgt i16 %13, -47
br i1 %16, label %17, label %3
17: ; preds = %6
%18 = add nsw i16 %13, 16
%19 = sdiv i16 %18, 31
%20 = add nuw nsw i16 %19, 1
%21 = zext i16 %20 to i32
br label %22
22: ; preds = %17, %22
%23 = phi i32 [ %34, %22 ], [ %7, %17 ]
%24 = phi i32 [ %33, %22 ], [ %21, %17 ]
%25 = and i32 %23, 7
%26 = shl i32 %15, %25
%27 = sdiv i32 %23, 8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %0, i64 %28
%30 = load i8, i8* %29
%31 = trunc i32 %26 to i8
%32 = or i8 %30, %31
store i8 %32, i8* %29
%33 = add nsw i32 %24, -1
%34 = add nsw i32 %23, 1
%35 = icmp ugt i32 %24, 1
%36 = icmp slt i32 %23, 255
%37 = select i1 %35, i1 %36, i1 false
br i1 %37, label %22, label %3
38: ; preds = %3
ret void
}