#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 */
double sqrt(double);
void mp_sqrt_init(int n, int radix, int in[], int out[], int out_rev[]) {
  void mp_unexp_d2mp(int n, int radix, double din, int out[]);
  double mp_unexp_mp2d(int n, int radix, int in[]);
  int outexp;
  double din;
  out[0] = 1;
  out_rev[0] = 1;
  outexp = in[1];
  din = mp_unexp_mp2d(n, radix, &in[2]);
  if (outexp % 2 != 0) {
    din *= radix;
    outexp--;
  }
  outexp /= 2;
  din = sqrt(din);
  if (din < 1) {
    din *= radix;
    outexp--;
  }
  out[1] = outexp;
  mp_unexp_d2mp(n, radix, din, &out[2]);
  outexp = -outexp;
  din = ((double)1) / din;
  while (din < 1) {
    din *= radix;
    outexp--;
  }
  out_rev[1] = outexp;
  mp_unexp_d2mp(n, radix, din, &out_rev[2]);
}


x86 assembly (clang, -O0):
<mp_sqrt_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
add    $0x8,%rdx
call   1040 <mp_unexp_mp2d@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11ac <mp_sqrt_init+0x7c>
cvtsi2sdl -0x8(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  0xe2f(%rip),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    11f4 <mp_sqrt_init+0xc4>
cvtsi2sdl -0x8(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
movsd  -0x30(%rbp),%xmm0
mov    -0x18(%rbp),%rdx
add    $0x8,%rdx
call   1030 <mp_unexp_d2mp@plt>
xor    %eax,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
movsd  0xdda(%rip),%xmm0
divsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xdc8(%rip),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    1260 <mp_sqrt_init+0x130>
cvtsi2sdl -0x8(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1230 <mp_sqrt_init+0x100>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
movsd  -0x30(%rbp),%xmm0
mov    -0x20(%rbp),%rdx
add    $0x8,%rdx
call   1030 <mp_unexp_d2mp@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_sqrt_init>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r12
mov    %rcx,%rbp
mov    %esi,%r15d
mov    %edi,%r14d
movl   $0x1,(%rcx)
movl   $0x1,(%r8)
mov    0x4(%rdx),%ebx
add    $0x8,%rdx
call   1040 <mp_unexp_mp2d@plt>
cvtsi2sd %r15d,%xmm2
test   $0x1,%bl
je     1171 <mp_sqrt_init+0x41>
movapd %xmm2,%xmm1
jmp    1179 <mp_sqrt_init+0x49>
movsd  0xe87(%rip),%xmm1
mulsd  %xmm1,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
movsd  %xmm2,(%rsp)
jb     1192 <mp_sqrt_init+0x62>
sqrtsd %xmm0,%xmm0
jmp    119c <mp_sqrt_init+0x6c>
call   1050 <sqrt@plt>
movsd  (%rsp),%xmm2
sar    %ebx
movsd  0xe5a(%rip),%xmm3
xor    %eax,%eax
ucomisd %xmm0,%xmm3
seta   %al
movapd %xmm2,%xmm1
ja     11b9 <mp_sqrt_init+0x89>
movapd %xmm3,%xmm1
sub    %eax,%ebx
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %ebx,0x4(%rbp)
add    $0x8,%rbp
mov    %r14d,%edi
mov    %r15d,%esi
mov    %rbp,%rdx
call   1030 <mp_unexp_d2mp@plt>
neg    %ebx
movsd  0xe1c(%rip),%xmm1
movapd %xmm1,%xmm0
divsd  0x8(%rsp),%xmm0
ucomisd %xmm0,%xmm1
jbe    121d <mp_sqrt_init+0xed>
movsd  0xe04(%rip),%xmm1
movsd  (%rsp),%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mulsd  %xmm2,%xmm0
add    $0xffffffff,%ebx
ucomisd %xmm0,%xmm1
ja     1210 <mp_sqrt_init+0xe0>
mov    %ebx,0x4(%r12)
add    $0x8,%r12
mov    %r14d,%edi
mov    %r15d,%esi
mov    %r12,%rdx
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <mp_unexp_d2mp@plt>

LLVM IR (-O0):
define void @mp_sqrt_init(i32 %0, i32 %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca double
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%13 = load i32*, i32** %9
%14 = getelementptr inbounds i32, i32* %13, i64 0
store i32 1, i32* %14
%15 = load i32*, i32** %10
%16 = getelementptr inbounds i32, i32* %15, i64 0
store i32 1, i32* %16
%17 = load i32*, i32** %8
%18 = getelementptr inbounds i32, i32* %17, i64 1
%19 = load i32, i32* %18
store i32 %19, i32* %11
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = load i32*, i32** %8
%23 = getelementptr inbounds i32, i32* %22, i64 2
%24 = call double @mp_unexp_mp2d(i32 %20, i32 %21, i32* %23)
store double %24, double* %12
%25 = load i32, i32* %11
%26 = srem i32 %25, 2
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %5
%29 = load i32, i32* %7
%30 = sitofp i32 %29 to double
%31 = load double, double* %12
%32 = fmul double %31, %30
store double %32, double* %12
%33 = load i32, i32* %11
%34 = add nsw i32 %33, -1
store i32 %34, i32* %11
br label %35
35: ; preds = %28, %5
%36 = load i32, i32* %11
%37 = sdiv i32 %36, 2
store i32 %37, i32* %11
%38 = load double, double* %12
%39 = call double @sqrt(double %38)
store double %39, double* %12
%40 = load double, double* %12
%41 = fcmp olt double %40, 1.000000e+00
br i1 %41, label %42, label %49
42: ; preds = %35
%43 = load i32, i32* %7
%44 = sitofp i32 %43 to double
%45 = load double, double* %12
%46 = fmul double %45, %44
store double %46, double* %12
%47 = load i32, i32* %11
%48 = add nsw i32 %47, -1
store i32 %48, i32* %11
br label %49
49: ; preds = %42, %35
%50 = load i32, i32* %11
%51 = load i32*, i32** %9
%52 = getelementptr inbounds i32, i32* %51, i64 1
store i32 %50, i32* %52
%53 = load i32, i32* %6
%54 = load i32, i32* %7
%55 = load double, double* %12
%56 = load i32*, i32** %9
%57 = getelementptr inbounds i32, i32* %56, i64 2
%58 = load i32, i32* %11
%59 = sub nsw i32 0, %58
store i32 %59, i32* %11
%60 = load double, double* %12
%61 = fdiv double 1.000000e+00, %60
store double %61, double* %12
br label %62
62: ; preds = %65, %49
%63 = load double, double* %12
%64 = fcmp olt double %63, 1.000000e+00
br i1 %64, label %65, label %72
65: ; preds = %62
%66 = load i32, i32* %7
%67 = sitofp i32 %66 to double
%68 = load double, double* %12
%69 = fmul double %68, %67
store double %69, double* %12
%70 = load i32, i32* %11
%71 = add nsw i32 %70, -1
store i32 %71, i32* %11
br label %62
72: ; preds = %62
%73 = load i32, i32* %11
%74 = load i32*, i32** %10
%75 = getelementptr inbounds i32, i32* %74, i64 1
store i32 %73, i32* %75
%76 = load i32, i32* %6
%77 = load i32, i32* %7
%78 = load double, double* %12
%79 = load i32*, i32** %10
%80 = getelementptr inbounds i32, i32* %79, i64 2
ret void
}

LLVM IR (-O2):
define void @mp_sqrt_init(i32 %0, i32 %1, i32* %2, i32* %3, i32* %4) {
store i32 1, i32* %3
store i32 1, i32* %4
%6 = getelementptr inbounds i32, i32* %2, i64 1
%7 = load i32, i32* %6
%8 = getelementptr inbounds i32, i32* %2, i64 2
%9 = tail call double @mp_unexp_mp2d(i32 %0, i32 %1, i32* %8)
%10 = and i32 %7, 1
%11 = icmp eq i32 %10, 0
%12 = sitofp i32 %1 to double
%13 = xor i1 %11, true
%14 = sext i1 %13 to i32
%15 = add nsw i32 %7, %14
%16 = select i1 %11, double 1.000000e+00, double %12
%17 = fmul double %9, %16
%18 = sdiv i32 %15, 2
%19 = tail call double @sqrt(double %17)
%20 = fcmp olt double %19, 1.000000e+00
%21 = sext i1 %20 to i32
%22 = add nsw i32 %18, %21
%23 = select i1 %20, double %12, double 1.000000e+00
%24 = fmul double %19, %23
%25 = getelementptr inbounds i32, i32* %3, i64 1
store i32 %22, i32* %25
%26 = getelementptr inbounds i32, i32* %3, i64 2
tail call void @mp_unexp_d2mp(i32 %0, i32 %1, double %24, i32* %26)
%27 = sub nsw i32 0, %22
%28 = fdiv double 1.000000e+00, %24
%29 = fcmp olt double %28, 1.000000e+00
br i1 %29, label %30, label %36
30: ; preds = %5, %30
%31 = phi double [ %33, %30 ], [ %28, %5 ]
%32 = phi i32 [ %34, %30 ], [ %27, %5 ]
%33 = fmul double %31, %12
%34 = add nsw i32 %32, -1
%35 = fcmp olt double %33, 1.000000e+00
br i1 %35, label %30, label %36
36: ; preds = %30, %5
%37 = phi i32 [ %27, %5 ], [ %34, %30 ]
%38 = phi double [ %28, %5 ], [ %33, %30 ]
%39 = getelementptr inbounds i32, i32* %4, i64 1
store i32 %37, i32* %39
%40 = getelementptr inbounds i32, i32* %4, i64 2
tail call void @mp_unexp_d2mp(i32 %0, i32 %1, double %38, i32* %40)
ret void
}