#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*/ cancel(unsigned long *, unsigned long *);
int /*<<< orphan*/ printf(char *, ...);
void solve(unsigned long p, unsigned long q) {
  printf("%ld/%ld = ", p, q);
  cancel(&p, &q);
  while (p > 1) {
    unsigned long r;
    r = (q + p) / p;
    printf("%d/%ld + ", 1, r);
    p = p * r - q;
    q = q * r;
    cancel(&p, &q);
  }
  (p > 0) ? printf("%ld/%ld\n", p, q) : printf("\n");
}


x86 assembly (clang, -O0):
<solve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <cancel@plt>
cmpq   $0x1,-0x8(%rbp)
jbe    11b9 <solve+0x99>
mov    -0x10(%rbp),%rax
add    -0x8(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x8(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdx
lea    0xe8e(%rip),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
imul   -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <cancel@plt>
jmp    1153 <solve+0x33>
cmpq   $0x0,-0x8(%rbp)
jbe    11df <solve+0xbf>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ed <solve+0xcd>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<solve>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rdx
mov    %rdi,%rsi
mov    %rdi,0x10(%rsp)
mov    %rdx,0x8(%rsp)
lea    0xeae(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1040 <cancel@plt>
mov    0x10(%rsp),%rcx
cmp    $0x2,%rcx
jb     11fb <solve+0xcb>
lea    0xe8d(%rip),%r14
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
jmp    11df <solve+0xaf>
nopw   0x0(%rax,%rax,1)
xor    %edx,%edx
div    %rcx
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%rax
imul   %rbx,%rax
mov    0x8(%rsp),%rcx
sub    %rcx,%rax
mov    %rax,0x10(%rsp)
imul   %rbx,%rcx
mov    %rcx,0x8(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <cancel@plt>
mov    0x10(%rsp),%rcx
cmp    $0x1,%rcx
jbe    11fb <solve+0xcb>
mov    0x8(%rsp),%rax
add    %rcx,%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
jne    1190 <solve+0x60>
xor    %edx,%edx
div    %ecx
mov    %eax,%ebx
jmp    1198 <solve+0x68>
cmp    $0x1,%rcx
jne    121b <solve+0xeb>
mov    0x8(%rsp),%rdx
lea    0xe08(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1225 <solve+0xf5>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%ld/%ld = \00"
@.str.1 = [10 x i8] c"%d/%ld + \00"
@.str.2 = [9 x i8] c"%ld/%ld\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @solve(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i64 %0, i64* %3
store i64 %1, i64* %4
%6 = load i64, i64* %3
%7 = load i64, i64* %4
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %6, i64 %7)
%9 = call i32 @cancel(i64* %3, i64* %4)
br label %10
10: ; preds = %13, %2
%11 = load i64, i64* %3
%12 = icmp ugt i64 %11, 1
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = load i64, i64* %4
%15 = load i64, i64* %3
%16 = add i64 %14, %15
%17 = load i64, i64* %3
%18 = udiv i64 %16, %17
store i64 %18, i64* %5
%19 = load i64, i64* %5
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 1, i64 %19)
%21 = load i64, i64* %3
%22 = load i64, i64* %5
%23 = mul i64 %21, %22
%24 = load i64, i64* %4
%25 = sub i64 %23, %24
store i64 %25, i64* %3
%26 = load i64, i64* %4
%27 = load i64, i64* %5
%28 = mul i64 %26, %27
store i64 %28, i64* %4
%29 = call i32 @cancel(i64* %3, i64* %4)
br label %10
30: ; preds = %10
%31 = load i64, i64* %3
%32 = icmp ugt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i64, i64* %3
%35 = load i64, i64* %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i64 %34, i64 %35)
br label %39
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = phi i32 [ %36, %33 ], [ %38, %37 ]
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"%ld/%ld = \00"
@.str.1 = [10 x i8] c"%d/%ld + \00"
@.str.2 = [9 x i8] c"%ld/%ld\0A\00"
define void @solve(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %3
store i64 %1, i64* %4
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %0, i64 %1)
%6 = call i32 @cancel(i64* %3, i64* %4)
%7 = load i64, i64* %3
%8 = icmp ugt i64 %7, 1
br i1 %8, label %9, label %23
9: ; preds = %2, %9
%10 = phi i64 [ %21, %9 ], [ %7, %2 ]
%11 = load i64, i64* %4
%12 = add i64 %11, %10
%13 = udiv i64 %12, %10
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 1, i64 %13)
%15 = load i64, i64* %3
%16 = mul i64 %15, %13
%17 = load i64, i64* %4
%18 = sub i64 %16, %17
store i64 %18, i64* %3
%19 = mul i64 %17, %13
store i64 %19, i64* %4
%20 = call i32 @cancel(i64* %3, i64* %4)
%21 = load i64, i64* %3
%22 = icmp ugt i64 %21, 1
br i1 %22, label %9, label %23
23: ; preds = %9, %2
%24 = phi i64 [ %7, %2 ], [ %21, %9 ]
%25 = icmp eq i64 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i64, i64* %4
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i64 1, i64 %27)
br label %31
29: ; preds = %23
%30 = call i32 @putchar(i32 10)
br label %31
31: ; preds = %29, %26
ret void
}