#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 */
double log(int);
int pow(int, unsigned long);
unsigned int i_to_T(unsigned int i, unsigned int T_size, unsigned int D_size) {
  unsigned long int hi = D_size;
  double row_d = log(i + 1) / log(2);
  unsigned long int row = (unsigned long int)(row_d);
  unsigned long int prev = pow(2, row) - 1;
  unsigned long int i_row = i - prev;
  unsigned long int hi_v = 2 * i_row + 1;
  unsigned long int lo_v = pow(2, row + 1) - (2 * i_row + 1);
  unsigned long int div = pow(2, row + 1);
  unsigned long int r = (hi_v * hi - lo_v) / div;
  return r;
}


x86 assembly (clang, -O0):
<i_to_T>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1030 <log@plt>
movsd  %xmm0,-0x60(%rbp)
mov    $0x2,%edi
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x60(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xe90(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rcx
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    $0x2,%edi
call   1040 <pow@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%eax
sub    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    $0x2,%edi
call   1040 <pow@plt>
cltq
mov    -0x38(%rbp),%rcx
shl    $0x1,%rcx
add    $0x1,%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    $0x2,%edi
call   1040 <pow@plt>
cltq
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rax
imul   -0x18(%rbp),%rax
sub    -0x48(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x50(%rbp)
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i_to_T>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    %edx,%r15d
lea    0x1(%r14),%edi
call   1030 <log@plt>
movapd %xmm0,(%rsp)
mov    $0x2,%edi
call   1030 <log@plt>
movapd (%rsp),%xmm1
divsd  %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rcx
sar    $0x3f,%rcx
subsd  0xe9a(%rip),%xmm1
cvttsd2si %xmm1,%rbx
and    %rcx,%rbx
or     %rax,%rbx
mov    $0x2,%edi
mov    %rbx,%rsi
call   1040 <pow@plt>
add    $0xffffffff,%eax
cltq
mov    %r14d,%ecx
sub    %rax,%rcx
lea    (%rcx,%rcx,1),%r14
add    $0x1,%r14
add    $0x1,%rbx
mov    $0x2,%edi
mov    %rbx,%rsi
call   1040 <pow@plt>
cltq
mov    %r14,%r12
sub    %rax,%r12
mov    $0x2,%edi
mov    %rbx,%rsi
call   1040 <pow@plt>
mov    %eax,%ecx
movslq %eax,%rsi
imul   %r14,%r15
add    %r12,%r15
mov    %r15,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     11d9 <i_to_T+0xb9>
mov    %r15,%rax
xor    %edx,%edx
div    %rsi
jmp    11e0 <i_to_T+0xc0>
mov    %r15d,%eax
xor    %edx,%edx
div    %ecx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @i_to_T(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca double
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%16 = load i32, i32* %6
%17 = zext i32 %16 to i64
store i64 %17, i64* %7
%18 = load i32, i32* %4
%19 = add i32 %18, 1
%20 = call double @log(i32 %19)
%21 = call double @log(i32 2)
%22 = fdiv double %20, %21
store double %22, double* %8
%23 = load double, double* %8
%24 = fptoui double %23 to i64
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = call i32 @pow(i32 2, i64 %25)
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
store i64 %28, i64* %10
%29 = load i32, i32* %4
%30 = zext i32 %29 to i64
%31 = load i64, i64* %10
%32 = sub i64 %30, %31
store i64 %32, i64* %11
%33 = load i64, i64* %11
%34 = mul i64 2, %33
%35 = add i64 %34, 1
store i64 %35, i64* %12
%36 = load i64, i64* %9
%37 = add i64 %36, 1
%38 = call i32 @pow(i32 2, i64 %37)
%39 = sext i32 %38 to i64
%40 = load i64, i64* %11
%41 = mul i64 2, %40
%42 = add i64 %41, 1
%43 = sub i64 %39, %42
store i64 %43, i64* %13
%44 = load i64, i64* %9
%45 = add i64 %44, 1
%46 = call i32 @pow(i32 2, i64 %45)
%47 = sext i32 %46 to i64
store i64 %47, i64* %14
%48 = load i64, i64* %12
%49 = load i64, i64* %7
%50 = mul i64 %48, %49
%51 = load i64, i64* %13
%52 = sub i64 %50, %51
%53 = load i64, i64* %14
%54 = udiv i64 %52, %53
store i64 %54, i64* %15
%55 = load i64, i64* %15
%56 = trunc i64 %55 to i32
ret i32 %56
}

LLVM IR (-O2):
define i32 @i_to_T(i32 %0, i32 %1, i32 %2) {
%4 = zext i32 %2 to i64
%5 = add i32 %0, 1
%6 = tail call double @log(i32 %5)
%7 = tail call double @log(i32 2)
%8 = fdiv double %6, %7
%9 = fptoui double %8 to i64
%10 = tail call i32 @pow(i32 2, i64 %9)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = zext i32 %0 to i64
%14 = sub nsw i64 %13, %12
%15 = shl nsw i64 %14, 1
%16 = or i64 %15, 1
%17 = add i64 %9, 1
%18 = tail call i32 @pow(i32 2, i64 %17)
%19 = sext i32 %18 to i64
%20 = sub nsw i64 %16, %19
%21 = tail call i32 @pow(i32 2, i64 %17)
%22 = sext i32 %21 to i64
%23 = mul i64 %16, %4
%24 = add i64 %20, %23
%25 = udiv i64 %24, %22
%26 = trunc i64 %25 to i32
ret i32 %26
}