#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 u_int;

/* Variables and functions */
int AF_INET6;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
double log(int);
double pow(int, double);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ stderr;
int mask_discover(char *number, int af) {
  double lg, lg2, pwr;
  u_int i, sp;
  int mx;
  sp = atoi(number);
  if (sp == 0) {
    (void)fprintf(stderr, "invalid mask specification: %s\n", number);
    exit(1);
  }
  mx = (af == AF_INET6) ? 128 : 32;
  for (i = 0; i <= mx; i++) {
    pwr = pow(2, i);
    if (pwr >= sp)
      break;
  }
  lg = log((double)sp) / log(2);
  lg2 = log(pwr) / log(2);
  printf("theoretical len: %.30f\n", (double)(mx - lg));
  printf("working len:     %.0f\n", (double)(mx - lg2));
  printf("hosts:           %.0f\n", pow(2, lg2));
  return (0);
}


x86 assembly (clang, -O0):
<mask_discover>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    11a8 <mask_discover+0x48>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe67(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edx
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    $0x20,%eax
mov    $0x80,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jg     1219 <mask_discover+0xb9>
cvtsi2sdl -0x2c(%rbp),%xmm0
mov    $0x2,%edi
call   1060 <pow@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x30(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jb     1206 <mask_discover+0xa6>
jmp    1219 <mask_discover+0xb9>
jmp    120b <mask_discover+0xab>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11cd <mask_discover+0x6d>
cvtsi2sdl -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <log@plt>
movsd  %xmm0,-0x48(%rbp)
mov    $0x2,%edi
call   1050 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
cvttsd2si -0x28(%rbp),%edi
call   1050 <log@plt>
movsd  %xmm0,-0x40(%rbp)
mov    $0x2,%edi
call   1050 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
cvtsi2sdl -0x34(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
lea    0xd9e(%rip),%rdi
call   1030 <printf@plt>
cvtsi2sdl -0x34(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
lea    0xda0(%rip),%rdi
call   1030 <printf@plt>
movsd  -0x20(%rbp),%xmm0
mov    $0x2,%edi
call   1060 <pow@plt>
lea    0xd9c(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mask_discover>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
call   1070 <atoi@plt>
test   %eax,%eax
je     1283 <mask_discover+0x123>
mov    %eax,%r15d
mov    0x2e62(%rip),%rax
cmp    %ebp,(%rax)
mov    $0x80,%eax
mov    $0x20,%ebp
cmove  %eax,%ebp
cvtsi2sd %r15d,%xmm0
movsd  %xmm0,(%rsp)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
mov    $0x2,%edi
call   1060 <pow@plt>
ucomisd (%rsp),%xmm0
jae    11d1 <mask_discover+0x71>
lea    0x1(%rbx),%eax
cmp    %ebx,%ebp
mov    %eax,%ebx
jne    11b0 <mask_discover+0x50>
mov    %r15d,%edi
movsd  %xmm0,(%rsp)
call   1050 <log@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x2,%edi
call   1050 <log@plt>
movsd  0x8(%rsp),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
cvttsd2si (%rsp),%edi
call   1050 <log@plt>
movsd  %xmm0,(%rsp)
mov    $0x2,%edi
call   1050 <log@plt>
movsd  (%rsp),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
movsd  %xmm0,0x10(%rsp)
subsd  0x8(%rsp),%xmm0
lea    0xde1(%rip),%rdi
call   1030 <printf@plt>
movsd  0x10(%rsp),%xmm0
subsd  (%rsp),%xmm0
lea    0xde2(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%edi
movsd  (%rsp),%xmm0
call   1060 <pow@plt>
lea    0xdde(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"invalid mask specification: %s\0A\00"
@AF_INET6 = global i32 0
@.str.1 = [24 x i8] c"theoretical len: %.30f\0A\00"
@.str.2 = [23 x i8] c"working len: %.0f\0A\00"
@.str.3 = [23 x i8] c"hosts: %.0f\0A\00"
define i32 @mask_discover(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = load i8*, i8** %3
%12 = call i32 @atoi(i8* %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %4
%22 = load i32, i32* @AF_INET6
%23 = icmp eq i32 %21, %22
%24 = zext i1 %23 to i64
%25 = select i1 %23, i32 128, i32 32
store i32 %25, i32* %10
store i32 0, i32* %8
br label %26
26: ; preds = %40, %20
%27 = load i32, i32* %8
%28 = load i32, i32* %10
%29 = icmp sle i32 %27, %28
br i1 %29, label %30, label %43
30: ; preds = %26
%31 = load i32, i32* %8
%32 = sitofp i32 %31 to double
%33 = call double @pow(i32 2, double %32)
store double %33, double* %7
%34 = load double, double* %7
%35 = load i32, i32* %9
%36 = sitofp i32 %35 to double
%37 = fcmp oge double %34, %36
br i1 %37, label %38, label %39
38: ; preds = %30
br label %43
39: ; preds = %30
br label %40
40: ; preds = %39
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %26
43: ; preds = %38, %26
%44 = load i32, i32* %9
%45 = sitofp i32 %44 to double
%46 = fptosi double %45 to i32
%47 = call double @log(i32 %46)
%48 = call double @log(i32 2)
%49 = fdiv double %47, %48
store double %49, double* %5
%50 = load double, double* %7
%51 = fptosi double %50 to i32
%52 = call double @log(i32 %51)
%53 = call double @log(i32 2)
%54 = fdiv double %52, %53
store double %54, double* %6
%55 = load i32, i32* %10
%56 = sitofp i32 %55 to double
%57 = load double, double* %5
%58 = fsub double %56, %57
%59 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), double %58)
%60 = load i32, i32* %10
%61 = sitofp i32 %60 to double
%62 = load double, double* %6
%63 = fsub double %61, %62
%64 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), double %63)
%65 = load double, double* %6
%66 = call double @pow(i32 2, double %65)
%67 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), double %66)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"invalid mask specification: %s\0A\00"
@AF_INET6 = global i32 0
@.str.1 = [24 x i8] c"theoretical len: %.30f\0A\00"
@.str.2 = [23 x i8] c"working len: %.0f\0A\00"
@.str.3 = [23 x i8] c"hosts: %.0f\0A\00"
define i32 @mask_discover(i8* %0, i32 %1) {
%3 = tail call i32 @atoi(i8* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @AF_INET6
%11 = icmp eq i32 %10, %1
%12 = select i1 %11, i32 128, i32 32
%13 = sitofp i32 %3 to double
%14 = or i32 %12, 1
br label %15
15: ; preds = %15, %9
%16 = phi i32 [ 0, %9 ], [ %20, %15 ]
%17 = sitofp i32 %16 to double
%18 = tail call double @pow(i32 2, double %17)
%19 = fcmp oge double %18, %13
%20 = add nuw nsw i32 %16, 1
%21 = icmp eq i32 %20, %14
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %15
23: ; preds = %15
%24 = tail call double @log(i32 %3)
%25 = tail call double @log(i32 2)
%26 = fdiv double %24, %25
%27 = fptosi double %18 to i32
%28 = tail call double @log(i32 %27)
%29 = tail call double @log(i32 2)
%30 = fdiv double %28, %29
%31 = sitofp i32 %12 to double
%32 = fsub double %31, %26
%33 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), double %32)
%34 = fsub double %31, %30
%35 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), double %34)
%36 = tail call double @pow(i32 2, double %30)
%37 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), double %36)
ret i32 0
}