#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 */
struct symbols_lst {
  int dummy;
};
typedef int /*<<< orphan*/ BN_CTX;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_new();
int BN_kronecker(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                 int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_inverse(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ const *,
                                  int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_num_bits(int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_rand(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                           int /*<<< orphan*/);
int /*<<< orphan*/ put_jacobi(struct symbols_lst *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/ *, int);
int precalc_jacobis(struct symbols_lst *jacobis, const BIGNUM *N, int num) {
  int kp, km;
  BN_CTX *ctx = BN_CTX_new();
  BIGNUM *t, *it;
  kp = km = num;
  while (km > 0 || kp > 0) {
    t = BN_new();
    it = BN_new();
    BN_rand(t, BN_num_bits(N), -1, 0);
    BN_mod_inverse(it, t, N, ctx);
    if (1 == BN_kronecker(t, N, ctx)) {
      kp--;
      put_jacobi(jacobis, t, it, 1);
    } else {
      km--;
      put_jacobi(jacobis, t, it, -1);
    }
  }
  BN_CTX_free(ctx);
  return 0;
}


x86 assembly (clang, -O0):
<precalc_jacobis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1090 <BN_CTX_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    %eax,-0x18(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x39(%rbp)
jg     11c0 <precalc_jacobis+0x40>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11d0 <precalc_jacobis+0x50>
jmp    1286 <precalc_jacobis+0x106>
mov    $0x0,%al
call   1060 <BN_new@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <BN_new@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <BN_num_bits@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1040 <BN_rand@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <BN_mod_inverse@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <BN_kronecker@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1262 <precalc_jacobis+0xe2>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    $0x1,%ecx
call   10a0 <put_jacobi@plt>
jmp    1281 <precalc_jacobis+0x101>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    $0xffffffff,%ecx
call   10a0 <put_jacobi@plt>
jmp    11a7 <precalc_jacobis+0x27>
mov    -0x28(%rbp),%rdi
call   1080 <BN_CTX_free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<precalc_jacobis>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r13
mov    %rdi,(%rsp)
xor    %eax,%eax
call   1090 <BN_CTX_new@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    122f <precalc_jacobis+0xaf>
mov    %r14d,%r12d
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <BN_new@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1060 <BN_new@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1070 <BN_num_bits@plt>
mov    %rbp,%rdi
mov    %eax,%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1040 <BN_rand@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
call   1050 <BN_mod_inverse@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1030 <BN_kronecker@plt>
xor    %edx,%edx
xor    %esi,%esi
cmp    $0x1,%eax
setne  %dl
sete   %sil
lea    (%rsi,%rsi,1),%ecx
add    $0xffffffff,%ecx
sub    %edx,%r12d
sub    %esi,%r14d
mov    (%rsp),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   10a0 <put_jacobi@plt>
test   %r12d,%r12d
jg     11b0 <precalc_jacobis+0x30>
test   %r14d,%r14d
jg     11b0 <precalc_jacobis+0x30>
mov    %r15,%rdi
call   1080 <BN_CTX_free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.symbols_lst = type { i32 }
define i32 @precalc_jacobis(%struct.symbols_lst* %0, i32* %1, i32 %2) {
%4 = alloca %struct.symbols_lst*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
store %struct.symbols_lst* %0, %struct.symbols_lst** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%12 = call i32* (...) @BN_CTX_new()
store i32* %12, i32** %9
%13 = load i32, i32* %6
store i32 %13, i32* %8
store i32 %13, i32* %7
br label %14
14: ; preds = %53, %3
%15 = load i32, i32* %8
%16 = icmp sgt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* %7
%19 = icmp sgt i32 %18, 0
br label %20
20: ; preds = %17, %14
%21 = phi i1 [ true, %14 ], [ %19, %17 ]
br i1 %21, label %22, label %54
22: ; preds = %20
%23 = call i32* (...) @BN_new()
store i32* %23, i32** %10
%24 = call i32* (...) @BN_new()
store i32* %24, i32** %11
%25 = load i32*, i32** %10
%26 = load i32*, i32** %5
%27 = call i32 @BN_num_bits(i32* %26)
%28 = call i32 @BN_rand(i32* %25, i32 %27, i32 -1, i32 0)
%29 = load i32*, i32** %11
%30 = load i32*, i32** %10
%31 = load i32*, i32** %5
%32 = load i32*, i32** %9
%33 = call i32 @BN_mod_inverse(i32* %29, i32* %30, i32* %31, i32* %32)
%34 = load i32*, i32** %10
%35 = load i32*, i32** %5
%36 = load i32*, i32** %9
%37 = call i32 @BN_kronecker(i32* %34, i32* %35, i32* %36)
%38 = icmp eq i32 1, %37
br i1 %38, label %39, label %46
39: ; preds = %22
%40 = load i32, i32* %7
%41 = add nsw i32 %40, -1
store i32 %41, i32* %7
%42 = load %struct.symbols_lst*, %struct.symbols_lst** %4
%43 = load i32*, i32** %10
%44 = load i32*, i32** %11
%45 = call i32 @put_jacobi(%struct.symbols_lst* %42, i32* %43, i32* %44, i32 1)
br label %53
46: ; preds = %22
%47 = load i32, i32* %8
%48 = add nsw i32 %47, -1
store i32 %48, i32* %8
%49 = load %struct.symbols_lst*, %struct.symbols_lst** %4
%50 = load i32*, i32** %10
%51 = load i32*, i32** %11
%52 = call i32 @put_jacobi(%struct.symbols_lst* %49, i32* %50, i32* %51, i32 -1)
br label %53
53: ; preds = %46, %39
br label %14
54: ; preds = %20
%55 = load i32*, i32** %9
%56 = call i32 @BN_CTX_free(i32* %55)
ret i32 0
}

LLVM IR (-O2):
%struct.symbols_lst = type { i32 }
define i32 @precalc_jacobis(%struct.symbols_lst* %0, i32* %1, i32 %2) {
%4 = tail call i32* (...) @BN_CTX_new()
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %26
6: ; preds = %3, %6
%7 = phi i32 [ %21, %6 ], [ %2, %3 ]
%8 = phi i32 [ %19, %6 ], [ %2, %3 ]
%9 = tail call i32* (...) @BN_new()
%10 = tail call i32* (...) @BN_new()
%11 = tail call i32 @BN_num_bits(i32* %1)
%12 = tail call i32 @BN_rand(i32* %9, i32 %11, i32 -1, i32 0)
%13 = tail call i32 @BN_mod_inverse(i32* %10, i32* %9, i32* %1, i32* %4)
%14 = tail call i32 @BN_kronecker(i32* %9, i32* %1, i32* %4)
%15 = icmp eq i32 %14, 1
%16 = select i1 %15, i32 1, i32 -1
%17 = xor i1 %15, true
%18 = sext i1 %17 to i32
%19 = add nsw i32 %8, %18
%20 = sext i1 %15 to i32
%21 = add nsw i32 %7, %20
%22 = tail call i32 @put_jacobi(%struct.symbols_lst* %0, i32* %9, i32* %10, i32 %16)
%23 = icmp sgt i32 %19, 0
%24 = icmp sgt i32 %21, 0
%25 = select i1 %23, i1 true, i1 %24
br i1 %25, label %6, label %26
26: ; preds = %6, %3
%27 = tail call i32 @BN_CTX_free(i32* %4)
ret i32 0
}