#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*/);
int /*<<< orphan*/ mpz_divexact(int /*<<< orphan*/, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(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_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, int);
scalar_t__ mpz_sgn(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_sub(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
void number_theory_inverse(mpz_t R, mpz_t A, mpz_t P) {
  mpz_t a;
  mpz_init(a);
  mpz_t b;
  mpz_init(b);
  mpz_t q;
  mpz_init(q);
  mpz_t r;
  mpz_init(r);
  mpz_t x;
  mpz_init(x);
  mpz_t lastx;
  mpz_init(lastx);
  mpz_t y;
  mpz_init(y);
  mpz_t lasty;
  mpz_init(lasty);
  mpz_t t1;
  mpz_init(t1);
  mpz_t t2;
  mpz_init(t2);
  mpz_set(b, P);
  mpz_set(a, A);
  mpz_set_ui(x, 0);
  mpz_set_ui(y, 1);
  mpz_set_ui(lastx, 1);
  mpz_set_ui(lasty, 0);
  while (mpz_sgn(b) != 0) {
    mpz_mod(r, a, b);
    mpz_sub(t1, a, r);
    mpz_divexact(q, t1, b);
    mpz_set(a, b);
    mpz_set(t1, x);
    mpz_mul(t2, q, x);
    mpz_sub(x, lastx, t2);
    mpz_mod(lastx, t1, P);
    mpz_set(t1, y);
    mpz_mul(t2, q, y);
    mpz_sub(y, lasty, t2);
    mpz_mod(lasty, t1, P);
    mpz_set(b, r);
  }
  mpz_set(R, lastx);
  mpz_clear(a);
  mpz_clear(b);
  mpz_clear(r);
  mpz_clear(q);
  mpz_clear(x);
  mpz_clear(y);
  mpz_clear(lastx);
  mpz_clear(lasty);
  mpz_clear(t1);
  mpz_clear(t2);
}


x86 assembly (clang, -O0):
<number_theory_inverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x14(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x18(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x1c(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x20(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x24(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x28(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x2c(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x30(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x34(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1080 <mpz_set_ui@plt>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1080 <mpz_set_ui@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1080 <mpz_set_ui@plt>
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
call   1080 <mpz_set_ui@plt>
mov    -0x14(%rbp),%edi
call   10b0 <mpz_sgn@plt>
cmp    $0x0,%rax
je     12f6 <number_theory_inverse+0x166>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1090 <mpz_mod@plt>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1040 <mpz_sub@plt>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1070 <mpz_divexact@plt>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x30(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1060 <mpz_mul@plt>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1040 <mpz_sub@plt>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1090 <mpz_mod@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1060 <mpz_mul@plt>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1040 <mpz_sub@plt>
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1090 <mpz_mod@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1030 <mpz_set@plt>
jmp    1235 <number_theory_inverse+0xa5>
mov    -0x4(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x10(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x14(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x18(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x20(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x28(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x24(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x2c(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x30(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x34(%rbp),%edi
call   10a0 <mpz_clear@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<number_theory_inverse>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
mov    %ebp,%esi
call   1030 <mpz_set@plt>
mov    %ebx,%esi
call   1030 <mpz_set@plt>
xor    %esi,%esi
call   1080 <mpz_set_ui@plt>
mov    $0x1,%esi
call   1080 <mpz_set_ui@plt>
mov    $0x1,%esi
call   1080 <mpz_set_ui@plt>
xor    %esi,%esi
call   1080 <mpz_set_ui@plt>
nopl   (%rax)
call   10b0 <mpz_sgn@plt>
test   %rax,%rax
je     1251 <number_theory_inverse+0xc1>
call   1090 <mpz_mod@plt>
call   1040 <mpz_sub@plt>
call   1070 <mpz_divexact@plt>
call   1030 <mpz_set@plt>
call   1030 <mpz_set@plt>
call   1060 <mpz_mul@plt>
call   1040 <mpz_sub@plt>
mov    %ebp,%edx
call   1090 <mpz_mod@plt>
call   1030 <mpz_set@plt>
call   1060 <mpz_mul@plt>
call   1040 <mpz_sub@plt>
mov    %ebp,%edx
call   1090 <mpz_mod@plt>
call   1030 <mpz_set@plt>
jmp    1200 <number_theory_inverse+0x70>
mov    %r14d,%edi
call   1030 <mpz_set@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
pop    %rbx
pop    %r14
pop    %rbp
jmp    10a0 <mpz_clear@plt>

LLVM IR (-O0):
define void @number_theory_inverse(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 i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%17 = load i32, i32* %7
%18 = call i32 @mpz_init(i32 %17)
%19 = load i32, i32* %8
%20 = call i32 @mpz_init(i32 %19)
%21 = load i32, i32* %9
%22 = call i32 @mpz_init(i32 %21)
%23 = load i32, i32* %10
%24 = call i32 @mpz_init(i32 %23)
%25 = load i32, i32* %11
%26 = call i32 @mpz_init(i32 %25)
%27 = load i32, i32* %12
%28 = call i32 @mpz_init(i32 %27)
%29 = load i32, i32* %13
%30 = call i32 @mpz_init(i32 %29)
%31 = load i32, i32* %14
%32 = call i32 @mpz_init(i32 %31)
%33 = load i32, i32* %15
%34 = call i32 @mpz_init(i32 %33)
%35 = load i32, i32* %16
%36 = call i32 @mpz_init(i32 %35)
%37 = load i32, i32* %8
%38 = load i32, i32* %6
%39 = call i32 @mpz_set(i32 %37, i32 %38)
%40 = load i32, i32* %7
%41 = load i32, i32* %5
%42 = call i32 @mpz_set(i32 %40, i32 %41)
%43 = load i32, i32* %11
%44 = call i32 @mpz_set_ui(i32 %43, i32 0)
%45 = load i32, i32* %13
%46 = call i32 @mpz_set_ui(i32 %45, i32 1)
%47 = load i32, i32* %12
%48 = call i32 @mpz_set_ui(i32 %47, i32 1)
%49 = load i32, i32* %14
%50 = call i32 @mpz_set_ui(i32 %49, i32 0)
br label %51
51: ; preds = %55, %3
%52 = load i32, i32* %8
%53 = call i64 @mpz_sgn(i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %104
55: ; preds = %51
%56 = load i32, i32* %10
%57 = load i32, i32* %7
%58 = load i32, i32* %8
%59 = call i32 @mpz_mod(i32 %56, i32 %57, i32 %58)
%60 = load i32, i32* %15
%61 = load i32, i32* %7
%62 = load i32, i32* %10
%63 = call i32 @mpz_sub(i32 %60, i32 %61, i32 %62)
%64 = load i32, i32* %9
%65 = load i32, i32* %15
%66 = load i32, i32* %8
%67 = call i32 @mpz_divexact(i32 %64, i32 %65, i32 %66)
%68 = load i32, i32* %7
%69 = load i32, i32* %8
%70 = call i32 @mpz_set(i32 %68, i32 %69)
%71 = load i32, i32* %15
%72 = load i32, i32* %11
%73 = call i32 @mpz_set(i32 %71, i32 %72)
%74 = load i32, i32* %16
%75 = load i32, i32* %9
%76 = load i32, i32* %11
%77 = call i32 @mpz_mul(i32 %74, i32 %75, i32 %76)
%78 = load i32, i32* %11
%79 = load i32, i32* %12
%80 = load i32, i32* %16
%81 = call i32 @mpz_sub(i32 %78, i32 %79, i32 %80)
%82 = load i32, i32* %12
%83 = load i32, i32* %15
%84 = load i32, i32* %6
%85 = call i32 @mpz_mod(i32 %82, i32 %83, i32 %84)
%86 = load i32, i32* %15
%87 = load i32, i32* %13
%88 = call i32 @mpz_set(i32 %86, i32 %87)
%89 = load i32, i32* %16
%90 = load i32, i32* %9
%91 = load i32, i32* %13
%92 = call i32 @mpz_mul(i32 %89, i32 %90, i32 %91)
%93 = load i32, i32* %13
%94 = load i32, i32* %14
%95 = load i32, i32* %16
%96 = call i32 @mpz_sub(i32 %93, i32 %94, i32 %95)
%97 = load i32, i32* %14
%98 = load i32, i32* %15
%99 = load i32, i32* %6
%100 = call i32 @mpz_mod(i32 %97, i32 %98, i32 %99)
%101 = load i32, i32* %8
%102 = load i32, i32* %10
%103 = call i32 @mpz_set(i32 %101, i32 %102)
br label %51
104: ; preds = %51
%105 = load i32, i32* %4
%106 = load i32, i32* %12
%107 = call i32 @mpz_set(i32 %105, i32 %106)
%108 = load i32, i32* %7
%109 = call i32 @mpz_clear(i32 %108)
%110 = load i32, i32* %8
%111 = call i32 @mpz_clear(i32 %110)
%112 = load i32, i32* %10
%113 = call i32 @mpz_clear(i32 %112)
%114 = load i32, i32* %9
%115 = call i32 @mpz_clear(i32 %114)
%116 = load i32, i32* %11
%117 = call i32 @mpz_clear(i32 %116)
%118 = load i32, i32* %13
%119 = call i32 @mpz_clear(i32 %118)
%120 = load i32, i32* %12
%121 = call i32 @mpz_clear(i32 %120)
%122 = load i32, i32* %14
%123 = call i32 @mpz_clear(i32 %122)
%124 = load i32, i32* %15
%125 = call i32 @mpz_clear(i32 %124)
%126 = load i32, i32* %16
%127 = call i32 @mpz_clear(i32 %126)
ret void
}

LLVM IR (-O2):
define void @number_theory_inverse(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_init(i32 undef)
%10 = tail call i32 @mpz_init(i32 undef)
%11 = tail call i32 @mpz_init(i32 undef)
%12 = tail call i32 @mpz_init(i32 undef)
%13 = tail call i32 @mpz_init(i32 undef)
%14 = tail call i32 @mpz_set(i32 undef, i32 %2)
%15 = tail call i32 @mpz_set(i32 undef, i32 %1)
%16 = tail call i32 @mpz_set_ui(i32 undef, i32 0)
%17 = tail call i32 @mpz_set_ui(i32 undef, i32 1)
%18 = tail call i32 @mpz_set_ui(i32 undef, i32 1)
%19 = tail call i32 @mpz_set_ui(i32 undef, i32 0)
%20 = tail call i64 @mpz_sgn(i32 undef)
%21 = icmp eq i64 %20, 0
br i1 %21, label %38, label %22
22: ; preds = %3, %22
%23 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 undef)
%24 = tail call i32 @mpz_sub(i32 undef, i32 undef, i32 undef)
%25 = tail call i32 @mpz_divexact(i32 undef, i32 undef, i32 undef)
%26 = tail call i32 @mpz_set(i32 undef, i32 undef)
%27 = tail call i32 @mpz_set(i32 undef, i32 undef)
%28 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%29 = tail call i32 @mpz_sub(i32 undef, i32 undef, i32 undef)
%30 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %2)
%31 = tail call i32 @mpz_set(i32 undef, i32 undef)
%32 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%33 = tail call i32 @mpz_sub(i32 undef, i32 undef, i32 undef)
%34 = tail call i32 @mpz_mod(i32 undef, i32 undef, i32 %2)
%35 = tail call i32 @mpz_set(i32 undef, i32 undef)
%36 = tail call i64 @mpz_sgn(i32 undef)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %22
38: ; preds = %22, %3
%39 = tail call i32 @mpz_set(i32 %0, i32 undef)
%40 = tail call i32 @mpz_clear(i32 undef)
%41 = tail call i32 @mpz_clear(i32 undef)
%42 = tail call i32 @mpz_clear(i32 undef)
%43 = tail call i32 @mpz_clear(i32 undef)
%44 = tail call i32 @mpz_clear(i32 undef)
%45 = tail call i32 @mpz_clear(i32 undef)
%46 = tail call i32 @mpz_clear(i32 undef)
%47 = tail call i32 @mpz_clear(i32 undef)
%48 = tail call i32 @mpz_clear(i32 undef)
%49 = tail call i32 @mpz_clear(i32 undef)
ret void
}