#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*/ ERANGE;
double __approx_log2(double);
int /*<<< orphan*/ errno;
scalar_t__ isinf(double);
scalar_t__ isnan(double);
double logb(double x) {
  if (isnan(x))
    return x;
  if (isinf(x))
    return x;
  if (!x) {
    errno = ERANGE;
    return 0;
  }
  return __approx_log2(x);
}


x86 assembly (clang, -O0):
<logb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
je     1160 <logb+0x30>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c9 <logb+0x99>
movsd  -0x10(%rbp),%xmm0
call   1050 <isinf@plt>
cmp    $0x0,%rax
je     1183 <logb+0x53>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c9 <logb+0x99>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11ba <logb+0x8a>
jp     11ba <logb+0x8a>
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2d(%rip),%rax
mov    %ecx,(%rax)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c9 <logb+0x99>
movsd  -0x10(%rbp),%xmm0
call   1030 <__approx_log2@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logb>:
push   %rax
movsd  %xmm0,(%rsp)
call   1040 <isnan@plt>
test   %rax,%rax
jne    114f <logb+0x1f>
movsd  (%rsp),%xmm0
call   1050 <isinf@plt>
test   %rax,%rax
je     1156 <logb+0x26>
movsd  (%rsp),%xmm0
pop    %rax
ret
xorpd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    1167 <logb+0x37>
jnp    1172 <logb+0x42>
movsd  (%rsp),%xmm0
pop    %rax
jmp    1030 <__approx_log2@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    0x2e56(%rip),%rcx
mov    %eax,(%rcx)
pop    %rax
ret

LLVM IR (-O0):
@ERANGE = global i32 0
@errno = global i32 0
define double @logb(double %0) {
%2 = alloca double
%3 = alloca double
store double %0, double* %3
%4 = load double, double* %3
%5 = call i64 @isnan(double %4)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load double, double* %3
store double %8, double* %2
br label %23
9: ; preds = %1
%10 = load double, double* %3
%11 = call i64 @isinf(double %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load double, double* %3
store double %14, double* %2
br label %23
15: ; preds = %9
%16 = load double, double* %3
%17 = fcmp une double %16, 0.000000e+00
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = load i32, i32* @ERANGE
store i32 %19, i32* @errno
store double 0.000000e+00, double* %2
br label %23
20: ; preds = %15
%21 = load double, double* %3
%22 = call double @__approx_log2(double %21)
store double %22, double* %2
br label %23
23: ; preds = %20, %18, %13, %7
%24 = load double, double* %2
ret double %24
}

LLVM IR (-O2):
@ERANGE = global i32 0
@errno = global i32 0
define double @logb(double %0) {
%2 = tail call i64 @isnan(double %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %13
4: ; preds = %1
%5 = tail call i64 @isinf(double %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = fcmp une double %0, 0.000000e+00
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = load i32, i32* @ERANGE
store i32 %10, i32* @errno
br label %13
11: ; preds = %7
%12 = tail call double @__approx_log2(double %0)
br label %13
13: ; preds = %4, %1, %11, %9
%14 = phi double [ %12, %11 ], [ 0.000000e+00, %9 ], [ %0, %1 ], [ %0, %4 ]
ret double %14
}