#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, long *);
int main(void) {
  long n, p, q, x, y, xx, yy, e;
  printf("Factor a postitive odd number by Fermat's method\n");
  printf("N = \n");
  scanf("%ld", &n);
  printf("Factoring %ld\n", n);
  if (n < 1) {
    printf("N must be greater than zero\n");
    return 1;
  }
  if (n % 2 == 0) {
    printf("N must be odd\n");
    return 1;
  }
  xx = 1;
  yy = 1;
  e = yy - xx - n;
  int count = 0;
  while (e != 0) {
    if (e < 0) {
      e += yy;
      yy += 2;
    } else {
      e -= xx;
      xx += 2;
    }
    count++;
  }
  x = (xx - 1) / 2;
  y = (yy - 1) / 2;
  q = y + x;
  p = y - x;
  printf("%ld*%ld=%ld\n", p, q, n);
  printf("count= %d\n", count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xee6(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x10(%rbp),%rsi
lea    0xed6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x1,-0x10(%rbp)
jge    1192 <main+0x72>
lea    0xecc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bd <main+0x19d>
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
cmp    $0x0,%rdx
jne    11c4 <main+0xa4>
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bd <main+0x19d>
movq   $0x1,-0x38(%rbp)
movq   $0x1,-0x40(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1247 <main+0x127>
cmpq   $0x0,-0x48(%rbp)
jge    121e <main+0xfe>
mov    -0x40(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x40(%rbp)
jmp    1239 <main+0x119>
mov    -0x38(%rbp),%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x38(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11eb <main+0xcb>
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rax
sub    $0x1,%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4c(%rbp),%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeec(%rip),%rdi
call   1030 <puts@plt>
lea    0xf11(%rip),%rdi
call   1030 <puts@plt>
lea    0xea9(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%rsi
lea    0xe97(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
jle    118c <main+0x5c>
test   $0x1,%cl
jne    11a2 <main+0x72>
lea    0xed7(%rip),%rdi
jmp    1193 <main+0x63>
lea    0xedc(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    123a <main+0x10a>
mov    %rcx,%rdx
neg    %rdx
mov    $0x1,%eax
xor    %r14d,%r14d
mov    $0x1,%esi
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rsi,%rbx
mov    %rax,%rbp
add    $0x2,%rax
add    $0x2,%rsi
mov    %rbx,%rdi
neg    %rdi
test   %rdx,%rdx
cmovs  %rbx,%rsi
cmovns %rbp,%rax
cmovs  %rbp,%rdi
add    $0x1,%r14d
add    %rdi,%rdx
jne    11c0 <main+0x90>
lea    -0x1(%rsi),%rdx
shr    $0x3f,%rdx
lea    (%rsi,%rdx,1),%rdi
add    $0xffffffffffffffff,%rdi
sar    %rdi
lea    -0x1(%rax),%rdx
shr    $0x3f,%rdx
lea    (%rax,%rdx,1),%rsi
add    $0xffffffffffffffff,%rsi
sar    %rsi
lea    (%rsi,%rdi,1),%rdx
sub    %rdi,%rsi
lea    0xdf3(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdf0(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Factor a postitive odd number by Fermat's method\0A\00"
@.str.1 = [6 x i8] c"N = \0A\00"
@.str.2 = [4 x i8] c"%ld\00"
@.str.3 = [15 x i8] c"Factoring %ld\0A\00"
@.str.4 = [29 x i8] c"N must be greater than zero\0A\00"
@.str.5 = [15 x i8] c"N must be odd\0A\00"
@.str.6 = [13 x i8] c"%ld*%ld=%ld\0A\00"
@.str.7 = [11 x i8] c"count= %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %1
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%14 = load i64, i64* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64 %14)
%16 = load i64, i64* %2
%17 = icmp slt i64 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %72
20: ; preds = %0
%21 = load i64, i64* %2
%22 = srem i64 %21, 2
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %72
26: ; preds = %20
store i64 1, i64* %7
store i64 1, i64* %8
%27 = load i64, i64* %8
%28 = load i64, i64* %7
%29 = sub nsw i64 %27, %28
%30 = load i64, i64* %2
%31 = sub nsw i64 %29, %30
store i64 %31, i64* %9
store i32 0, i32* %10
br label %32
32: ; preds = %50, %26
%33 = load i64, i64* %9
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %53
35: ; preds = %32
%36 = load i64, i64* %9
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %35
%39 = load i64, i64* %8
%40 = load i64, i64* %9
%41 = add nsw i64 %40, %39
store i64 %41, i64* %9
%42 = load i64, i64* %8
%43 = add nsw i64 %42, 2
store i64 %43, i64* %8
br label %50
44: ; preds = %35
%45 = load i64, i64* %7
%46 = load i64, i64* %9
%47 = sub nsw i64 %46, %45
store i64 %47, i64* %9
%48 = load i64, i64* %7
%49 = add nsw i64 %48, 2
store i64 %49, i64* %7
br label %50
50: ; preds = %44, %38
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
br label %32
53: ; preds = %32
%54 = load i64, i64* %7
%55 = sub nsw i64 %54, 1
%56 = sdiv i64 %55, 2
store i64 %56, i64* %5
%57 = load i64, i64* %8
%58 = sub nsw i64 %57, 1
%59 = sdiv i64 %58, 2
store i64 %59, i64* %6
%60 = load i64, i64* %6
%61 = load i64, i64* %5
%62 = add nsw i64 %60, %61
store i64 %62, i64* %4
%63 = load i64, i64* %6
%64 = load i64, i64* %5
%65 = sub nsw i64 %63, %64
store i64 %65, i64* %3
%66 = load i64, i64* %3
%67 = load i64, i64* %4
%68 = load i64, i64* %2
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i64 %66, i64 %67, i64 %68)
%70 = load i32, i32* %10
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %70)
store i32 0, i32* %1
br label %72
72: ; preds = %53, %24, %18
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
@.str.2 = [4 x i8] c"%ld\00"
@.str.3 = [15 x i8] c"Factoring %ld\0A\00"
@.str.6 = [13 x i8] c"%ld*%ld=%ld\0A\00"
@.str.7 = [11 x i8] c"count= %d\0A\00"
@str = [49 x i8] c"Factor a postitive odd number by Fermat's method\00"
@str.8 = [5 x i8] c"N = \00"
@str.9 = [14 x i8] c"N must be odd\00"
@str.10 = [28 x i8] c"N must be greater than zero\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.8, i64 0, i64 0))
%5 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%6 = load i64, i64* %1
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64 %6)
%8 = load i64, i64* %1
%9 = icmp slt i64 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.10, i64 0, i64 0))
br label %43
12: ; preds = %0
%13 = and i64 %8, 1
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
br label %43
17: ; preds = %12
%18 = sub nsw i64 0, %8
br label %19
19: ; preds = %17, %19
%20 = phi i32 [ %32, %19 ], [ 0, %17 ]
%21 = phi i64 [ %31, %19 ], [ %18, %17 ]
%22 = phi i64 [ %28, %19 ], [ 1, %17 ]
%23 = phi i64 [ %27, %19 ], [ 1, %17 ]
%24 = icmp slt i64 %21, 0
%25 = add nsw i64 %22, 2
%26 = add nsw i64 %23, 2
%27 = select i1 %24, i64 %23, i64 %26
%28 = select i1 %24, i64 %25, i64 %22
%29 = sub i64 0, %23
%30 = select i1 %24, i64 %22, i64 %29
%31 = add i64 %30, %21
%32 = add nuw nsw i32 %20, 1
%33 = icmp eq i64 %31, 0
br i1 %33, label %34, label %19
34: ; preds = %19
%35 = add i64 %27, -1
%36 = sdiv i64 %35, 2
%37 = add i64 %28, -1
%38 = sdiv i64 %37, 2
%39 = add nsw i64 %38, %36
%40 = sub nsw i64 %38, %36
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i64 %40, i64 %39, i64 %8)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %32)
br label %43
43: ; preds = %34, %15, %10
%44 = phi i32 [ 1, %10 ], [ 1, %15 ], [ 0, %34 ]
ret i32 %44
}