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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ errx(int, char *, int);
int /*<<< orphan*/ fill_icsi_log_table(float *, int);
int /*<<< orphan*/ free(float *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  uint32_t precision = 14;
  if (argc > 1)
    precision = atoi(argv[1]);
  if (precision > 18)
    errx(1, "Invalid precision %d, must be [0,18]", precision);
  float *lookup_table;
  int table_size = (1 << precision);
  lookup_table = (float *)malloc(sizeof(float) * table_size);
  fill_icsi_log_table(lookup_table, precision);
  printf("/* fast log lookup table, precision %d, size %d */\n", precision,
         table_size);
  printf("const uint32_t fast_log_precision = %d;\n", precision);
  printf("const uint32_t fast_log_table_size = %d;\n", table_size);
  printf("const float fast_log_table[%d] = {\n", table_size);
  int32_t i;
  for (i = 0; i < table_size; ++i)
    printf("%e, ", lookup_table[i]);
  printf(" };\n");
  free(lookup_table);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xe,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1197 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x12,-0x14(%rbp)
jle    11b5 <main+0x55>
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
lea    0xe50(%rip),%rsi
call   1040 <errx@plt>
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <fill_icsi_log_table@plt>
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1268 <main+0x108>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe7d(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    122f <main+0xcf>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xe,%r14d
cmp    $0x2,%edi
jl     11a7 <main+0x37>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
cmp    $0x13,%eax
jl     11a7 <main+0x37>
lea    0xe66(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
call   1050 <errx@plt>
mov    $0x1,%r15d
mov    $0x1,%ebp
mov    %r14d,%ecx
shl    %cl,%ebp
lea    0x0(,%rbp,4),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14d,%esi
call   1070 <fill_icsi_log_table@plt>
lea    0xe4d(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe6e(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe86(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xea0(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x1f,%r14d
je     124c <main+0xdc>
cmp    $0x2,%ebp
cmovge %ebp,%r15d
xor    %ebp,%ebp
lea    0xea5(%rip),%r14
nopl   0x0(%rax,%rax,1)
movss  (%rbx,%rbp,4),%xmm0
cvtss2sd %xmm0,%xmm0
mov    %r14,%rdi
mov    $0x1,%al
call   1060 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1230 <main+0xc0>
lea    0xe82(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Invalid precision %d, must be [0,18]\00"
@.str.1 = [52 x i8] c"/* fast log lookup table, precision %d, size %d */\0A\00"
@.str.2 = [41 x i8] c"const uint32_t fast_log_precision = %d;\0A\00"
@.str.3 = [42 x i8] c"const uint32_t fast_log_table_size = %d;\0A\00"
@.str.4 = [36 x i8] c"const float fast_log_table[%d] = {\0A\00"
@.str.5 = [5 x i8] c"%e, \00"
@.str.6 = [5 x i8] c" };\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca float*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 14, i32* %6
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %6
br label %17
17: ; preds = %12, %2
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 18
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %6
%22 = call i32 @errx(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %17
%24 = load i32, i32* %6
%25 = shl i32 1, %24
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = mul i64 4, %27
%29 = trunc i64 %28 to i32
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to float*
store float* %31, float** %7
%32 = load float*, float** %7
%33 = load i32, i32* %6
%34 = call i32 @fill_icsi_log_table(float* %32, i32 %33)
%35 = load i32, i32* %6
%36 = load i32, i32* %8
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 %36)
%38 = load i32, i32* %6
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %8
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %42)
store i32 0, i32* %9
br label %44
44: ; preds = %56, %23
%45 = load i32, i32* %9
%46 = load i32, i32* %8
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %59
48: ; preds = %44
%49 = load float*, float** %7
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds float, float* %49, i64 %51
%53 = load float, float* %52
%54 = fpext float %53 to double
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), double %54)
br label %56
56: ; preds = %48
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 1
store i32 %58, i32* %9
br label %44
59: ; preds = %44
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%61 = load float*, float** %7
%62 = call i32 @free(float* %61)
ret i32 0
}

LLVM IR (-O2):
@.str = [37 x i8] c"Invalid precision %d, must be [0,18]\00"
@.str.1 = [52 x i8] c"/* fast log lookup table, precision %d, size %d */\0A\00"
@.str.2 = [41 x i8] c"const uint32_t fast_log_precision = %d;\0A\00"
@.str.3 = [42 x i8] c"const uint32_t fast_log_table_size = %d;\0A\00"
@.str.4 = [36 x i8] c"const float fast_log_table[%d] = {\0A\00"
@.str.5 = [5 x i8] c"%e, \00"
@str = [4 x i8] c" };\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %11
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = icmp sgt i32 %7, 18
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %11
11: ; preds = %2, %9, %4
%12 = phi i32 [ %7, %9 ], [ %7, %4 ], [ 14, %2 ]
%13 = shl nuw i32 1, %12
%14 = shl i32 %13, 2
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to float*
%17 = tail call i32 @fill_icsi_log_table(float* %16, i32 %12)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %13)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%22 = icmp eq i32 %12, 31
br i1 %22, label %34, label %23
23: ; preds = %11
%24 = call i32 @llvm.smax.i32(i32 %13, i32 1)
%25 = zext i32 %24 to i64
br label %26
26: ; preds = %23, %26
%27 = phi i64 [ 0, %23 ], [ %32, %26 ]
%28 = getelementptr inbounds float, float* %16, i64 %27
%29 = load float, float* %28
%30 = fpext float %29 to double
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), double %30)
%32 = add nuw nsw i64 %27, 1
%33 = icmp eq i64 %32, %25
br i1 %33, label %34, label %26
34: ; preds = %26, %11
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%36 = tail call i32 @free(float* %16)
ret i32 0
}