#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*/ mpq_t;
typedef int /*<<< orphan*/ mpf_t;

/* Variables and functions */
int /*<<< orphan*/ gmp_asprintf(char **, char *, int /*<<< orphan*/);
int /*<<< orphan*/ mpf_clear(int /*<<< orphan*/);
int /*<<< orphan*/ mpf_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpf_set_q(int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
char *mpq2str(mpq_t mp_rational) {
  char *str;
  char *ptr;
  mpf_t mp_float;
  mpf_init(mp_float);
  mpf_set_q(mp_float, mp_rational);
  gmp_asprintf(&str, "%.32Ff", mp_float);
  mpf_clear(mp_float);
  for (ptr = str + strlen(str) - 1; *ptr == '0'; ptr--)
    *ptr = '\0';
  if (*ptr == '.')
    *ptr = '\0';
  return str;
}


x86 assembly (clang, -O0):
<mpq2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <mpf_init@plt>
mov    -0x1c(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <mpf_set_q@plt>
mov    -0x1c(%rbp),%edx
lea    -0x10(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1070 <gmp_asprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <mpf_clear@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    11d6 <mpq2str+0x86>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    11ae <mpq2str+0x5e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11ed <mpq2str+0x9d>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpq2str>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
call   1050 <mpf_init@plt>
mov    %ebx,%esi
call   1030 <mpf_set_q@plt>
lea    0xe96(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1070 <gmp_asprintf@plt>
call   1060 <mpf_clear@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
nop
movzbl (%rax),%ecx
cmp    $0x30,%cl
jne    11a1 <mpq2str+0x51>
movb   $0x0,(%rax)
add    $0xffffffffffffffff,%rax
jmp    1190 <mpq2str+0x40>
cmp    $0x2e,%cl
jne    11a9 <mpq2str+0x59>
movb   $0x0,(%rax)
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%.32Ff\00"
define i8* @mpq2str(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %5
%7 = call i32 @mpf_init(i32 %6)
%8 = load i32, i32* %5
%9 = load i32, i32* %2
%10 = call i32 @mpf_set_q(i32 %8, i32 %9)
%11 = load i32, i32* %5
%12 = call i32 @gmp_asprintf(i8** %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %5
%14 = call i32 @mpf_clear(i32 %13)
%15 = load i8*, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 -1
store i8* %20, i8** %4
br label %21
21: ; preds = %28, %1
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 48
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i8*, i8** %4
store i8 0, i8* %27
br label %28
28: ; preds = %26
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %4
br label %21
31: ; preds = %21
%32 = load i8*, i8** %4
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 46
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = load i8*, i8** %4
store i8 0, i8* %37
br label %38
38: ; preds = %36, %31
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@.str = [7 x i8] c"%.32Ff\00"
define i8* @mpq2str(i32 %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i32 @mpf_init(i32 undef)
%5 = tail call i32 @mpf_set_q(i32 undef, i32 %0)
%6 = call i32 @gmp_asprintf(i8** %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 undef)
%7 = call i32 @mpf_clear(i32 undef)
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
br label %12
12: ; preds = %16, %1
%13 = phi i8* [ %11, %1 ], [ %14, %16 ]
%14 = getelementptr inbounds i8, i8* %13, i64 -1
%15 = load i8, i8* %14
switch i8 %15, label %18 [
i8 48, label %16
i8 46, label %17
]
16: ; preds = %12
store i8 0, i8* %14
br label %12
17: ; preds = %12
store i8 0, i8* %14
br label %18
18: ; preds = %12, %17
%19 = load i8*, i8** %2
ret i8* %19
}