#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*/ mpz_t;

/* Variables and functions */
unsigned long ULONG_MAX;
int /*<<< orphan*/ mpz_add_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, int);
int mpz_divisible_p(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int mpz_legendre(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mod(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mul(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_powm(int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_powm_ui(int /*<<< orphan*/, int /*<<< orphan*/, int,
                               int /*<<< orphan*/);
unsigned long mpz_scan1(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_sub_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_tdiv_q_2exp(int /*<<< orphan*/, int /*<<< orphan*/, int);
int mpz_tstbit(int /*<<< orphan*/, int);
int TonelliShanks(mpz_t res, mpz_t a, mpz_t p) {
  mpz_t Q, c, t, b, tmp;
  int i;
  unsigned long int S, M;
  mpz_init(Q);
  mpz_init(c);
  mpz_init(t);
  mpz_init(b);
  mpz_init(tmp);
  if (mpz_tstbit(p, 0) != 1)
    return 1;
  if (mpz_legendre(a, p) != 1)
    return 1;
  if (mpz_divisible_p(a, p) == 1) {
    mpz_set_ui(res, 0);
    return 0;
  }
  if (mpz_tstbit(p, 1) == 1) {
    mpz_set(res, p);
    mpz_add_ui(res, res, 1);
    mpz_tdiv_q_2exp(res, res, 2);
    mpz_powm(res, a, res, p);
    return 0;
  }
  mpz_set(Q, p);
  mpz_sub_ui(Q, Q, 1);
  S = mpz_scan1(Q, 0);
  if (S == ULONG_MAX)
    return 1;
  mpz_tdiv_q_2exp(Q, Q, S);
  mpz_set_ui(c, 2);
  while (mpz_legendre(c, p) != -1)
    mpz_add_ui(c, c, 1);
  mpz_powm(c, c, Q, p);
  mpz_powm(t, a, Q, p);
  mpz_add_ui(Q, Q, 1);
  mpz_mod(Q, Q, p);
  mpz_tdiv_q_2exp(Q, Q, 1);
  mpz_powm(res, a, Q, p);
  M = S;
  while (mpz_cmp_ui(t, 1) != 0) {
    for (i = 1; i < M; i++) {
      mpz_powm_ui(tmp, t, 1 << i, p);
      if (mpz_cmp_ui(tmp, 1) == 0)
        break;
    }
    mpz_powm_ui(b, c, 1 << (M - i - 1), p);
    mpz_mul(res, res, b);
    mpz_mod(res, res, p);
    mpz_mul(c, b, b);
    mpz_mod(c, c, p);
    mpz_mul(t, t, c);
    mpz_mod(t, t, p);
    M = i;
  }
  mpz_clear(b);
  mpz_clear(t);
  mpz_clear(c);
  mpz_clear(Q);
  mpz_clear(tmp);
  return 0;
}


x86 assembly (clang, -O0):
<TonelliShanks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <mpz_init@plt>
mov    -0x18(%rbp),%edi
call   1090 <mpz_init@plt>
mov    -0x1c(%rbp),%edi
call   1090 <mpz_init@plt>
mov    -0x20(%rbp),%edi
call   1090 <mpz_init@plt>
mov    -0x24(%rbp),%edi
call   1090 <mpz_init@plt>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1030 <mpz_tstbit@plt>
cmp    $0x1,%eax
je     1258 <TonelliShanks+0x58>
movl   $0x1,-0x4(%rbp)
jmp    1522 <TonelliShanks+0x322>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1080 <mpz_legendre@plt>
cmp    $0x1,%eax
je     1278 <TonelliShanks+0x78>
movl   $0x1,-0x4(%rbp)
jmp    1522 <TonelliShanks+0x322>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1050 <mpz_divisible_p@plt>
cmp    $0x1,%eax
jne    12a2 <TonelliShanks+0xa2>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   10f0 <mpz_set_ui@plt>
movl   $0x0,-0x4(%rbp)
jmp    1522 <TonelliShanks+0x322>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1030 <mpz_tstbit@plt>
cmp    $0x1,%eax
jne    1300 <TonelliShanks+0x100>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1060 <mpz_set@plt>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x1,%edx
call   1070 <mpz_add_ui@plt>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x2,%edx
call   10c0 <mpz_tdiv_q_2exp@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0x10(%rbp),%ecx
call   1040 <mpz_powm@plt>
movl   $0x0,-0x4(%rbp)
jmp    1522 <TonelliShanks+0x322>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1060 <mpz_set@plt>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    $0x1,%edx
call   10b0 <mpz_sub_ui@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1120 <mpz_scan1@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2ca4(%rip),%rcx
cmp    (%rcx),%rax
jne    1349 <TonelliShanks+0x149>
movl   $0x1,-0x4(%rbp)
jmp    1522 <TonelliShanks+0x322>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
call   10c0 <mpz_tdiv_q_2exp@plt>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   10f0 <mpz_set_ui@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1080 <mpz_legendre@plt>
cmp    $0xffffffff,%eax
je     1390 <TonelliShanks+0x190>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x1,%edx
call   1070 <mpz_add_ui@plt>
jmp    1367 <TonelliShanks+0x167>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%ecx
call   1040 <mpz_powm@plt>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%ecx
call   1040 <mpz_powm@plt>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    $0x1,%edx
call   1070 <mpz_add_ui@plt>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1100 <mpz_mod@plt>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    $0x1,%edx
call   10c0 <mpz_tdiv_q_2exp@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%ecx
call   1040 <mpz_powm@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   10d0 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
je     14f3 <TonelliShanks+0x2f3>
movl   $0x1,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    146c <TonelliShanks+0x26c>
mov    -0x24(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    -0x10(%rbp),%ecx
call   10e0 <mpz_powm_ui@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   10d0 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jne    1459 <TonelliShanks+0x259>
jmp    146c <TonelliShanks+0x26c>
jmp    145e <TonelliShanks+0x25e>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1417 <TonelliShanks+0x217>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    -0x10(%rbp),%ecx
call   10e0 <mpz_powm_ui@plt>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x20(%rbp),%edx
call   10a0 <mpz_mul@plt>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1100 <mpz_mod@plt>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x20(%rbp),%edx
call   10a0 <mpz_mul@plt>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1100 <mpz_mod@plt>
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   10a0 <mpz_mul@plt>
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1100 <mpz_mod@plt>
movslq -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    13f9 <TonelliShanks+0x1f9>
mov    -0x20(%rbp),%edi
call   1110 <mpz_clear@plt>
mov    -0x1c(%rbp),%edi
call   1110 <mpz_clear@plt>
mov    -0x18(%rbp),%edi
call   1110 <mpz_clear@plt>
mov    -0x14(%rbp),%edi
call   1110 <mpz_clear@plt>
mov    -0x24(%rbp),%edi
call   1110 <mpz_clear@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TonelliShanks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %esi,%r14d
mov    %edi,%r15d
call   1090 <mpz_init@plt>
call   1090 <mpz_init@plt>
call   1090 <mpz_init@plt>
call   1090 <mpz_init@plt>
call   1090 <mpz_init@plt>
mov    %r13d,%edi
xor    %esi,%esi
call   1030 <mpz_tstbit@plt>
mov    $0x1,%r12d
cmp    $0x1,%eax
jne    1451 <TonelliShanks+0x251>
mov    %r14d,%edi
mov    %r13d,%esi
call   1080 <mpz_legendre@plt>
cmp    $0x1,%eax
jne    1451 <TonelliShanks+0x251>
mov    %r14d,%edi
mov    %r13d,%esi
call   1050 <mpz_divisible_p@plt>
cmp    $0x1,%eax
jne    127c <TonelliShanks+0x7c>
xor    %r12d,%r12d
mov    %r15d,%edi
xor    %esi,%esi
call   10f0 <mpz_set_ui@plt>
jmp    1451 <TonelliShanks+0x251>
mov    $0x1,%r12d
mov    %r13d,%edi
mov    $0x1,%esi
call   1030 <mpz_tstbit@plt>
cmp    $0x1,%eax
jne    12d5 <TonelliShanks+0xd5>
mov    %r15d,%edi
mov    %r13d,%esi
call   1060 <mpz_set@plt>
mov    %r15d,%edi
mov    %r15d,%esi
mov    $0x1,%edx
call   1070 <mpz_add_ui@plt>
mov    %r15d,%edi
mov    %r15d,%esi
mov    $0x2,%edx
call   10c0 <mpz_tdiv_q_2exp@plt>
mov    %r15d,%edi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %r13d,%ecx
call   1040 <mpz_powm@plt>
jmp    144e <TonelliShanks+0x24e>
mov    %r13d,%esi
call   1060 <mpz_set@plt>
mov    $0x1,%edx
call   10b0 <mpz_sub_ui@plt>
xor    %esi,%esi
call   1120 <mpz_scan1@plt>
mov    %rax,%rbp
mov    0x2ce0(%rip),%rax
cmp    (%rax),%rbp
je     1451 <TonelliShanks+0x251>
mov    %ebp,%edx
call   10c0 <mpz_tdiv_q_2exp@plt>
mov    $0x2,%esi
call   10f0 <mpz_set_ui@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%esi
call   1080 <mpz_legendre@plt>
cmp    $0xffffffff,%eax
je     1339 <TonelliShanks+0x139>
mov    $0x1,%edx
call   1070 <mpz_add_ui@plt>
jmp    1320 <TonelliShanks+0x120>
mov    %r13d,%ecx
call   1040 <mpz_powm@plt>
mov    %r14d,%esi
mov    %r13d,%ecx
call   1040 <mpz_powm@plt>
mov    $0x1,%edx
call   1070 <mpz_add_ui@plt>
mov    %r13d,%edx
call   1100 <mpz_mod@plt>
mov    $0x1,%edx
call   10c0 <mpz_tdiv_q_2exp@plt>
mov    %r15d,%edi
mov    %r14d,%esi
mov    %r13d,%ecx
call   1040 <mpz_powm@plt>
mov    $0x1,%esi
call   10d0 <mpz_cmp_ui@plt>
jmp    13e8 <TonelliShanks+0x1e8>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%r14d
not    %ebx
add    %ebx,%ebp
mov    $0x1,%edx
mov    %ebp,%ecx
shl    %cl,%edx
mov    %r13d,%ecx
call   10e0 <mpz_powm_ui@plt>
mov    %r15d,%edi
mov    %r15d,%esi
call   10a0 <mpz_mul@plt>
mov    %r15d,%edi
mov    %r15d,%esi
mov    %r13d,%edx
call   1100 <mpz_mod@plt>
call   10a0 <mpz_mul@plt>
mov    %r13d,%edx
call   1100 <mpz_mod@plt>
call   10a0 <mpz_mul@plt>
mov    %r13d,%edx
call   1100 <mpz_mod@plt>
mov    $0x1,%esi
call   10d0 <mpz_cmp_ui@plt>
mov    %r14,%rbp
test   %rax,%rax
je     1435 <TonelliShanks+0x235>
mov    $0x1,%ebx
cmp    $0x2,%rbp
jb     1390 <TonelliShanks+0x190>
mov    $0x1,%ebx
nopl   (%rax)
mov    $0x1,%edx
mov    %ebx,%ecx
shl    %cl,%edx
mov    %r13d,%ecx
call   10e0 <mpz_powm_ui@plt>
mov    $0x1,%esi
call   10d0 <mpz_cmp_ui@plt>
test   %rax,%rax
je     1390 <TonelliShanks+0x190>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1400 <TonelliShanks+0x200>
mov    %rbp,%rbx
jmp    1390 <TonelliShanks+0x190>
call   1110 <mpz_clear@plt>
call   1110 <mpz_clear@plt>
call   1110 <mpz_clear@plt>
call   1110 <mpz_clear@plt>
call   1110 <mpz_clear@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ULONG_MAX = global i64 0
define i32 @TonelliShanks(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%16 = load i32, i32* %8
%17 = call i32 @mpz_init(i32 %16)
%18 = load i32, i32* %9
%19 = call i32 @mpz_init(i32 %18)
%20 = load i32, i32* %10
%21 = call i32 @mpz_init(i32 %20)
%22 = load i32, i32* %11
%23 = call i32 @mpz_init(i32 %22)
%24 = load i32, i32* %12
%25 = call i32 @mpz_init(i32 %24)
%26 = load i32, i32* %7
%27 = call i32 @mpz_tstbit(i32 %26, i32 0)
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %3
store i32 1, i32* %4
br label %194
30: ; preds = %3
%31 = load i32, i32* %6
%32 = load i32, i32* %7
%33 = call i32 @mpz_legendre(i32 %31, i32 %32)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 1, i32* %4
br label %194
36: ; preds = %30
%37 = load i32, i32* %6
%38 = load i32, i32* %7
%39 = call i32 @mpz_divisible_p(i32 %37, i32 %38)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* %5
%43 = call i32 @mpz_set_ui(i32 %42, i32 0)
store i32 0, i32* %4
br label %194
44: ; preds = %36
%45 = load i32, i32* %7
%46 = call i32 @mpz_tstbit(i32 %45, i32 1)
%47 = icmp eq i32 %46, 1
br i1 %47, label %48, label %63
48: ; preds = %44
%49 = load i32, i32* %5
%50 = load i32, i32* %7
%51 = call i32 @mpz_set(i32 %49, i32 %50)
%52 = load i32, i32* %5
%53 = load i32, i32* %5
%54 = call i32 @mpz_add_ui(i32 %52, i32 %53, i32 1)
%55 = load i32, i32* %5
%56 = load i32, i32* %5
%57 = call i32 @mpz_tdiv_q_2exp(i32 %55, i32 %56, i32 2)
%58 = load i32, i32* %5
%59 = load i32, i32* %6
%60 = load i32, i32* %5
%61 = load i32, i32* %7
%62 = call i32 @mpz_powm(i32 %58, i32 %59, i32 %60, i32 %61)
store i32 0, i32* %4
br label %194
63: ; preds = %44
%64 = load i32, i32* %8
%65 = load i32, i32* %7
%66 = call i32 @mpz_set(i32 %64, i32 %65)
%67 = load i32, i32* %8
%68 = load i32, i32* %8
%69 = call i32 @mpz_sub_ui(i32 %67, i32 %68, i32 1)
%70 = load i32, i32* %8
%71 = call i64 @mpz_scan1(i32 %70, i32 0)
store i64 %71, i64* %14
%72 = load i64, i64* %14
%73 = load i64, i64* @ULONG_MAX
%74 = icmp eq i64 %72, %73
br i1 %74, label %75, label %76
75: ; preds = %63
store i32 1, i32* %4
br label %194
76: ; preds = %63
%77 = load i32, i32* %8
%78 = load i32, i32* %8
%79 = load i64, i64* %14
%80 = trunc i64 %79 to i32
%81 = call i32 @mpz_tdiv_q_2exp(i32 %77, i32 %78, i32 %80)
%82 = load i32, i32* %9
%83 = call i32 @mpz_set_ui(i32 %82, i32 2)
br label %84
84: ; preds = %89, %76
%85 = load i32, i32* %9
%86 = load i32, i32* %7
%87 = call i32 @mpz_legendre(i32 %85, i32 %86)
%88 = icmp ne i32 %87, -1
br i1 %88, label %89, label %93
89: ; preds = %84
%90 = load i32, i32* %9
%91 = load i32, i32* %9
%92 = call i32 @mpz_add_ui(i32 %90, i32 %91, i32 1)
br label %84
93: ; preds = %84
%94 = load i32, i32* %9
%95 = load i32, i32* %9
%96 = load i32, i32* %8
%97 = load i32, i32* %7
%98 = call i32 @mpz_powm(i32 %94, i32 %95, i32 %96, i32 %97)
%99 = load i32, i32* %10
%100 = load i32, i32* %6
%101 = load i32, i32* %8
%102 = load i32, i32* %7
%103 = call i32 @mpz_powm(i32 %99, i32 %100, i32 %101, i32 %102)
%104 = load i32, i32* %8
%105 = load i32, i32* %8
%106 = call i32 @mpz_add_ui(i32 %104, i32 %105, i32 1)
%107 = load i32, i32* %8
%108 = load i32, i32* %8
%109 = load i32, i32* %7
%110 = call i32 @mpz_mod(i32 %107, i32 %108, i32 %109)
%111 = load i32, i32* %8
%112 = load i32, i32* %8
%113 = call i32 @mpz_tdiv_q_2exp(i32 %111, i32 %112, i32 1)
%114 = load i32, i32* %5
%115 = load i32, i32* %6
%116 = load i32, i32* %8
%117 = load i32, i32* %7
%118 = call i32 @mpz_powm(i32 %114, i32 %115, i32 %116, i32 %117)
%119 = load i64, i64* %14
store i64 %119, i64* %15
br label %120
120: ; preds = %145, %93
%121 = load i32, i32* %10
%122 = call i64 @mpz_cmp_ui(i32 %121, i32 1)
%123 = icmp ne i64 %122, 0
br i1 %123, label %124, label %183
124: ; preds = %120
store i32 1, i32* %13
br label %125
125: ; preds = %142, %124
%126 = load i32, i32* %13
%127 = sext i32 %126 to i64
%128 = load i64, i64* %15
%129 = icmp ult i64 %127, %128
br i1 %129, label %130, label %145
130: ; preds = %125
%131 = load i32, i32* %12
%132 = load i32, i32* %10
%133 = load i32, i32* %13
%134 = shl i32 1, %133
%135 = load i32, i32* %7
%136 = call i32 @mpz_powm_ui(i32 %131, i32 %132, i32 %134, i32 %135)
%137 = load i32, i32* %12
%138 = call i64 @mpz_cmp_ui(i32 %137, i32 1)
%139 = icmp eq i64 %138, 0
br i1 %139, label %140, label %141
140: ; preds = %130
br label %145
141: ; preds = %130
br label %142
142: ; preds = %141
%143 = load i32, i32* %13
%144 = add nsw i32 %143, 1
store i32 %144, i32* %13
br label %125
145: ; preds = %140, %125
%146 = load i32, i32* %11
%147 = load i32, i32* %9
%148 = load i64, i64* %15
%149 = load i32, i32* %13
%150 = sext i32 %149 to i64
%151 = sub i64 %148, %150
%152 = sub i64 %151, 1
%153 = trunc i64 %152 to i32
%154 = shl i32 1, %153
%155 = load i32, i32* %7
%156 = call i32 @mpz_powm_ui(i32 %146, i32 %147, i32 %154, i32 %155)
%157 = load i32, i32* %5
%158 = load i32, i32* %5
%159 = load i32, i32* %11
%160 = call i32 @mpz_mul(i32 %157, i32 %158, i32 %159)
%161 = load i32, i32* %5
%162 = load i32, i32* %5
%163 = load i32, i32* %7
%164 = call i32 @mpz_mod(i32 %161, i32 %162, i32 %163)
%165 = load i32, i32* %9
%166 = load i32, i32* %11
%167 = load i32, i32* %11
%168 = call i32 @mpz_mul(i32 %165, i32 %166, i32 %167)
%169 = load i32, i32* %9
%170 = load i32, i32* %9
%171 = load i32, i32* %7
%172 = call i32 @mpz_mod(i32 %169, i32 %170, i32 %171)
%173 = load i32, i32* %10
%174 = load i32, i32* %10
%175 = load i32, i32* %9
%176 = call i32 @mpz_mul(i32 %173, i32 %174, i32 %175)
%177 = load i32, i32* %10
%178 = load i32, i32* %10
%179 = load i32, i32* %7
%180 = call i32 @mpz_mod(i32 %177, i32 %178, i32 %179)
%181 = load i32, i32* %13
%182 = sext i32 %181 to i64
store i64 %182, i64* %15
br label %120
183: ; preds = %120
%184 = load i32, i32* %11
%185 = call i32 @mpz_clear(i32 %184)
%186 = load i32, i32* %10
%187 = call i32 @mpz_clear(i32 %186)
%188 = load i32, i32* %9
%189 = call i32 @mpz_clear(i32 %188)
%190 = load i32, i32* %8
%191 = call i32 @mpz_clear(i32 %190)
%192 = load i32, i32* %12
%193 = call i32 @mpz_clear(i32 %192)
store i32 0, i32* %4
br label %194
194: ; preds = %183, %75, %48, %41, %35, %29
%195 = load i32, i32* %4
ret i32 %195
}

LLVM IR (-O2):
@ULONG_MAX = global i64 0
define i32 @TonelliShanks(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @mpz_init(i32 undef)
%5 = tail call i32 @mpz_init(i32 undef)
%6 = tail call i32 @mpz_init(i32 undef)
%7 = tail call i32 @mpz_init(i32 undef)
%8 = tail call i32 @mpz_init(i32 undef)
%9 = tail call i32 @mpz_tstbit(i32 %2, i32 0)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %87
11: ; preds = %3
%12 = tail call i32 @mpz_legendre(i32 %1, i32 %2)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %87
14: ; preds = %11
%15 = tail call i32 @mpz_divisible_p(i32 %1, i32 %2)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @mpz_set_ui(i32 %0, i32 0)
br label %87
19: ; preds = %14
%20 = tail call i32 @mpz_tstbit(i32 %2, i32 1)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = tail call i32 @mpz_set(i32 %0, i32 %2)
%24 = tail call i32 @mpz_add_ui(i32 %0, i32 %0, i32 1)
%25 = tail call i32 @mpz_tdiv_q_2exp(i32 %0, i32 %0, i32 2)
%26 = tail call i32 @mpz_powm(i32 %0, i32 %1, i32 %0, i32 %2)
br label %87
27: ; preds = %19
%28 = tail call i32 @mpz_set(i32 undef, i32 %2)
%29 = tail call i32 @mpz_sub_ui(i32 undef, i32 undef, i32 1)
%30 = tail call i64 @mpz_scan1(i32 undef, i32 0)
%31 = load i64, i64* @ULONG_MAX
%32 = icmp eq i64 %30, %31
br i1 %32, label %87, label %33
33: ; preds = %27
%34 = trunc i64 %30 to i32
%35 = tail call i32 @mpz_tdiv_q_2exp(i32 undef, i32 undef, i32 %34)
%36 = tail call i32 @mpz_set_ui(i32 undef, i32 2)
%37 = tail call i32 @mpz_legendre(i32 undef, i32 %2)
%38 = icmp eq i32 %37, -1
br i1 %38, label %43, label %39
39: ; preds = %33, %39
%40 = tail call i32 @mpz_add_ui(i32 undef, i32 undef, i32 1)
%41 = tail call i32 @mpz_legendre(i32 undef, i32 %2)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %39
43: ; preds = %39, %33
%44 = tail call i32 @mpz_powm(i32 undef, i32 undef, i32 undef, i32 %2)
%45 = tail call i32 @mpz_powm(i32 undef, i32 %1, i32 undef, i32 %2)
%46 = tail call i32 @mpz_add_ui(i32 undef, i32 undef, i32 1)
%47 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %2)
%48 = tail call i32 @mpz_tdiv_q_2exp(i32 undef, i32 undef, i32 1)
%49 = tail call i32 @mpz_powm(i32 %0, i32 %1, i32 undef, i32 %2)
%50 = tail call i64 @mpz_cmp_ui(i32 undef, i32 1)
%51 = icmp eq i64 %50, 0
br i1 %51, label %81, label %52
52: ; preds = %43, %65
%53 = phi i64 [ %67, %65 ], [ %30, %43 ]
%54 = icmp ugt i64 %53, 1
br i1 %54, label %55, label %65
55: ; preds = %52, %62
%56 = phi i64 [ %63, %62 ], [ 1, %52 ]
%57 = trunc i64 %56 to i32
%58 = shl nuw i32 1, %57
%59 = tail call i32 @mpz_powm_ui(i32 undef, i32 undef, i32 %58, i32 %2)
%60 = tail call i64 @mpz_cmp_ui(i32 undef, i32 1)
%61 = icmp eq i64 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %55
%63 = add nuw i64 %56, 1
%64 = icmp eq i64 %63, %53
br i1 %64, label %65, label %55
65: ; preds = %62, %55, %52
%66 = phi i64 [ 1, %52 ], [ %56, %55 ], [ %53, %62 ]
%67 = and i64 %66, 4294967295
%68 = xor i64 %66, -1
%69 = add i64 %53, %68
%70 = trunc i64 %69 to i32
%71 = shl nuw i32 1, %70
%72 = tail call i32 @mpz_powm_ui(i32 undef, i32 undef, i32 %71, i32 %2)
%73 = tail call i32 @mpz_mul(i32 %0, i32 %0, i32 undef)
%74 = tail call i32 @mpz_mod(i32 %0, i32 %0, i32 %2)
%75 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%76 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %2)
%77 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%78 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %2)
%79 = tail call i64 @mpz_cmp_ui(i32 undef, i32 1)
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %52
81: ; preds = %65, %43
%82 = tail call i32 @mpz_clear(i32 undef)
%83 = tail call i32 @mpz_clear(i32 undef)
%84 = tail call i32 @mpz_clear(i32 undef)
%85 = tail call i32 @mpz_clear(i32 undef)
%86 = tail call i32 @mpz_clear(i32 undef)
br label %87
87: ; preds = %27, %11, %3, %81, %22, %17
%88 = phi i32 [ 0, %17 ], [ 0, %22 ], [ 0, %81 ], [ 1, %3 ], [ 1, %11 ], [ 1, %27 ]
ret i32 %88
}