#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*/ BN_CTX;
typedef int /*<<< orphan*/ const BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_bin2bn(unsigned char *, int, int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_gcd(int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
unsigned char BN_is_one(int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_mod_add(int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_inverse(int /*<<< orphan*/ const *,
                                  int /*<<< orphan*/ const *,
                                  int /*<<< orphan*/ const *,
                                  int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_mul(int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_sqr(int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_mod_sub(int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *,
                              int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ const *BN_new();
int /*<<< orphan*/ BN_num_bits(int /*<<< orphan*/ const *);
int /*<<< orphan*/ BN_rand(int /*<<< orphan*/ const *, int /*<<< orphan*/, int,
                           int /*<<< orphan*/);
int /*<<< orphan*/ BN_sub(int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *);
void rand_sol(BIGNUM *x, BIGNUM *y, const BIGNUM *R, const BIGNUM *s,
              const BIGNUM *S, const BIGNUM *N, BN_CTX *ctx) {
  BIGNUM *t = BN_new();
  BIGNUM *tsqr = BN_new();
  unsigned char found = 0;
  BIGNUM *a = BN_new();
  BIGNUM *b = BN_new();
  BIGNUM *c = BN_new();
  BIGNUM *two = BN_new();
  while (!found) {
    BN_rand(t, BN_num_bits(N), -1, 0);
    BN_mod_sqr(tsqr, t, N, ctx);
    BN_mod_mul(b, tsqr, S, N, ctx);
    BN_mod_add(a, R, b, N, ctx);
    BN_gcd(b, a, N, ctx);
    found = BN_is_one(b);
  }
  BN_mod_mul(b, s, a, N, ctx);
  BN_mod_inverse(a, b, N, ctx);
  BN_mod_mul(b, S, tsqr, N, ctx);
  BN_mod_sub(c, R, b, N, ctx);
  BN_mod_mul(y, c, a, N, ctx);
  BN_bin2bn((unsigned char *)"\x2", 1, two);
  BN_mod_mul(b, S, t, N, ctx);
  BN_mod_mul(c, two, b, N, ctx);
  BN_mod_mul(b, c, a, N, ctx);
  BN_sub(x, N, b);
  BN_free(t);
  BN_free(tsqr);
  BN_free(a);
  BN_free(b);
  BN_free(c);
  BN_free(two);
}


x86 assembly (clang, -O0):
<rand_sol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    $0x0,%al
call   1060 <BN_new@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1060 <BN_new@plt>
mov    %rax,-0x40(%rbp)
movb   $0x0,-0x41(%rbp)
mov    $0x0,%al
call   1060 <BN_new@plt>
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1060 <BN_new@plt>
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1060 <BN_new@plt>
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1060 <BN_new@plt>
mov    %rax,-0x68(%rbp)
cmpb   $0x0,-0x41(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1250 <rand_sol+0x80>
jmp    12e0 <rand_sol+0x110>
mov    -0x38(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <BN_num_bits@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1030 <BN_rand@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    0x10(%rbp),%rcx
call   10d0 <BN_mod_sqr@plt>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   10c0 <BN_mod_mul@plt>
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   10e0 <BN_mod_add@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    0x10(%rbp),%rcx
call   1050 <BN_gcd@plt>
mov    -0x58(%rbp),%rdi
call   10f0 <BN_is_one@plt>
mov    %al,-0x41(%rbp)
jmp    123a <rand_sol+0x6a>
mov    -0x58(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   10c0 <BN_mod_mul@plt>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    0x10(%rbp),%rcx
call   1040 <BN_mod_inverse@plt>
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   10c0 <BN_mod_mul@plt>
mov    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   1070 <BN_mod_sub@plt>
mov    -0x10(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   10c0 <BN_mod_mul@plt>
mov    -0x68(%rbp),%rdx
lea    0xc9c(%rip),%rdi
mov    $0x1,%esi
call   1090 <BN_bin2bn@plt>
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   10c0 <BN_mod_mul@plt>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   10c0 <BN_mod_mul@plt>
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
call   10c0 <BN_mod_mul@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x58(%rbp),%rdx
call   10a0 <BN_sub@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <BN_free@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <BN_free@plt>
mov    -0x50(%rbp),%rdi
call   10b0 <BN_free@plt>
mov    -0x58(%rbp),%rdi
call   10b0 <BN_free@plt>
mov    -0x60(%rbp),%rdi
call   10b0 <BN_free@plt>
mov    -0x68(%rbp),%rdi
call   10b0 <BN_free@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_sol>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,%rbx
mov    %r8,0x10(%rsp)
mov    %rcx,0x20(%rsp)
mov    %rdx,0x18(%rsp)
mov    %rsi,0x28(%rsp)
mov    %rdi,0x30(%rsp)
mov    0x70(%rsp),%r13
xor    %eax,%eax
call   1060 <BN_new@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1060 <BN_new@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1060 <BN_new@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1060 <BN_new@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1060 <BN_new@plt>
mov    %rax,(%rsp)
xor    %eax,%eax
call   1060 <BN_new@plt>
mov    %rax,0x8(%rsp)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1080 <BN_num_bits@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1030 <BN_rand@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r13,%rcx
call   10d0 <BN_mod_sqr@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    0x10(%rsp),%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   10c0 <BN_mod_mul@plt>
mov    %rbp,%rdi
mov    0x18(%rsp),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   10e0 <BN_mod_add@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1050 <BN_gcd@plt>
mov    %r14,%rdi
call   10f0 <BN_is_one@plt>
test   %al,%al
je     1240 <rand_sol+0x70>
mov    %r14,%rdi
mov    0x20(%rsp),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   10c0 <BN_mod_mul@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1040 <BN_mod_inverse@plt>
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
mov    %r12,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   10c0 <BN_mod_mul@plt>
mov    (%rsp),%rdi
mov    0x18(%rsp),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   1070 <BN_mod_sub@plt>
mov    0x28(%rsp),%rdi
mov    (%rsp),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   10c0 <BN_mod_mul@plt>
lea    0xcdb(%rip),%rdi
mov    $0x1,%esi
mov    0x8(%rsp),%rdx
call   1090 <BN_bin2bn@plt>
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   10c0 <BN_mod_mul@plt>
mov    (%rsp),%rdi
mov    0x8(%rsp),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   10c0 <BN_mod_mul@plt>
mov    %r14,%rdi
mov    (%rsp),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
call   10c0 <BN_mod_mul@plt>
mov    0x30(%rsp),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <BN_sub@plt>
mov    %r15,%rdi
call   10b0 <BN_free@plt>
mov    %r12,%rdi
call   10b0 <BN_free@plt>
mov    %rbp,%rdi
call   10b0 <BN_free@plt>
mov    %r14,%rdi
call   10b0 <BN_free@plt>
mov    (%rsp),%rdi
call   10b0 <BN_free@plt>
mov    0x8(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <BN_free@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"\02\00"
define void @rand_sol(i32* %0, i32* %1, i32* %2, i32* %3, i32* %4, i32* %5, i32* %6) {
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32*
%17 = alloca i8
%18 = alloca i32*
%19 = alloca i32*
%20 = alloca i32*
%21 = alloca i32*
store i32* %0, i32** %8
store i32* %1, i32** %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i32* %4, i32** %12
store i32* %5, i32** %13
store i32* %6, i32** %14
%22 = call i32* (...) @BN_new()
store i32* %22, i32** %15
%23 = call i32* (...) @BN_new()
store i32* %23, i32** %16
store i8 0, i8* %17
%24 = call i32* (...) @BN_new()
store i32* %24, i32** %18
%25 = call i32* (...) @BN_new()
store i32* %25, i32** %19
%26 = call i32* (...) @BN_new()
store i32* %26, i32** %20
%27 = call i32* (...) @BN_new()
store i32* %27, i32** %21
br label %28
28: ; preds = %32, %7
%29 = load i8, i8* %17
%30 = icmp ne i8 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %61
32: ; preds = %28
%33 = load i32*, i32** %15
%34 = load i32*, i32** %13
%35 = call i32 @BN_num_bits(i32* %34)
%36 = call i32 @BN_rand(i32* %33, i32 %35, i32 -1, i32 0)
%37 = load i32*, i32** %16
%38 = load i32*, i32** %15
%39 = load i32*, i32** %13
%40 = load i32*, i32** %14
%41 = call i32 @BN_mod_sqr(i32* %37, i32* %38, i32* %39, i32* %40)
%42 = load i32*, i32** %19
%43 = load i32*, i32** %16
%44 = load i32*, i32** %12
%45 = load i32*, i32** %13
%46 = load i32*, i32** %14
%47 = call i32 @BN_mod_mul(i32* %42, i32* %43, i32* %44, i32* %45, i32* %46)
%48 = load i32*, i32** %18
%49 = load i32*, i32** %10
%50 = load i32*, i32** %19
%51 = load i32*, i32** %13
%52 = load i32*, i32** %14
%53 = call i32 @BN_mod_add(i32* %48, i32* %49, i32* %50, i32* %51, i32* %52)
%54 = load i32*, i32** %19
%55 = load i32*, i32** %18
%56 = load i32*, i32** %13
%57 = load i32*, i32** %14
%58 = call i32 @BN_gcd(i32* %54, i32* %55, i32* %56, i32* %57)
%59 = load i32*, i32** %19
%60 = call zeroext i8 @BN_is_one(i32* %59)
store i8 %60, i8* %17
br label %28
61: ; preds = %28
%62 = load i32*, i32** %19
%63 = load i32*, i32** %11
%64 = load i32*, i32** %18
%65 = load i32*, i32** %13
%66 = load i32*, i32** %14
%67 = call i32 @BN_mod_mul(i32* %62, i32* %63, i32* %64, i32* %65, i32* %66)
%68 = load i32*, i32** %18
%69 = load i32*, i32** %19
%70 = load i32*, i32** %13
%71 = load i32*, i32** %14
%72 = call i32 @BN_mod_inverse(i32* %68, i32* %69, i32* %70, i32* %71)
%73 = load i32*, i32** %19
%74 = load i32*, i32** %12
%75 = load i32*, i32** %16
%76 = load i32*, i32** %13
%77 = load i32*, i32** %14
%78 = call i32 @BN_mod_mul(i32* %73, i32* %74, i32* %75, i32* %76, i32* %77)
%79 = load i32*, i32** %20
%80 = load i32*, i32** %10
%81 = load i32*, i32** %19
%82 = load i32*, i32** %13
%83 = load i32*, i32** %14
%84 = call i32 @BN_mod_sub(i32* %79, i32* %80, i32* %81, i32* %82, i32* %83)
%85 = load i32*, i32** %9
%86 = load i32*, i32** %20
%87 = load i32*, i32** %18
%88 = load i32*, i32** %13
%89 = load i32*, i32** %14
%90 = call i32 @BN_mod_mul(i32* %85, i32* %86, i32* %87, i32* %88, i32* %89)
%91 = load i32*, i32** %21
%92 = call i32 @BN_bin2bn(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1, i32* %91)
%93 = load i32*, i32** %19
%94 = load i32*, i32** %12
%95 = load i32*, i32** %15
%96 = load i32*, i32** %13
%97 = load i32*, i32** %14
%98 = call i32 @BN_mod_mul(i32* %93, i32* %94, i32* %95, i32* %96, i32* %97)
%99 = load i32*, i32** %20
%100 = load i32*, i32** %21
%101 = load i32*, i32** %19
%102 = load i32*, i32** %13
%103 = load i32*, i32** %14
%104 = call i32 @BN_mod_mul(i32* %99, i32* %100, i32* %101, i32* %102, i32* %103)
%105 = load i32*, i32** %19
%106 = load i32*, i32** %20
%107 = load i32*, i32** %18
%108 = load i32*, i32** %13
%109 = load i32*, i32** %14
%110 = call i32 @BN_mod_mul(i32* %105, i32* %106, i32* %107, i32* %108, i32* %109)
%111 = load i32*, i32** %8
%112 = load i32*, i32** %13
%113 = load i32*, i32** %19
%114 = call i32 @BN_sub(i32* %111, i32* %112, i32* %113)
%115 = load i32*, i32** %15
%116 = call i32 @BN_free(i32* %115)
%117 = load i32*, i32** %16
%118 = call i32 @BN_free(i32* %117)
%119 = load i32*, i32** %18
%120 = call i32 @BN_free(i32* %119)
%121 = load i32*, i32** %19
%122 = call i32 @BN_free(i32* %121)
%123 = load i32*, i32** %20
%124 = call i32 @BN_free(i32* %123)
%125 = load i32*, i32** %21
%126 = call i32 @BN_free(i32* %125)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"\02\00"
define void @rand_sol(i32* %0, i32* %1, i32* %2, i32* %3, i32* %4, i32* %5, i32* %6) {
%8 = tail call i32* (...) @BN_new()
%9 = tail call i32* (...) @BN_new()
%10 = tail call i32* (...) @BN_new()
%11 = tail call i32* (...) @BN_new()
%12 = tail call i32* (...) @BN_new()
%13 = tail call i32* (...) @BN_new()
br label %14
14: ; preds = %7, %14
%15 = tail call i32 @BN_num_bits(i32* %5)
%16 = tail call i32 @BN_rand(i32* %8, i32 %15, i32 -1, i32 0)
%17 = tail call i32 @BN_mod_sqr(i32* %9, i32* %8, i32* %5, i32* %6)
%18 = tail call i32 @BN_mod_mul(i32* %11, i32* %9, i32* %4, i32* %5, i32* %6)
%19 = tail call i32 @BN_mod_add(i32* %10, i32* %2, i32* %11, i32* %5, i32* %6)
%20 = tail call i32 @BN_gcd(i32* %11, i32* %10, i32* %5, i32* %6)
%21 = tail call zeroext i8 @BN_is_one(i32* %11)
%22 = icmp eq i8 %21, 0
br i1 %22, label %14, label %23
23: ; preds = %14
%24 = tail call i32 @BN_mod_mul(i32* %11, i32* %3, i32* %10, i32* %5, i32* %6)
%25 = tail call i32 @BN_mod_inverse(i32* %10, i32* %11, i32* %5, i32* %6)
%26 = tail call i32 @BN_mod_mul(i32* %11, i32* %4, i32* %9, i32* %5, i32* %6)
%27 = tail call i32 @BN_mod_sub(i32* %12, i32* %2, i32* %11, i32* %5, i32* %6)
%28 = tail call i32 @BN_mod_mul(i32* %1, i32* %12, i32* %10, i32* %5, i32* %6)
%29 = tail call i32 @BN_bin2bn(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1, i32* %13)
%30 = tail call i32 @BN_mod_mul(i32* %11, i32* %4, i32* %8, i32* %5, i32* %6)
%31 = tail call i32 @BN_mod_mul(i32* %12, i32* %13, i32* %11, i32* %5, i32* %6)
%32 = tail call i32 @BN_mod_mul(i32* %11, i32* %12, i32* %10, i32* %5, i32* %6)
%33 = tail call i32 @BN_sub(i32* %0, i32* %5, i32* %11)
%34 = tail call i32 @BN_free(i32* %8)
%35 = tail call i32 @BN_free(i32* %9)
%36 = tail call i32 @BN_free(i32* %10)
%37 = tail call i32 @BN_free(i32* %11)
%38 = tail call i32 @BN_free(i32* %12)
%39 = tail call i32 @BN_free(i32* %13)
ret void
}