#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*/ gmp_printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
scalar_t__ mpz_cmp(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mpz_divisible_p(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init_set_str(int /*<<< orphan*/, char *,
                                    int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init_set_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_nextprime(int /*<<< orphan*/, int /*<<< orphan*/);
int mpz_probab_prime_p(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_remove(int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int Problem003(void) {
  int check = 1;
  mpz_t input;
  mpz_t suspected_prime_factor, largest_prime_factor;
  mpz_init_set_str(input, "600851475143", 0);
  mpz_init(suspected_prime_factor);
  mpz_init_set_ui(largest_prime_factor, 1);
  mpz_init_set_ui(suspected_prime_factor, 0);
  while (check) {
    mpz_nextprime(suspected_prime_factor, suspected_prime_factor);
    if (mpz_cmp(input, suspected_prime_factor) < 0) {
      check = 0;
      printf("ERR");
    }
    if (mpz_probab_prime_p(suspected_prime_factor, 10) == 2) {
    } else {
      continue;
    }
    if (mpz_divisible_p(input, suspected_prime_factor)) {
      mpz_remove(input, input, suspected_prime_factor);
      if (mpz_probab_prime_p(input, 10) == 2) {
        if (mpz_cmp(suspected_prime_factor, input) > 0) {
          mpz_set(largest_prime_factor, suspected_prime_factor);
        } else {
          mpz_set(largest_prime_factor, input);
        }
        check = 0;
      }
    }
  }
  gmp_printf("%30Zd", largest_prime_factor);
  mpz_clear(input);
  mpz_clear(suspected_prime_factor);
  mpz_clear(largest_prime_factor);
  return 0;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x1,-0x4(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe27(%rip),%rsi
xor    %edx,%edx
call   1070 <mpz_init_set_str@plt>
mov    -0xc(%rbp),%edi
call   1080 <mpz_init@plt>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   10a0 <mpz_init_set_ui@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   10a0 <mpz_init_set_ui@plt>
cmpl   $0x0,-0x4(%rbp)
je     12d6 <Problem003+0x116>
mov    -0xc(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1090 <mpz_nextprime@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10b0 <mpz_cmp@plt>
cmp    $0x0,%rax
jge    123c <Problem003+0x7c>
movl   $0x0,-0x4(%rbp)
lea    0xdd6(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
mov    $0xa,%esi
call   1030 <mpz_probab_prime_p@plt>
cmp    $0x2,%eax
jne    1257 <Problem003+0x97>
jmp    125c <Problem003+0x9c>
jmp    11ff <Problem003+0x3f>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <mpz_divisible_p@plt>
cmp    $0x0,%rax
je     12d1 <Problem003+0x111>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   10e0 <mpz_remove@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1030 <mpz_probab_prime_p@plt>
cmp    $0x2,%eax
jne    12cc <Problem003+0x10c>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10b0 <mpz_cmp@plt>
cmp    $0x0,%rax
jle    12ba <Problem003+0xfa>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <mpz_set@plt>
jmp    12c5 <Problem003+0x105>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <mpz_set@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d1 <Problem003+0x111>
jmp    11ff <Problem003+0x3f>
mov    -0x10(%rbp),%esi
lea    0xd31(%rip),%rdi
call   10d0 <gmp_printf@plt>
mov    -0x8(%rbp),%edi
call   10c0 <mpz_clear@plt>
mov    -0xc(%rbp),%edi
call   10c0 <mpz_clear@plt>
mov    -0x10(%rbp),%edi
call   10c0 <mpz_clear@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %r14
push   %rbx
push   %rax
lea    0xe35(%rip),%rsi
xor    %edx,%edx
call   1070 <mpz_init_set_str@plt>
call   1080 <mpz_init@plt>
mov    $0x1,%esi
call   10a0 <mpz_init_set_ui@plt>
xor    %esi,%esi
call   10a0 <mpz_init_set_ui@plt>
lea    0xe1e(%rip),%r14
jmp    1205 <Problem003+0x45>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %rbx,%rbx
js     1256 <Problem003+0x96>
call   1090 <mpz_nextprime@plt>
call   10b0 <mpz_cmp@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    121f <Problem003+0x5f>
mov    %r14,%rdi
call   1060 <printf@plt>
mov    $0xa,%esi
call   1030 <mpz_probab_prime_p@plt>
cmp    $0x2,%eax
jne    1200 <Problem003+0x40>
call   1040 <mpz_divisible_p@plt>
test   %rax,%rax
je     1200 <Problem003+0x40>
call   10e0 <mpz_remove@plt>
mov    $0xa,%esi
call   1030 <mpz_probab_prime_p@plt>
cmp    $0x2,%eax
jne    1200 <Problem003+0x40>
call   10b0 <mpz_cmp@plt>
call   1050 <mpz_set@plt>
lea    0xdb4(%rip),%rdi
call   10d0 <gmp_printf@plt>
call   10c0 <mpz_clear@plt>
call   10c0 <mpz_clear@plt>
call   10c0 <mpz_clear@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"600851475143\00"
@.str.1 = [4 x i8] c"ERR\00"
@.str.2 = [6 x i8] c"%30Zd\00"
define i32 @Problem003() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 1, i32* %1
%5 = load i32, i32* %2
%6 = call i32 @mpz_init_set_str(i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
%7 = load i32, i32* %3
%8 = call i32 @mpz_init(i32 %7)
%9 = load i32, i32* %4
%10 = call i32 @mpz_init_set_ui(i32 %9, i32 1)
%11 = load i32, i32* %3
%12 = call i32 @mpz_init_set_ui(i32 %11, i32 0)
br label %13
13: ; preds = %60, %31, %0
%14 = load i32, i32* %1
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %61
16: ; preds = %13
%17 = load i32, i32* %3
%18 = load i32, i32* %3
%19 = call i32 @mpz_nextprime(i32 %17, i32 %18)
%20 = load i32, i32* %2
%21 = load i32, i32* %3
%22 = call i64 @mpz_cmp(i32 %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
store i32 0, i32* %1
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %16
%27 = load i32, i32* %3
%28 = call i32 @mpz_probab_prime_p(i32 %27, i32 10)
%29 = icmp eq i32 %28, 2
br i1 %29, label %30, label %31
30: ; preds = %26
br label %32
31: ; preds = %26
br label %13
32: ; preds = %30
%33 = load i32, i32* %2
%34 = load i32, i32* %3
%35 = call i64 @mpz_divisible_p(i32 %33, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %60
37: ; preds = %32
%38 = load i32, i32* %2
%39 = load i32, i32* %2
%40 = load i32, i32* %3
%41 = call i32 @mpz_remove(i32 %38, i32 %39, i32 %40)
%42 = load i32, i32* %2
%43 = call i32 @mpz_probab_prime_p(i32 %42, i32 10)
%44 = icmp eq i32 %43, 2
br i1 %44, label %45, label %59
45: ; preds = %37
%46 = load i32, i32* %3
%47 = load i32, i32* %2
%48 = call i64 @mpz_cmp(i32 %46, i32 %47)
%49 = icmp sgt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = load i32, i32* %4
%52 = load i32, i32* %3
%53 = call i32 @mpz_set(i32 %51, i32 %52)
br label %58
54: ; preds = %45
%55 = load i32, i32* %4
%56 = load i32, i32* %2
%57 = call i32 @mpz_set(i32 %55, i32 %56)
br label %58
58: ; preds = %54, %50
store i32 0, i32* %1
br label %59
59: ; preds = %58, %37
br label %60
60: ; preds = %59, %32
br label %13
61: ; preds = %13
%62 = load i32, i32* %4
%63 = call i32 @gmp_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %2
%65 = call i32 @mpz_clear(i32 %64)
%66 = load i32, i32* %3
%67 = call i32 @mpz_clear(i32 %66)
%68 = load i32, i32* %4
%69 = call i32 @mpz_clear(i32 %68)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"600851475143\00"
@.str.1 = [4 x i8] c"ERR\00"
@.str.2 = [6 x i8] c"%30Zd\00"
define i32 @Problem003() {
%1 = tail call i32 @mpz_init_set_str(i32 undef, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
%2 = tail call i32 @mpz_init(i32 undef)
%3 = tail call i32 @mpz_init_set_ui(i32 undef, i32 1)
%4 = tail call i32 @mpz_init_set_ui(i32 undef, i32 0)
br label %5
5: ; preds = %0, %24
%6 = tail call i32 @mpz_nextprime(i32 undef, i32 undef)
%7 = tail call i64 @mpz_cmp(i32 undef, i32 undef)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = tail call i32 @mpz_probab_prime_p(i32 undef, i32 10)
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %24
14: ; preds = %11
%15 = tail call i64 @mpz_divisible_p(i32 undef, i32 undef)
%16 = icmp eq i64 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = tail call i32 @mpz_remove(i32 undef, i32 undef, i32 undef)
%19 = tail call i32 @mpz_probab_prime_p(i32 undef, i32 10)
%20 = icmp eq i32 %19, 2
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = tail call i64 @mpz_cmp(i32 undef, i32 undef)
%23 = tail call i32 @mpz_set(i32 undef, i32 undef)
br label %25
24: ; preds = %14, %17, %11
br i1 %8, label %25, label %5
25: ; preds = %24, %21
%26 = tail call i32 @gmp_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 undef)
%27 = tail call i32 @mpz_clear(i32 undef)
%28 = tail call i32 @mpz_clear(i32 undef)
%29 = tail call i32 @mpz_clear(i32 undef)
ret i32 0
}