#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*/ mpz_clear(int /*<<< orphan*/);
scalar_t__ mpz_cmp(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_divexact(int /*<<< orphan*/, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int mpz_get_si(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_sub_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ number_theory_exp_modp(int /*<<< orphan*/,
                                          int /*<<< orphan*/,
                                          int /*<<< orphan*/,
                                          int /*<<< orphan*/);
int number_theory_legendre(mpz_t a, mpz_t p) {
  mpz_t t1;
  mpz_init(t1);
  mpz_t t2;
  mpz_init(t2);
  mpz_t t3;
  mpz_init(t3);
  mpz_sub_ui(t1, p, 1);
  mpz_set_ui(t2, 2);
  mpz_divexact(t3, t1, t2);
  number_theory_exp_modp(t2, a, t3, p);
  int value;
  if (mpz_cmp(t1, t2) == 0)
    value = -1;
  else
    value = mpz_get_si(t2);
  mpz_clear(t1);
  mpz_clear(t2);
  mpz_clear(t3);
  return value;
}


x86 assembly (clang, -O0):
<number_theory_legendre>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0x10(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0x14(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x1,%edx
call   1050 <mpz_sub_ui@plt>
mov    -0x10(%rbp),%edi
mov    $0x2,%esi
call   1090 <mpz_set_ui@plt>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <mpz_divexact@plt>
mov    -0x10(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%ecx
call   1070 <number_theory_exp_modp@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1080 <mpz_cmp@plt>
cmp    $0x0,%rax
jne    1203 <number_theory_legendre+0x83>
movl   $0xffffffff,-0x18(%rbp)
jmp    120e <number_theory_legendre+0x8e>
mov    -0x10(%rbp),%edi
call   1030 <mpz_get_si@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x10(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x14(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<number_theory_legendre>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
call   1040 <mpz_init@plt>
call   1040 <mpz_init@plt>
call   1040 <mpz_init@plt>
mov    %ebx,%esi
mov    $0x1,%edx
call   1050 <mpz_sub_ui@plt>
mov    $0x2,%esi
call   1090 <mpz_set_ui@plt>
call   1060 <mpz_divexact@plt>
mov    %ebp,%esi
mov    %ebx,%ecx
call   1070 <number_theory_exp_modp@plt>
call   1080 <mpz_cmp@plt>
test   %rax,%rax
je     11cd <number_theory_legendre+0x4d>
call   1030 <mpz_get_si@plt>
mov    %eax,%ebx
jmp    11d2 <number_theory_legendre+0x52>
mov    $0xffffffff,%ebx
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @number_theory_legendre(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %5
%10 = call i32 @mpz_init(i32 %9)
%11 = load i32, i32* %6
%12 = call i32 @mpz_init(i32 %11)
%13 = load i32, i32* %7
%14 = call i32 @mpz_init(i32 %13)
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = call i32 @mpz_sub_ui(i32 %15, i32 %16, i32 1)
%18 = load i32, i32* %6
%19 = call i32 @mpz_set_ui(i32 %18, i32 2)
%20 = load i32, i32* %7
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = call i32 @mpz_divexact(i32 %20, i32 %21, i32 %22)
%24 = load i32, i32* %6
%25 = load i32, i32* %3
%26 = load i32, i32* %7
%27 = load i32, i32* %4
%28 = call i32 @number_theory_exp_modp(i32 %24, i32 %25, i32 %26, i32 %27)
%29 = load i32, i32* %5
%30 = load i32, i32* %6
%31 = call i64 @mpz_cmp(i32 %29, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %2
store i32 -1, i32* %8
br label %37
34: ; preds = %2
%35 = load i32, i32* %6
%36 = call i32 @mpz_get_si(i32 %35)
store i32 %36, i32* %8
br label %37
37: ; preds = %34, %33
%38 = load i32, i32* %5
%39 = call i32 @mpz_clear(i32 %38)
%40 = load i32, i32* %6
%41 = call i32 @mpz_clear(i32 %40)
%42 = load i32, i32* %7
%43 = call i32 @mpz_clear(i32 %42)
%44 = load i32, i32* %8
ret i32 %44
}

LLVM IR (-O2):
define i32 @number_theory_legendre(i32 %0, i32 %1) {
%3 = tail call i32 @mpz_init(i32 undef)
%4 = tail call i32 @mpz_init(i32 undef)
%5 = tail call i32 @mpz_init(i32 undef)
%6 = tail call i32 @mpz_sub_ui(i32 undef, i32 %1, i32 1)
%7 = tail call i32 @mpz_set_ui(i32 undef, i32 2)
%8 = tail call i32 @mpz_divexact(i32 undef, i32 undef, i32 undef)
%9 = tail call i32 @number_theory_exp_modp(i32 undef, i32 %0, i32 undef, i32 %1)
%10 = tail call i64 @mpz_cmp(i32 undef, i32 undef)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = tail call i32 @mpz_get_si(i32 undef)
br label %14
14: ; preds = %2, %12
%15 = phi i32 [ %13, %12 ], [ -1, %2 ]
%16 = tail call i32 @mpz_clear(i32 undef)
%17 = tail call i32 @mpz_clear(i32 undef)
%18 = tail call i32 @mpz_clear(i32 undef)
ret i32 %15
}