#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 FP_ILOGB0;
int __CCAPRICE_MATH_HI_L(double);
int __CCAPRICE_MATH_LO_L(double);
int ilogb(double x) {
  if (x == 0) {
    return FP_ILOGB0;
  }
  int ix = 0;
  int hx = __CCAPRICE_MATH_HI_L(x) & 0x7FFFFFFF;
  int lx = __CCAPRICE_MATH_LO_L(x);
  if (hx < 0x00100000) {
    if ((hx | lx) == 0)
      return 0x80000001;
    else {
      if (hx == 0)
        for (ix = -1043; lx > 0; lx <<= 1)
          ix--;
      else
        for (ix = -1022, hx <<= 11; hx > 0; hx <<= 1)
          ix--;
      return ix;
    }
  }
  return (hx < 0x7FF00000) ? (hx >> 20) - 1023 : 0x7FFFFFFF;
}


x86 assembly (clang, -O0):
<ilogb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1156 <ilogb+0x36>
jp     1156 <ilogb+0x36>
mov    0x2e94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124f <ilogb+0x12f>
movl   $0x0,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1040 <__CCAPRICE_MATH_HI_L@plt>
and    $0x7fffffff,%eax
mov    %eax,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <__CCAPRICE_MATH_LO_L@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x100000,-0x18(%rbp)
jge    121c <ilogb+0xfc>
mov    -0x18(%rbp),%eax
or     -0x1c(%rbp),%eax
cmp    $0x0,%eax
jne    11a4 <ilogb+0x84>
movl   $0x80000001,-0x4(%rbp)
jmp    124f <ilogb+0x12f>
cmpl   $0x0,-0x18(%rbp)
jne    11db <ilogb+0xbb>
movl   $0xfffffbed,-0x14(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11d6 <ilogb+0xb6>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b5 <ilogb+0x95>
jmp    1211 <ilogb+0xf1>
movl   $0xfffffc02,-0x14(%rbp)
mov    -0x18(%rbp),%eax
shl    $0xb,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    120c <ilogb+0xec>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11eb <ilogb+0xcb>
jmp    1211 <ilogb+0xf1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124f <ilogb+0x12f>
cmpl   $0x7ff00000,-0x18(%rbp)
jge    123c <ilogb+0x11c>
mov    -0x18(%rbp),%eax
sar    $0x14,%eax
sub    $0x3ff,%eax
mov    %eax,-0x20(%rbp)
jmp    1249 <ilogb+0x129>
mov    $0x7fffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    1249 <ilogb+0x129>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ilogb>:
push   %rbp
push   %rbx
push   %rax
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    113a <ilogb+0x1a>
jp     113a <ilogb+0x1a>
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
jmp    11ab <ilogb+0x8b>
movsd  %xmm0,(%rsp)
call   1040 <__CCAPRICE_MATH_HI_L@plt>
mov    %eax,%ebx
mov    %eax,%ebp
and    $0x7fffffff,%ebp
movsd  (%rsp),%xmm0
call   1030 <__CCAPRICE_MATH_LO_L@plt>
cmp    $0xfffff,%ebp
ja     118b <ilogb+0x6b>
mov    %eax,%ecx
mov    %ebp,%eax
or     %ecx,%eax
je     11a6 <ilogb+0x86>
test   %ebp,%ebp
je     11b2 <ilogb+0x92>
shl    $0xb,%ebx
mov    $0xfffffc02,%eax
test   %ebx,%ebx
jle    11ab <ilogb+0x8b>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%eax
add    %ebx,%ebx
test   %ebx,%ebx
jg     1180 <ilogb+0x60>
jmp    11ab <ilogb+0x8b>
mov    %ebp,%ecx
shr    $0x14,%ecx
add    $0xfffffc01,%ecx
cmp    $0x7ff00000,%ebp
mov    $0x7fffffff,%eax
cmovb  %ecx,%eax
jmp    11ab <ilogb+0x8b>
mov    $0x80000001,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0xfffffbed,%eax
test   %ecx,%ecx
jle    11ab <ilogb+0x8b>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%eax
add    %ecx,%ecx
test   %ecx,%ecx
jg     11c0 <ilogb+0xa0>
jmp    11ab <ilogb+0x8b>

LLVM IR (-O0):
@FP_ILOGB0 = global i32 0
define i32 @ilogb(double %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store double %0, double* %3
%7 = load double, double* %3
%8 = fcmp oeq double %7, 0.000000e+00
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i32, i32* @FP_ILOGB0
store i32 %10, i32* %2
br label %64
11: ; preds = %1
store i32 0, i32* %4
%12 = load double, double* %3
%13 = call i32 @__CCAPRICE_MATH_HI_L(double %12)
%14 = and i32 %13, 2147483647
store i32 %14, i32* %5
%15 = load double, double* %3
%16 = call i32 @__CCAPRICE_MATH_LO_L(double %15)
store i32 %16, i32* %6
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 1048576
br i1 %18, label %19, label %54
19: ; preds = %11
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = or i32 %20, %21
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -2147483647, i32* %2
br label %64
25: ; preds = %19
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %25
store i32 -1043, i32* %4
br label %29
29: ; preds = %35, %28
%30 = load i32, i32* %6
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i32, i32* %4
%34 = add nsw i32 %33, -1
store i32 %34, i32* %4
br label %35
35: ; preds = %32
%36 = load i32, i32* %6
%37 = shl i32 %36, 1
store i32 %37, i32* %6
br label %29
38: ; preds = %29
br label %52
39: ; preds = %25
store i32 -1022, i32* %4
%40 = load i32, i32* %5
%41 = shl i32 %40, 11
store i32 %41, i32* %5
br label %42
42: ; preds = %48, %39
%43 = load i32, i32* %5
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %42
%46 = load i32, i32* %4
%47 = add nsw i32 %46, -1
store i32 %47, i32* %4
br label %48
48: ; preds = %45
%49 = load i32, i32* %5
%50 = shl i32 %49, 1
store i32 %50, i32* %5
br label %42
51: ; preds = %42
br label %52
52: ; preds = %51, %38
%53 = load i32, i32* %4
store i32 %53, i32* %2
br label %64
54: ; preds = %11
%55 = load i32, i32* %5
%56 = icmp slt i32 %55, 2146435072
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = load i32, i32* %5
%59 = ashr i32 %58, 20
%60 = sub nsw i32 %59, 1023
br label %62
61: ; preds = %54
br label %62
62: ; preds = %61, %57
%63 = phi i32 [ %60, %57 ], [ 2147483647, %61 ]
store i32 %63, i32* %2
br label %64
64: ; preds = %62, %52, %24, %9
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
@FP_ILOGB0 = global i32 0
define i32 @ilogb(double %0) {
%2 = fcmp oeq double %0, 0.000000e+00
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i32, i32* @FP_ILOGB0
br label %37
5: ; preds = %1
%6 = tail call i32 @__CCAPRICE_MATH_HI_L(double %0)
%7 = and i32 %6, 2147483647
%8 = tail call i32 @__CCAPRICE_MATH_LO_L(double %0)
%9 = icmp ult i32 %7, 1048576
br i1 %9, label %10, label %32
10: ; preds = %5
%11 = or i32 %7, %8
%12 = icmp eq i32 %11, 0
br i1 %12, label %37, label %13
13: ; preds = %10
%14 = icmp eq i32 %7, 0
br i1 %14, label %15, label %23
15: ; preds = %13
%16 = icmp sgt i32 %8, 0
br i1 %16, label %17, label %37
17: ; preds = %15, %17
%18 = phi i32 [ %21, %17 ], [ %8, %15 ]
%19 = phi i32 [ %20, %17 ], [ -1043, %15 ]
%20 = add nsw i32 %19, -1
%21 = shl nuw i32 %18, 1
%22 = icmp sgt i32 %21, 0
br i1 %22, label %17, label %37
23: ; preds = %13
%24 = shl i32 %6, 11
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %23, %26
%27 = phi i32 [ %30, %26 ], [ %24, %23 ]
%28 = phi i32 [ %29, %26 ], [ -1022, %23 ]
%29 = add nsw i32 %28, -1
%30 = shl nuw i32 %27, 1
%31 = icmp sgt i32 %30, 0
br i1 %31, label %26, label %37
32: ; preds = %5
%33 = icmp ult i32 %7, 2146435072
%34 = lshr i32 %7, 20
%35 = add nsw i32 %34, -1023
%36 = select i1 %33, i32 %35, i32 2147483647
br label %37
37: ; preds = %26, %17, %23, %15, %32, %10, %3
%38 = phi i32 [ %4, %3 ], [ %36, %32 ], [ -2147483647, %10 ], [ -1043, %15 ], [ -1022, %23 ], [ %20, %17 ], [ %29, %26 ]
ret i32 %38
}