#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*/ uint32_t;
typedef scalar_t__ uint16_t;
typedef int /*<<< orphan*/ EVP_CIPHER_CTX;

/* Variables and functions */
int /*<<< orphan*/ EVP_CIPHER_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *aes_ctx_create();
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, size_t);
scalar_t__ count_bits32(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ randombuff(int /*<<< orphan*/ *, unsigned char *, size_t);
int /*<<< orphan*/ stderr;
void lwe_sample_n_binomial32(uint16_t *s, const size_t n) {
  size_t rndlen = 4 * n;
  uint32_t *rnd = (uint32_t *)malloc(rndlen);
  if (rnd == NULL) {
    fprintf(stderr, "malloc failure\n");
    return;
  }
  EVP_CIPHER_CTX *aes_ctx = aes_ctx_create();
  randombuff(aes_ctx, (unsigned char *)rnd, rndlen);
  if (aes_ctx) {
    EVP_CIPHER_CTX_free(aes_ctx);
  }
  size_t i;
  for (i = 0; i < n; i++) {
    s[i] = count_bits32(rnd[i]) - 16;
  }
  bzero(rnd, rndlen);
  free(rnd);
}


x86 assembly (clang, -O0):
<lwe_sample_n_binomial32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ce <lwe_sample_n_binomial32+0x4e>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1060 <fprintf@plt>
jmp    125e <lwe_sample_n_binomial32+0xde>
mov    $0x0,%al
call   1040 <aes_ctx_create@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <randombuff@plt>
cmpq   $0x0,-0x28(%rbp)
je     11fe <lwe_sample_n_binomial32+0x7e>
mov    -0x28(%rbp),%rdi
call   1050 <EVP_CIPHER_CTX_free@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1248 <lwe_sample_n_binomial32+0xc8>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   10a0 <count_bits32@plt>
mov    %rax,%rdx
sub    $0x10,%rdx
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1206 <lwe_sample_n_binomial32+0x86>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <bzero@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lwe_sample_n_binomial32>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x0(,%rsi,4),%r14
mov    %r14,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     121b <lwe_sample_n_binomial32+0x9b>
mov    %rax,%rbp
xor    %eax,%eax
call   1040 <aes_ctx_create@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1080 <randombuff@plt>
test   %r13,%r13
je     11ce <lwe_sample_n_binomial32+0x4e>
mov    %r13,%rdi
call   1050 <EVP_CIPHER_CTX_free@plt>
test   %r15,%r15
je     11fa <lwe_sample_n_binomial32+0x7a>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%rbp,%rbx,4),%edi
call   10a0 <count_bits32@plt>
add    $0xfffffffffffffff0,%rax
mov    %rax,(%r12,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11e0 <lwe_sample_n_binomial32+0x60>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <bzero@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"malloc failure\0A\00"
define void @lwe_sample_n_binomial32(i64* %0, i64 %1) {
%3 = alloca i64*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
store i64* %0, i64** %3
store i64 %1, i64* %4
%9 = load i64, i64* %4
%10 = mul i64 4, %9
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %55
19: ; preds = %2
%20 = call i32* (...) @aes_ctx_create()
store i32* %20, i32** %7
%21 = load i32*, i32** %7
%22 = load i32*, i32** %6
%23 = bitcast i32* %22 to i8*
%24 = load i64, i64* %5
%25 = call i32 @randombuff(i32* %21, i8* %23, i64 %24)
%26 = load i32*, i32** %7
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = load i32*, i32** %7
%30 = call i32 @EVP_CIPHER_CTX_free(i32* %29)
br label %31
31: ; preds = %28, %19
store i64 0, i64* %8
br label %32
32: ; preds = %46, %31
%33 = load i64, i64* %8
%34 = load i64, i64* %4
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %49
36: ; preds = %32
%37 = load i32*, i32** %6
%38 = load i64, i64* %8
%39 = getelementptr inbounds i32, i32* %37, i64 %38
%40 = load i32, i32* %39
%41 = call i64 @count_bits32(i32 %40)
%42 = sub nsw i64 %41, 16
%43 = load i64*, i64** %3
%44 = load i64, i64* %8
%45 = getelementptr inbounds i64, i64* %43, i64 %44
store i64 %42, i64* %45
br label %46
46: ; preds = %36
%47 = load i64, i64* %8
%48 = add i64 %47, 1
store i64 %48, i64* %8
br label %32
49: ; preds = %32
%50 = load i32*, i32** %6
%51 = load i64, i64* %5
%52 = call i32 @bzero(i32* %50, i64 %51)
%53 = load i32*, i32** %6
%54 = call i32 @free(i32* %53)
br label %55
55: ; preds = %49, %16
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"malloc failure\0A\00"
define void @lwe_sample_n_binomial32(i64* nocapture writeonly %0, i64 %1) {
%3 = shl i64 %1, 2
%4 = tail call i64 @malloc(i64 %3)
%5 = inttoptr i64 %4 to i32*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %31
10: ; preds = %2
%11 = tail call i32* (...) @aes_ctx_create()
%12 = inttoptr i64 %4 to i8*
%13 = tail call i32 @randombuff(i32* %11, i8* %12, i64 %3)
%14 = icmp eq i32* %11, null
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = tail call i32 @EVP_CIPHER_CTX_free(i32* %11)
br label %17
17: ; preds = %15, %10
%18 = icmp eq i64 %1, 0
br i1 %18, label %28, label %19
19: ; preds = %17, %19
%20 = phi i64 [ %26, %19 ], [ 0, %17 ]
%21 = getelementptr inbounds i32, i32* %5, i64 %20
%22 = load i32, i32* %21
%23 = tail call i64 @count_bits32(i32 %22)
%24 = add nsw i64 %23, -16
%25 = getelementptr inbounds i64, i64* %0, i64 %20
store i64 %24, i64* %25
%26 = add nuw i64 %20, 1
%27 = icmp eq i64 %26, %1
br i1 %27, label %28, label %19
28: ; preds = %19, %17
%29 = tail call i32 @bzero(i32* %5, i64 %3)
%30 = tail call i32 @free(i32* %5)
br label %31
31: ; preds = %28, %7
ret void
}