#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;
typedef int /*<<< orphan*/ mpz_srcptr;
typedef int /*<<< orphan*/ mpz_ptr;

/* Variables and functions */
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_divmod(int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init_set_ui(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);
int /*<<< orphan*/ mpz_sub(int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
void invmod(mpz_ptr result, mpz_srcptr num, mpz_srcptr mod) {
  mpz_t a, b, s, q, tmp;
  mpz_init_set(a, num);
  mpz_init_set(b, mod);
  mpz_init_set_ui(s, 0);
  mpz_set_ui(result, 1);
  mpz_init(q);
  mpz_init(tmp);
  while (mpz_cmp_ui(a, 1) > 0) {
    mpz_divmod(q, tmp, a, b);
    mpz_set(a, b);
    mpz_set(b, tmp);
    mpz_mul(tmp, q, s);
    mpz_sub(tmp, result, tmp);
    mpz_set(result, s);
    mpz_set(s, tmp);
  }
  mpz_clear(a);
  mpz_clear(b);
  mpz_clear(s);
  mpz_clear(q);
  mpz_clear(tmp);
}


x86 assembly (clang, -O0):
<invmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10c0 <mpz_init_set@plt>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10c0 <mpz_init_set@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1080 <mpz_init_set_ui@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1090 <mpz_set_ui@plt>
mov    -0x1c(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x20(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jle    1263 <invmod+0xc3>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   10b0 <mpz_divmod@plt>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <mpz_mul@plt>
mov    -0x20(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1040 <mpz_sub@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <mpz_set@plt>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <mpz_set@plt>
jmp    11ee <invmod+0x4e>
mov    -0x10(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x14(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x18(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x20(%rbp),%edi
call   10a0 <mpz_clear@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invmod>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %edi,%ebx
call   10c0 <mpz_init_set@plt>
mov    %ebp,%esi
call   10c0 <mpz_init_set@plt>
xor    %esi,%esi
call   1080 <mpz_init_set_ui@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1090 <mpz_set_ui@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
test   %rax,%rax
jle    1208 <invmod+0x68>
call   10b0 <mpz_divmod@plt>
call   1030 <mpz_set@plt>
call   1030 <mpz_set@plt>
call   1060 <mpz_mul@plt>
mov    %ebx,%esi
call   1040 <mpz_sub@plt>
mov    %ebx,%edi
call   1030 <mpz_set@plt>
call   1030 <mpz_set@plt>
jmp    11d0 <invmod+0x30>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    10a0 <mpz_clear@plt>

LLVM IR (-O0):
define void @invmod(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
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* %7
%13 = load i32, i32* %5
%14 = call i32 @mpz_init_set(i32 %12, i32 %13)
%15 = load i32, i32* %8
%16 = load i32, i32* %6
%17 = call i32 @mpz_init_set(i32 %15, i32 %16)
%18 = load i32, i32* %9
%19 = call i32 @mpz_init_set_ui(i32 %18, i32 0)
%20 = load i32, i32* %4
%21 = call i32 @mpz_set_ui(i32 %20, i32 1)
%22 = load i32, i32* %10
%23 = call i32 @mpz_init(i32 %22)
%24 = load i32, i32* %11
%25 = call i32 @mpz_init(i32 %24)
br label %26
26: ; preds = %30, %3
%27 = load i32, i32* %7
%28 = call i64 @mpz_cmp_ui(i32 %27, i32 1)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %56
30: ; preds = %26
%31 = load i32, i32* %10
%32 = load i32, i32* %11
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = call i32 @mpz_divmod(i32 %31, i32 %32, i32 %33, i32 %34)
%36 = load i32, i32* %7
%37 = load i32, i32* %8
%38 = call i32 @mpz_set(i32 %36, i32 %37)
%39 = load i32, i32* %8
%40 = load i32, i32* %11
%41 = call i32 @mpz_set(i32 %39, i32 %40)
%42 = load i32, i32* %11
%43 = load i32, i32* %10
%44 = load i32, i32* %9
%45 = call i32 @mpz_mul(i32 %42, i32 %43, i32 %44)
%46 = load i32, i32* %11
%47 = load i32, i32* %4
%48 = load i32, i32* %11
%49 = call i32 @mpz_sub(i32 %46, i32 %47, i32 %48)
%50 = load i32, i32* %4
%51 = load i32, i32* %9
%52 = call i32 @mpz_set(i32 %50, i32 %51)
%53 = load i32, i32* %9
%54 = load i32, i32* %11
%55 = call i32 @mpz_set(i32 %53, i32 %54)
br label %26
56: ; preds = %26
%57 = load i32, i32* %7
%58 = call i32 @mpz_clear(i32 %57)
%59 = load i32, i32* %8
%60 = call i32 @mpz_clear(i32 %59)
%61 = load i32, i32* %9
%62 = call i32 @mpz_clear(i32 %61)
%63 = load i32, i32* %10
%64 = call i32 @mpz_clear(i32 %63)
%65 = load i32, i32* %11
%66 = call i32 @mpz_clear(i32 %65)
ret void
}

LLVM IR (-O2):
define void @invmod(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @mpz_init_set(i32 undef, i32 %1)
%5 = tail call i32 @mpz_init_set(i32 undef, i32 %2)
%6 = tail call i32 @mpz_init_set_ui(i32 undef, i32 0)
%7 = tail call i32 @mpz_set_ui(i32 %0, i32 1)
%8 = tail call i32 @mpz_init(i32 undef)
%9 = tail call i32 @mpz_init(i32 undef)
%10 = tail call i64 @mpz_cmp_ui(i32 undef, i32 1)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %3, %12
%13 = tail call i32 @mpz_divmod(i32 undef, i32 undef, i32 undef, i32 undef)
%14 = tail call i32 @mpz_set(i32 undef, i32 undef)
%15 = tail call i32 @mpz_set(i32 undef, i32 undef)
%16 = tail call i32 @mpz_mul(i32 undef, i32 undef, i32 undef)
%17 = tail call i32 @mpz_sub(i32 undef, i32 %0, i32 undef)
%18 = tail call i32 @mpz_set(i32 %0, i32 undef)
%19 = tail call i32 @mpz_set(i32 undef, i32 undef)
%20 = tail call i64 @mpz_cmp_ui(i32 undef, i32 1)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %12, label %22
22: ; preds = %12, %3
%23 = tail call i32 @mpz_clear(i32 undef)
%24 = tail call i32 @mpz_clear(i32 undef)
%25 = tail call i32 @mpz_clear(i32 undef)
%26 = tail call i32 @mpz_clear(i32 undef)
%27 = tail call i32 @mpz_clear(i32 undef)
ret void
}