#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 size_t uint64_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(size_t *, size_t);
size_t count_bits8(size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(size_t *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ randombuff(int /*<<< orphan*/ *, unsigned char *, size_t);
int /*<<< orphan*/ stderr;
void lwe_sample_n_binomial24(uint16_t *s, const size_t n) {
  size_t rndlen = 3 * n;
  if (rndlen % 8 != 0) {
    rndlen += 8 - (rndlen % 8);
  }
  uint64_t *rnd = (uint64_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);
  uint64_t *ptr_rnd = rnd;
  size_t i, j;
  for (i = 0; i < n; i += 8) {
    uint64_t sum = count_bits8(ptr_rnd[0]) + count_bits8(ptr_rnd[1]) +
                   count_bits8(ptr_rnd[2]);
    size_t bound = i + 8 < n ? 8 : n - i;
    for (j = 0; j < bound; j++) {
      s[i + j] = (uint16_t)((sum >> (j * 8)) & 0xFF) - 12;
    }
    ptr_rnd += 3;
  }
  if (aes_ctx) {
    EVP_CIPHER_CTX_free(aes_ctx);
  }
  bzero(rnd, rndlen);
  free(rnd);
}


x86 assembly (clang, -O0):
<lwe_sample_n_binomial24>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
imul   $0x3,-0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
je     11c3 <lwe_sample_n_binomial24+0x43>
mov    -0x18(%rbp),%rcx
and    $0x7,%rcx
mov    $0x8,%eax
sub    %rcx,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f5 <lwe_sample_n_binomial24+0x75>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1341 <lwe_sample_n_binomial24+0x1c1>
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   1090 <randombuff@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1317 <lwe_sample_n_binomial24+0x197>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1050 <count_bits8@plt>
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <count_bits8@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <count_bits8@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
add    $0x8,%rax
cmp    -0x10(%rbp),%rax
jae    1295 <lwe_sample_n_binomial24+0x115>
mov    $0x8,%eax
mov    %rax,-0x68(%rbp)
jmp    12a1 <lwe_sample_n_binomial24+0x121>
mov    -0x10(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x50(%rbp),%rax
jae    12fa <lwe_sample_n_binomial24+0x17a>
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rcx
shl    $0x3,%rcx
shr    %cl,%rdx
and    $0xff,%rdx
sub    $0xc,%rdx
mov    -0x8(%rbp),%rax
mov    -0x38(%rbp),%rcx
add    -0x40(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    12b1 <lwe_sample_n_binomial24+0x131>
mov    -0x30(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
jmp    1221 <lwe_sample_n_binomial24+0xa1>
cmpq   $0x0,-0x28(%rbp)
je     132b <lwe_sample_n_binomial24+0x1ab>
mov    -0x28(%rbp),%rdi
call   1060 <EVP_CIPHER_CTX_free@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <bzero@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lwe_sample_n_binomial24>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
lea    (%rsi,%rsi,2),%rax
mov    %eax,%ecx
and    $0x7,%ecx
mov    $0x8,%ebp
sub    %rcx,%rbp
test   %rcx,%rcx
cmove  %rcx,%rbp
add    %rax,%rbp
mov    %rbp,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     141b <lwe_sample_n_binomial24+0x29b>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <aes_ctx_create@plt>
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
mov    %r15,(%rsp)
mov    %r15,%rsi
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdx
call   1090 <randombuff@plt>
test   %r14,%r14
je     13e5 <lwe_sample_n_binomial24+0x265>
lea    0x10(%r13),%r12
xor    %r8d,%r8d
mov    $0x8,%r15d
xor    %edi,%edi
xor    %r9d,%r9d
mov    (%rsp),%rax
mov    %r14,0x18(%rsp)
jmp    123b <lwe_sample_n_binomial24+0xbb>
nopl   0x0(%rax)
mov    0x48(%rsp),%rax
add    $0x18,%rax
add    $0x1,%rdi
add    $0x40,%r12
mov    0x50(%rsp),%r15
add    $0x8,%r15
add    $0xfffffffffffffff8,%r8
add    $0x40,%r13
cmp    %r14,%r9
jae    13e5 <lwe_sample_n_binomial24+0x265>
mov    %r13,0x30(%rsp)
mov    %r8,0x38(%rsp)
mov    %rdi,0x40(%rsp)
mov    %r9,0x28(%rsp)
cmp    %r15,%r14
mov    %r15,0x50(%rsp)
cmovb  %r14,%r15
lea    0x8(%r9),%r13
cmp    %r13,%r14
mov    %r13,%rbx
cmovb  %r14,%rbx
mov    (%rax),%rdi
mov    %rax,%r14
call   1050 <count_bits8@plt>
mov    %rax,0x20(%rsp)
mov    0x8(%r14),%rdi
call   1050 <count_bits8@plt>
mov    %rax,%rbp
mov    %r14,0x48(%rsp)
mov    0x10(%r14),%rdi
call   1050 <count_bits8@plt>
mov    %r13,%r9
mov    0x30(%rsp),%r13
mov    0x40(%rsp),%rdi
movdqa 0xd97(%rip),%xmm9
movdqa 0xd7e(%rip),%xmm8
movapd 0xd66(%rip),%xmm7
movdqa 0xd4e(%rip),%xmm6
mov    0x38(%rsp),%r8
mov    0x18(%rsp),%r14
mov    %r14,%rdx
sub    0x28(%rsp),%rdx
cmp    %r14,%r9
mov    $0x8,%ecx
cmovb  %rcx,%rdx
test   %rdx,%rdx
je     1210 <lwe_sample_n_binomial24+0x90>
lea    0x0(,%rdi,8),%rcx
sub    %rcx,%rbx
add    0x20(%rsp),%rbp
add    %rax,%rbp
cmp    $0x4,%rbx
jae    1310 <lwe_sample_n_binomial24+0x190>
xor    %eax,%eax
jmp    13b5 <lwe_sample_n_binomial24+0x235>
nopl   0x0(%rax)
add    %r8,%r15
and    $0xfffffffffffffffc,%r15
mov    %rbx,%rax
and    $0xfffffffffffffffc,%rax
movq   %rbp,%xmm0
pshufd $0x44,%xmm0,%xmm0
xor    %ecx,%ecx
movdqa 0xcce(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movdqa %xmm1,%xmm2
psllq  $0x3,%xmm2
movdqa %xmm0,%xmm3
psrlq  %xmm2,%xmm3
pshufd $0xee,%xmm2,%xmm4
paddq  %xmm6,%xmm2
movdqa %xmm0,%xmm5
psrlq  %xmm4,%xmm5
movsd  %xmm3,%xmm5
movdqa %xmm0,%xmm3
psrlq  %xmm2,%xmm3
pshufd $0xee,%xmm2,%xmm2
movdqa %xmm0,%xmm4
psrlq  %xmm2,%xmm4
movsd  %xmm3,%xmm4
andpd  %xmm7,%xmm5
andpd  %xmm7,%xmm4
paddq  %xmm8,%xmm5
paddq  %xmm8,%xmm4
movdqu %xmm5,-0x10(%r12,%rcx,8)
movdqu %xmm4,(%r12,%rcx,8)
add    $0x4,%rcx
paddq  %xmm9,%xmm1
cmp    %rcx,%r15
jne    1340 <lwe_sample_n_binomial24+0x1c0>
cmp    %rax,%rbx
je     1210 <lwe_sample_n_binomial24+0x90>
lea    0x0(,%rax,8),%rcx
nopl   (%rax)
mov    %rbp,%rsi
shr    %cl,%rsi
movzbl %sil,%esi
add    $0xfffffffffffffff4,%rsi
mov    %rsi,0x0(%r13,%rax,8)
add    $0x1,%rax
add    $0x8,%rcx
cmp    %rdx,%rax
jb     13c0 <lwe_sample_n_binomial24+0x240>
jmp    1210 <lwe_sample_n_binomial24+0x90>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     13f4 <lwe_sample_n_binomial24+0x274>
call   1060 <EVP_CIPHER_CTX_free@plt>
mov    (%rsp),%rbx
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
call   10a0 <bzero@plt>
mov    %rbx,%rdi
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    0x2bbe(%rip),%rax
mov    (%rax),%edi
lea    0xc25(%rip),%rsi
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"malloc failure\0A\00"
define void @lwe_sample_n_binomial24(i64* %0, i64 %1) {
%3 = alloca i64*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i32*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i64* %0, i64** %3
store i64 %1, i64* %4
%13 = load i64, i64* %4
%14 = mul i64 3, %13
store i64 %14, i64* %5
%15 = load i64, i64* %5
%16 = urem i64 %15, 8
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i64, i64* %5
%20 = urem i64 %19, 8
%21 = sub i64 8, %20
%22 = load i64, i64* %5
%23 = add i64 %22, %21
store i64 %23, i64* %5
br label %24
24: ; preds = %18, %2
%25 = load i64, i64* %5
%26 = call i64 @malloc(i64 %25)
%27 = inttoptr i64 %26 to i64*
store i64* %27, i64** %6
%28 = load i64*, i64** %6
%29 = icmp eq i64* %28, null
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %108
33: ; preds = %24
%34 = call i32* (...) @aes_ctx_create()
store i32* %34, i32** %7
%35 = load i32*, i32** %7
%36 = load i64*, i64** %6
%37 = bitcast i64* %36 to i8*
%38 = load i64, i64* %5
%39 = call i32 @randombuff(i32* %35, i8* %37, i64 %38)
%40 = load i64*, i64** %6
store i64* %40, i64** %8
store i64 0, i64* %9
br label %41
41: ; preds = %93, %33
%42 = load i64, i64* %9
%43 = load i64, i64* %4
%44 = icmp ult i64 %42, %43
br i1 %44, label %45, label %96
45: ; preds = %41
%46 = load i64*, i64** %8
%47 = getelementptr inbounds i64, i64* %46, i64 0
%48 = load i64, i64* %47
%49 = call i64 @count_bits8(i64 %48)
%50 = load i64*, i64** %8
%51 = getelementptr inbounds i64, i64* %50, i64 1
%52 = load i64, i64* %51
%53 = call i64 @count_bits8(i64 %52)
%54 = add i64 %49, %53
%55 = load i64*, i64** %8
%56 = getelementptr inbounds i64, i64* %55, i64 2
%57 = load i64, i64* %56
%58 = call i64 @count_bits8(i64 %57)
%59 = add i64 %54, %58
store i64 %59, i64* %11
%60 = load i64, i64* %9
%61 = add i64 %60, 8
%62 = load i64, i64* %4
%63 = icmp ult i64 %61, %62
br i1 %63, label %64, label %65
64: ; preds = %45
br label %69
65: ; preds = %45
%66 = load i64, i64* %4
%67 = load i64, i64* %9
%68 = sub i64 %66, %67
br label %69
69: ; preds = %65, %64
%70 = phi i64 [ 8, %64 ], [ %68, %65 ]
store i64 %70, i64* %12
store i64 0, i64* %10
br label %71
71: ; preds = %87, %69
%72 = load i64, i64* %10
%73 = load i64, i64* %12
%74 = icmp ult i64 %72, %73
br i1 %74, label %75, label %90
75: ; preds = %71
%76 = load i64, i64* %11
%77 = load i64, i64* %10
%78 = mul i64 %77, 8
%79 = lshr i64 %76, %78
%80 = and i64 %79, 255
%81 = sub nsw i64 %80, 12
%82 = load i64*, i64** %3
%83 = load i64, i64* %9
%84 = load i64, i64* %10
%85 = add i64 %83, %84
%86 = getelementptr inbounds i64, i64* %82, i64 %85
store i64 %81, i64* %86
br label %87
87: ; preds = %75
%88 = load i64, i64* %10
%89 = add i64 %88, 1
store i64 %89, i64* %10
br label %71
90: ; preds = %71
%91 = load i64*, i64** %8
%92 = getelementptr inbounds i64, i64* %91, i64 3
store i64* %92, i64** %8
br label %93
93: ; preds = %90
%94 = load i64, i64* %9
%95 = add i64 %94, 8
store i64 %95, i64* %9
br label %41
96: ; preds = %41
%97 = load i32*, i32** %7
%98 = icmp ne i32* %97, null
br i1 %98, label %99, label %102
99: ; preds = %96
%100 = load i32*, i32** %7
%101 = call i32 @EVP_CIPHER_CTX_free(i32* %100)
br label %102
102: ; preds = %99, %96
%103 = load i64*, i64** %6
%104 = load i64, i64* %5
%105 = call i32 @bzero(i64* %103, i64 %104)
%106 = load i64*, i64** %6
%107 = call i32 @free(i64* %106)
br label %108
108: ; preds = %102, %30
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"malloc failure\0A\00"
define void @lwe_sample_n_binomial24(i64* nocapture writeonly %0, i64 %1) {
%3 = mul i64 %1, 3
%4 = and i64 %3, 7
%5 = icmp eq i64 %4, 0
%6 = sub nuw nsw i64 8, %4
%7 = select i1 %5, i64 0, i64 %6
%8 = add i64 %7, %3
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i64*
%11 = icmp eq i64 %9, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %95
15: ; preds = %2
%16 = tail call i32* (...) @aes_ctx_create()
%17 = inttoptr i64 %9 to i8*
%18 = tail call i32 @randombuff(i32* %16, i8* %17, i64 %8)
%19 = icmp eq i64 %1, 0
br i1 %19, label %88, label %20
20: ; preds = %15, %85
%21 = phi i64 [ %87, %85 ], [ 0, %15 ]
%22 = phi i64 [ %38, %85 ], [ 0, %15 ]
%23 = phi i64* [ %86, %85 ], [ %10, %15 ]
%24 = add i64 %22, 8
%25 = call i64 @llvm.umin.i64(i64 %1, i64 %24)
%26 = mul i64 %21, -8
%27 = add i64 %25, %26
%28 = load i64, i64* %23
%29 = tail call i64 @count_bits8(i64 %28)
%30 = getelementptr inbounds i64, i64* %23, i64 1
%31 = load i64, i64* %30
%32 = tail call i64 @count_bits8(i64 %31)
%33 = add i64 %32, %29
%34 = getelementptr inbounds i64, i64* %23, i64 2
%35 = load i64, i64* %34
%36 = tail call i64 @count_bits8(i64 %35)
%37 = add i64 %33, %36
%38 = add i64 %22, 8
%39 = icmp ult i64 %38, %1
%40 = sub i64 %1, %22
%41 = select i1 %39, i64 8, i64 %40
%42 = icmp eq i64 %41, 0
br i1 %42, label %85, label %43
43: ; preds = %20
%44 = icmp ult i64 %27, 4
br i1 %44, label %73, label %45
45: ; preds = %43
%46 = and i64 %27, -4
%47 = insertelement <2 x i64> poison, i64 %37, i64 0
%48 = shufflevector <2 x i64> %47, <2 x i64> poison, <2 x i32> zeroinitializer
%49 = insertelement <2 x i64> poison, i64 %37, i64 0
%50 = shufflevector <2 x i64> %49, <2 x i64> poison, <2 x i32> zeroinitializer
br label %51
51: ; preds = %51, %45
%52 = phi i64 [ 0, %45 ], [ %68, %51 ]
%53 = phi <2 x i64> [ <i64 0, i64 1>, %45 ], [ %69, %51 ]
%54 = shl <2 x i64> %53, <i64 3, i64 3>
%55 = shl <2 x i64> %53, <i64 3, i64 3>
%56 = add <2 x i64> %55, <i64 16, i64 16>
%57 = lshr <2 x i64> %48, %54
%58 = lshr <2 x i64> %50, %56
%59 = and <2 x i64> %57, <i64 255, i64 255>
%60 = and <2 x i64> %58, <i64 255, i64 255>
%61 = add nsw <2 x i64> %59, <i64 -12, i64 -12>
%62 = add nsw <2 x i64> %60, <i64 -12, i64 -12>
%63 = add i64 %52, %22
%64 = getelementptr inbounds i64, i64* %0, i64 %63
%65 = bitcast i64* %64 to <2 x i64>*
store <2 x i64> %61, <2 x i64>* %65
%66 = getelementptr inbounds i64, i64* %64, i64 2
%67 = bitcast i64* %66 to <2 x i64>*
store <2 x i64> %62, <2 x i64>* %67
%68 = add nuw i64 %52, 4
%69 = add <2 x i64> %53, <i64 4, i64 4>
%70 = icmp eq i64 %68, %46
br i1 %70, label %71, label %51
71: ; preds = %51
%72 = icmp eq i64 %27, %46
br i1 %72, label %85, label %73
73: ; preds = %43, %71
%74 = phi i64 [ 0, %43 ], [ %46, %71 ]
br label %75
75: ; preds = %73, %75
%76 = phi i64 [ %83, %75 ], [ %74, %73 ]
%77 = shl i64 %76, 3
%78 = lshr i64 %37, %77
%79 = and i64 %78, 255
%80 = add nsw i64 %79, -12
%81 = add i64 %76, %22
%82 = getelementptr inbounds i64, i64* %0, i64 %81
store i64 %80, i64* %82
%83 = add nuw i64 %76, 1
%84 = icmp ult i64 %83, %41
br i1 %84, label %75, label %85
85: ; preds = %75, %71, %20
%86 = getelementptr inbounds i64, i64* %23, i64 3
%87 = add i64 %21, 1
br i1 %39, label %20, label %88
88: ; preds = %85, %15
%89 = icmp eq i32* %16, null
br i1 %89, label %92, label %90
90: ; preds = %88
%91 = tail call i32 @EVP_CIPHER_CTX_free(i32* %16)
br label %92
92: ; preds = %90, %88
%93 = tail call i32 @bzero(i64* %10, i64 %8)
%94 = tail call i32 @free(i64* %10)
br label %95
95: ; preds = %92, %12
ret void
}