#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 long int32_t;
typedef int int16_t;

/* Variables and functions */
long L_mult(int, int);
long L_sub(long, long);
int add(int, int);
int shr(int, int);
int16_t Sqrt(int32_t Num) {
  int16_t i;
  int16_t Rez = (int16_t)0;
  int16_t Exp = (int16_t)0x4000;
  int32_t Acc, L_temp;
  for (i = 0; i < 14; i++) {
    Acc = L_mult(add(Rez, Exp), add(Rez, Exp));
    L_temp = L_sub(Num, Acc);
    if (L_temp >= 0L)
      Rez = add(Rez, Exp);
    Exp = shr(Exp, (int16_t)1);
  }
  return Rez;
}


x86 assembly (clang, -O0):
<Sqrt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x4000,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xe,-0xc(%rbp)
jge    11da <Sqrt+0x9a>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <add@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <add@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
call   1030 <L_mult@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <L_sub@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jl     11bc <Sqrt+0x7c>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <add@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <shr@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1161 <Sqrt+0x21>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Sqrt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    $0x4000,%ebx
xor    %r15d,%r15d
mov    $0xe,%ebp
jmp    1173 <Sqrt+0x33>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <shr@plt>
mov    %eax,%ebx
add    $0xffffffff,%ebp
je     11b3 <Sqrt+0x73>
mov    %r15d,%edi
mov    %ebx,%esi
call   1040 <add@plt>
mov    %eax,%r12d
mov    %r15d,%edi
mov    %ebx,%esi
call   1040 <add@plt>
mov    %r12d,%edi
mov    %eax,%esi
call   1030 <L_mult@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <L_sub@plt>
test   %rax,%rax
js     1160 <Sqrt+0x20>
mov    %r15d,%edi
mov    %ebx,%esi
call   1040 <add@plt>
mov    %eax,%r15d
jmp    1160 <Sqrt+0x20>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @Sqrt(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i64 %0, i64* %2
store i32 0, i32* %4
store i32 16384, i32* %5
store i32 0, i32* %3
br label %8
8: ; preds = %31, %1
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 14
br i1 %10, label %11, label %34
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = call i32 @add(i32 %12, i32 %13)
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = call i32 @add(i32 %15, i32 %16)
%18 = call i64 @L_mult(i32 %14, i32 %17)
store i64 %18, i64* %6
%19 = load i64, i64* %2
%20 = load i64, i64* %6
%21 = call i64 @L_sub(i64 %19, i64 %20)
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = icmp sge i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %11
%25 = load i32, i32* %4
%26 = load i32, i32* %5
%27 = call i32 @add(i32 %25, i32 %26)
store i32 %27, i32* %4
br label %28
28: ; preds = %24, %11
%29 = load i32, i32* %5
%30 = call i32 @shr(i32 %29, i32 1)
store i32 %30, i32* %5
br label %31
31: ; preds = %28
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 1
store i32 %33, i32* %3
br label %8
34: ; preds = %8
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
define i32 @Sqrt(i64 %0) {
br label %2
2: ; preds = %1, %13
%3 = phi i32 [ 0, %1 ], [ %16, %13 ]
%4 = phi i32 [ 0, %1 ], [ %14, %13 ]
%5 = phi i32 [ 16384, %1 ], [ %15, %13 ]
%6 = tail call i32 @add(i32 %4, i32 %5)
%7 = tail call i32 @add(i32 %4, i32 %5)
%8 = tail call i64 @L_mult(i32 %6, i32 %7)
%9 = tail call i64 @L_sub(i64 %0, i64 %8)
%10 = icmp sgt i64 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @add(i32 %4, i32 %5)
br label %13
13: ; preds = %11, %2
%14 = phi i32 [ %12, %11 ], [ %4, %2 ]
%15 = tail call i32 @shr(i32 %5, i32 1)
%16 = add nuw nsw i32 %3, 1
%17 = icmp eq i32 %16, 14
br i1 %17, label %18, label %2
18: ; preds = %13
ret i32 %14
}