#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 */
int /*<<< orphan*/ CRTTwo(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/);
int TonelliShanks(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mod(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ mpz_mul_si(int /*<<< orphan*/, int /*<<< orphan*/, int);
void GetSquareRootsModulo(mpz_t *z, mpz_t a, mpz_t p, mpz_t q, mpz_t n,
                          char **error) {
  mpz_t tmp1, tmp2;
  mpz_init(tmp1);
  mpz_init(tmp2);
  if (TonelliShanks(tmp1, a, p) == 1) {
    *error = "cannot calculate square root modulo";
    return;
  }
  if (TonelliShanks(tmp2, a, q) == 1) {
    *error = "cannot calculate square root modulo";
    return;
  }
  CRTTwo(z[0], tmp1, p, tmp2, q, n);
  mpz_mul_si(tmp1, tmp1, -1);
  mpz_mod(tmp1, tmp1, p);
  mpz_mul_si(z[1], z[0], -1);
  mpz_mod(z[1], z[1], n);
  CRTTwo(z[2], tmp1, p, tmp2, q, n);
  mpz_mul_si(z[3], z[2], -1);
  mpz_mod(z[3], z[3], n);
  mpz_clear(tmp1);
  mpz_clear(tmp2);
  *error = "success";
}


x86 assembly (clang, -O0):
<GetSquareRootsModulo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x28(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x24(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1080 <TonelliShanks@plt>
cmp    $0x1,%eax
jne    11b7 <GetSquareRootsModulo+0x57>
mov    -0x20(%rbp),%rax
lea    0xe51(%rip),%rcx
mov    %rcx,(%rax)
jmp    12b1 <GetSquareRootsModulo+0x151>
mov    -0x28(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1080 <TonelliShanks@plt>
cmp    $0x1,%eax
jne    11e1 <GetSquareRootsModulo+0x81>
mov    -0x20(%rbp),%rax
lea    0xe27(%rip),%rcx
mov    %rcx,(%rax)
jmp    12b1 <GetSquareRootsModulo+0x151>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
call   1040 <CRTTwo@plt>
mov    -0x24(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    $0xffffffff,%edx
call   1030 <mpz_mul_si@plt>
mov    -0x24(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <mpz_mod@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edx
call   1030 <mpz_mul_si@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%edx
call   1060 <mpz_mod@plt>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
call   1040 <CRTTwo@plt>
mov    -0x8(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%esi
mov    $0xffffffff,%edx
call   1030 <mpz_mul_si@plt>
mov    -0x8(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x8(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x18(%rbp),%edx
call   1060 <mpz_mod@plt>
mov    -0x24(%rbp),%edi
call   1070 <mpz_clear@plt>
mov    -0x28(%rbp),%edi
call   1070 <mpz_clear@plt>
mov    -0x20(%rbp),%rax
lea    0xd76(%rip),%rcx
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetSquareRootsModulo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,(%rsp)
mov    %r8d,%r15d
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r13
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
mov    %ebx,%esi
mov    %ebp,%edx
call   1080 <TonelliShanks@plt>
lea    0xe6a(%rip),%r14
cmp    $0x1,%eax
je     1236 <GetSquareRootsModulo+0xd6>
mov    %ebx,%esi
mov    %r12d,%edx
call   1080 <TonelliShanks@plt>
cmp    $0x1,%eax
je     1236 <GetSquareRootsModulo+0xd6>
mov    0x0(%r13),%edi
mov    %ebp,%edx
mov    %r12d,%r8d
mov    %r15d,%r9d
call   1040 <CRTTwo@plt>
mov    $0xffffffff,%edx
call   1030 <mpz_mul_si@plt>
mov    %ebp,%edx
call   1060 <mpz_mod@plt>
mov    0x0(%r13),%esi
mov    0x4(%r13),%edi
mov    $0xffffffff,%edx
call   1030 <mpz_mul_si@plt>
mov    0x4(%r13),%edi
mov    %edi,%esi
mov    %r15d,%edx
call   1060 <mpz_mod@plt>
mov    0x8(%r13),%edi
mov    %ebp,%edx
mov    %r12d,%r8d
mov    %r15d,%r9d
call   1040 <CRTTwo@plt>
mov    0x8(%r13),%esi
mov    0xc(%r13),%edi
mov    $0xffffffff,%edx
call   1030 <mpz_mul_si@plt>
mov    0xc(%r13),%edi
mov    %edi,%esi
mov    %r15d,%edx
call   1060 <mpz_mod@plt>
call   1070 <mpz_clear@plt>
call   1070 <mpz_clear@plt>
lea    0xdee(%rip),%r14
mov    (%rsp),%rax
mov    %r14,(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"cannot calculate square root modulo\00"
@.str.1 = [8 x i8] c"success\00"
define void @GetSquareRootsModulo(i32* %0, i32 %1, i32 %2, i32 %3, i32 %4, i8** %5) {
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8**
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i8** %5, i8*** %12
%15 = load i32, i32* %13
%16 = call i32 @mpz_init(i32 %15)
%17 = load i32, i32* %14
%18 = call i32 @mpz_init(i32 %17)
%19 = load i32, i32* %13
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = call i32 @TonelliShanks(i32 %19, i32 %20, i32 %21)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %6
%25 = load i8**, i8*** %12
store i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8** %25
br label %95
26: ; preds = %6
%27 = load i32, i32* %14
%28 = load i32, i32* %8
%29 = load i32, i32* %10
%30 = call i32 @TonelliShanks(i32 %27, i32 %28, i32 %29)
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = load i8**, i8*** %12
store i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8** %33
br label %95
34: ; preds = %26
%35 = load i32*, i32** %7
%36 = getelementptr inbounds i32, i32* %35, i64 0
%37 = load i32, i32* %36
%38 = load i32, i32* %13
%39 = load i32, i32* %9
%40 = load i32, i32* %14
%41 = load i32, i32* %10
%42 = load i32, i32* %11
%43 = call i32 @CRTTwo(i32 %37, i32 %38, i32 %39, i32 %40, i32 %41, i32 %42)
%44 = load i32, i32* %13
%45 = load i32, i32* %13
%46 = call i32 @mpz_mul_si(i32 %44, i32 %45, i32 -1)
%47 = load i32, i32* %13
%48 = load i32, i32* %13
%49 = load i32, i32* %9
%50 = call i32 @mpz_mod(i32 %47, i32 %48, i32 %49)
%51 = load i32*, i32** %7
%52 = getelementptr inbounds i32, i32* %51, i64 1
%53 = load i32, i32* %52
%54 = load i32*, i32** %7
%55 = getelementptr inbounds i32, i32* %54, i64 0
%56 = load i32, i32* %55
%57 = call i32 @mpz_mul_si(i32 %53, i32 %56, i32 -1)
%58 = load i32*, i32** %7
%59 = getelementptr inbounds i32, i32* %58, i64 1
%60 = load i32, i32* %59
%61 = load i32*, i32** %7
%62 = getelementptr inbounds i32, i32* %61, i64 1
%63 = load i32, i32* %62
%64 = load i32, i32* %11
%65 = call i32 @mpz_mod(i32 %60, i32 %63, i32 %64)
%66 = load i32*, i32** %7
%67 = getelementptr inbounds i32, i32* %66, i64 2
%68 = load i32, i32* %67
%69 = load i32, i32* %13
%70 = load i32, i32* %9
%71 = load i32, i32* %14
%72 = load i32, i32* %10
%73 = load i32, i32* %11
%74 = call i32 @CRTTwo(i32 %68, i32 %69, i32 %70, i32 %71, i32 %72, i32 %73)
%75 = load i32*, i32** %7
%76 = getelementptr inbounds i32, i32* %75, i64 3
%77 = load i32, i32* %76
%78 = load i32*, i32** %7
%79 = getelementptr inbounds i32, i32* %78, i64 2
%80 = load i32, i32* %79
%81 = call i32 @mpz_mul_si(i32 %77, i32 %80, i32 -1)
%82 = load i32*, i32** %7
%83 = getelementptr inbounds i32, i32* %82, i64 3
%84 = load i32, i32* %83
%85 = load i32*, i32** %7
%86 = getelementptr inbounds i32, i32* %85, i64 3
%87 = load i32, i32* %86
%88 = load i32, i32* %11
%89 = call i32 @mpz_mod(i32 %84, i32 %87, i32 %88)
%90 = load i32, i32* %13
%91 = call i32 @mpz_clear(i32 %90)
%92 = load i32, i32* %14
%93 = call i32 @mpz_clear(i32 %92)
%94 = load i8**, i8*** %12
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %94
br label %95
95: ; preds = %34, %32, %24
ret void
}

LLVM IR (-O2):
@.str = [36 x i8] c"cannot calculate square root modulo\00"
@.str.1 = [8 x i8] c"success\00"
define void @GetSquareRootsModulo(i32* nocapture %0, i32 %1, i32 %2, i32 %3, i32 %4, i8** nocapture writeonly %5) {
%7 = tail call i32 @mpz_init(i32 undef)
%8 = tail call i32 @mpz_init(i32 undef)
%9 = tail call i32 @TonelliShanks(i32 undef, i32 %1, i32 %2)
%10 = icmp eq i32 %9, 1
br i1 %10, label %36, label %11
11: ; preds = %6
%12 = tail call i32 @TonelliShanks(i32 undef, i32 %1, i32 %3)
%13 = icmp eq i32 %12, 1
br i1 %13, label %36, label %14
14: ; preds = %11
%15 = load i32, i32* %0
%16 = tail call i32 @CRTTwo(i32 %15, i32 undef, i32 %2, i32 undef, i32 %3, i32 %4)
%17 = tail call i32 @mpz_mul_si(i32 undef, i32 undef, i32 -1)
%18 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %2)
%19 = getelementptr inbounds i32, i32* %0, i64 1
%20 = load i32, i32* %19
%21 = load i32, i32* %0
%22 = tail call i32 @mpz_mul_si(i32 %20, i32 %21, i32 -1)
%23 = load i32, i32* %19
%24 = tail call i32 @mpz_mod(i32 %23, i32 %23, i32 %4)
%25 = getelementptr inbounds i32, i32* %0, i64 2
%26 = load i32, i32* %25
%27 = tail call i32 @CRTTwo(i32 %26, i32 undef, i32 %2, i32 undef, i32 %3, i32 %4)
%28 = getelementptr inbounds i32, i32* %0, i64 3
%29 = load i32, i32* %28
%30 = load i32, i32* %25
%31 = tail call i32 @mpz_mul_si(i32 %29, i32 %30, i32 -1)
%32 = load i32, i32* %28
%33 = tail call i32 @mpz_mod(i32 %32, i32 %32, i32 %4)
%34 = tail call i32 @mpz_clear(i32 undef)
%35 = tail call i32 @mpz_clear(i32 undef)
br label %36
36: ; preds = %11, %6, %14
%37 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), %11 ]
store i8* %37, i8** %5
ret void
}