#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*/ SetSgError(char *);
int /*<<< orphan*/ SimplifyFraction(int, int, int *, int *);
int /*<<< orphan*/ sprintf(char *, char *, int, ...);
const char *FormatFraction(int nume, int deno, int Decimal, char *Buffer,
                           int SizeBuffer) {
  int n, d;
  char *cp, *cpp;
  static char StaticBuffer[40];
  if (NULL == Buffer) {
    Buffer = StaticBuffer;
    SizeBuffer = sizeof StaticBuffer / sizeof(*StaticBuffer);
  }
  Buffer[SizeBuffer - 1] = '\0';
  if (nume == 0) {
    Buffer[0] = '0';
    Buffer[1] = '\0';
  }
  if (Decimal) {
    sprintf(Buffer, "%.6g", (double)nume / deno);
    cp = Buffer;
    if (*cp == '-')
      cp++;
    if (*cp == '0') {
      cpp = cp + 1;
      while (*cp)
        *cp++ = *cpp++;
    }
  } else {
    SimplifyFraction(nume, deno, &n, &d);
    if (d == 1)
      sprintf(Buffer, "%d", n);
    else
      sprintf(Buffer, "%d/%d", n, d);
  }
  if (Buffer[SizeBuffer - 1] != '\0') {
    Buffer[SizeBuffer - 1] = '\0';
    SetSgError("Internal Error: FormatFraction(): Buffer too small");
    return NULL;
  }
  return Buffer;
}


x86 assembly (clang, -O0):
<FormatFraction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1167 <FormatFraction+0x37>
lea    0x2ef4(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x28,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0xc(%rbp)
jne    1191 <FormatFraction+0x61>
mov    -0x20(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
cmpl   $0x0,-0x14(%rbp)
je     123d <FormatFraction+0x10d>
mov    -0x20(%rbp),%rdi
cvtsi2sdl -0xc(%rbp),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edx
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    11e3 <FormatFraction+0xb3>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    1238 <FormatFraction+0x108>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     1233 <FormatFraction+0x103>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
mov    (%rax),%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
jmp    11ff <FormatFraction+0xcf>
jmp    1238 <FormatFraction+0x108>
jmp    1291 <FormatFraction+0x161>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
lea    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1050 <SimplifyFraction@plt>
cmpl   $0x1,-0x2c(%rbp)
jne    1274 <FormatFraction+0x144>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    128c <FormatFraction+0x15c>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xd80(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    1291 <FormatFraction+0x161>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     12d5 <FormatFraction+0x1a5>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xd48(%rip),%rdi
call   1030 <SetSgError@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dd <FormatFraction+0x1ad>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FormatFraction>:
push   %r14
push   %rbx
push   %rax
add    $0xffffffff,%r8d
test   %rcx,%rcx
lea    0x2f0e(%rip),%rbx
cmovne %rcx,%rbx
movslq %r8d,%rax
mov    $0x27,%r14d
cmovne %rax,%r14
movb   $0x0,(%rbx,%r14,1)
test   %edi,%edi
je     11b0 <FormatFraction+0x80>
test   %edx,%edx
je     11b9 <FormatFraction+0x89>
cvtsi2sd %edi,%xmm0
cvtsi2sd %esi,%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edx
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
xor    %eax,%eax
cmpb   $0x2d,(%rbx)
sete   %al
cmpb   $0x30,(%rbx,%rax,1)
jne    11e3 <FormatFraction+0xb3>
add    %rbx,%rax
add    $0x1,%rax
cs nopw 0x0(%rax,%rax,1)
movzbl (%rax),%ecx
mov    %cl,-0x1(%rax)
add    $0x1,%rax
test   %cl,%cl
jne    11a0 <FormatFraction+0x70>
jmp    11e3 <FormatFraction+0xb3>
movw   $0x30,(%rbx)
test   %edx,%edx
jne    1160 <FormatFraction+0x30>
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
call   1050 <SimplifyFraction@plt>
mov    (%rsp),%ecx
mov    0x4(%rsp),%edx
cmp    $0x1,%ecx
jne    1208 <FormatFraction+0xd8>
lea    0xe2f(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
cmpb   $0x0,(%rbx,%r14,1)
je     11fd <FormatFraction+0xcd>
movb   $0x0,(%rbx,%r14,1)
lea    0xe15(%rip),%rdi
call   1030 <SetSgError@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf6(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
cmpb   $0x0,(%rbx,%r14,1)
jne    11ea <FormatFraction+0xba>
jmp    11fd <FormatFraction+0xcd>

LLVM IR (-O0):
@FormatFraction.StaticBuffer = internal global [40 x i8] zeroinitializer
@.str = [5 x i8] c"%.6g\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [6 x i8] c"%d/%d\00"
@.str.3 = [51 x i8] c"Internal Error: FormatFraction(): Buffer too small\00"
define i8* @FormatFraction(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%16 = load i8*, i8** %10
%17 = icmp eq i8* null, %16
br i1 %17, label %18, label %19
18: ; preds = %5
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @FormatFraction.StaticBuffer, i64 0, i64 0), i8** %10
store i32 40, i32* %11
br label %19
19: ; preds = %18, %5
%20 = load i8*, i8** %10
%21 = load i32, i32* %11
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 0, i8* %24
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i8*, i8** %10
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 48, i8* %29
%30 = load i8*, i8** %10
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8 0, i8* %31
br label %32
32: ; preds = %27, %19
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %72
35: ; preds = %32
%36 = load i8*, i8** %10
%37 = load i32, i32* %7
%38 = sitofp i32 %37 to double
%39 = load i32, i32* %8
%40 = sitofp i32 %39 to double
%41 = fdiv double %38, %40
%42 = fptosi double %41 to i32
%43 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = load i8*, i8** %10
store i8* %44, i8** %14
%45 = load i8*, i8** %14
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 45
br i1 %48, label %49, label %52
49: ; preds = %35
%50 = load i8*, i8** %14
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %14
br label %52
52: ; preds = %49, %35
%53 = load i8*, i8** %14
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 48
br i1 %56, label %57, label %71
57: ; preds = %52
%58 = load i8*, i8** %14
%59 = getelementptr inbounds i8, i8* %58, i64 1
store i8* %59, i8** %15
br label %60
60: ; preds = %64, %57
%61 = load i8*, i8** %14
%62 = load i8, i8* %61
%63 = icmp ne i8 %62, 0
br i1 %63, label %64, label %70
64: ; preds = %60
%65 = load i8*, i8** %15
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %15
%67 = load i8, i8* %65
%68 = load i8*, i8** %14
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %14
store i8 %67, i8* %68
br label %60
70: ; preds = %60
br label %71
71: ; preds = %70, %52
br label %88
72: ; preds = %32
%73 = load i32, i32* %7
%74 = load i32, i32* %8
%75 = call i32 @SimplifyFraction(i32 %73, i32 %74, i32* %12, i32* %13)
%76 = load i32, i32* %13
%77 = icmp eq i32 %76, 1
br i1 %77, label %78, label %82
78: ; preds = %72
%79 = load i8*, i8** %10
%80 = load i32, i32* %12
%81 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %79, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %80)
br label %87
82: ; preds = %72
%83 = load i8*, i8** %10
%84 = load i32, i32* %12
%85 = load i32, i32* %13
%86 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %83, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %84, i32 %85)
br label %87
87: ; preds = %82, %78
br label %88
88: ; preds = %87, %71
%89 = load i8*, i8** %10
%90 = load i32, i32* %11
%91 = sub nsw i32 %90, 1
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %89, i64 %92
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %104
97: ; preds = %88
%98 = load i8*, i8** %10
%99 = load i32, i32* %11
%100 = sub nsw i32 %99, 1
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8, i8* %98, i64 %101
store i8 0, i8* %102
%103 = call i32 @SetSgError(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %6
br label %106
104: ; preds = %88
%105 = load i8*, i8** %10
store i8* %105, i8** %6
br label %106
106: ; preds = %104, %97
%107 = load i8*, i8** %6
ret i8* %107
}

LLVM IR (-O2):
@FormatFraction.StaticBuffer = internal global [40 x i8] zeroinitializer
@.str = [5 x i8] c"%.6g\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [6 x i8] c"%d/%d\00"
@.str.3 = [51 x i8] c"Internal Error: FormatFraction(): Buffer too small\00"
define i8* @FormatFraction(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %6 to i8*
%9 = bitcast i32* %7 to i8*
%10 = icmp eq i8* %3, null
%11 = select i1 %10, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @FormatFraction.StaticBuffer, i64 0, i64 0), i8* %3
%12 = add nsw i32 %4, -1
%13 = sext i32 %12 to i64
%14 = select i1 %10, i64 39, i64 %13
%15 = getelementptr inbounds i8, i8* %11, i64 %14
store i8 0, i8* %15
%16 = icmp eq i32 %0, 0
br i1 %16, label %17, label %19
17: ; preds = %5
store i8 48, i8* %11
%18 = getelementptr inbounds i8, i8* %11, i64 1
store i8 0, i8* %18
br label %19
19: ; preds = %17, %5
%20 = icmp eq i32 %2, 0
br i1 %20, label %38, label %21
21: ; preds = %19
%22 = sitofp i32 %0 to double
%23 = sitofp i32 %1 to double
%24 = fdiv double %22, %23
%25 = fptosi double %24 to i32
%26 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i8, i8* %11
%28 = icmp eq i8 %27, 45
%29 = zext i1 %28 to i64
%30 = getelementptr i8, i8* %11, i64 %29
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 48
br i1 %32, label %33, label %47
33: ; preds = %21, %33
%34 = phi i8* [ %35, %33 ], [ %30, %21 ]
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i8, i8* %35
store i8 %36, i8* %34
%37 = icmp eq i8 %36, 0
br i1 %37, label %47, label %33
38: ; preds = %19
%39 = call i32 @SimplifyFraction(i32 %0, i32 %1, i32* %6, i32* %7)
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, 1
%42 = load i32, i32* %6
br i1 %41, label %43, label %45
43: ; preds = %38
%44 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %42)
br label %47
45: ; preds = %38
%46 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %42, i32 %40)
br label %47
47: ; preds = %33, %43, %45, %21
%48 = load i8, i8* %15
%49 = icmp eq i8 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %47
store i8 0, i8* %15
%51 = call i32 @SetSgError(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
br label %52
52: ; preds = %47, %50
%53 = phi i8* [ null, %50 ], [ %11, %47 ]
ret i8* %53
}