#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*/ fprintf(int /*<<< orphan*/, char *, ...);
double *malloc(int);
int /*<<< orphan*/ memcpy(double *, double *, int);
int /*<<< orphan*/ stderr;
double *lookup_item_memory(double **item_memory, int im_length, int raw_key,
                           int len, double precision) {
  int key = (int)((double)raw_key * precision);
  if (key < im_length) {
    double *random_hv = malloc(im_length * sizeof(double));
    if (!random_hv)
      goto mem_error;
    memcpy(random_hv, item_memory[key], len);
    return random_hv;
  } else {
    fprintf(stderr, "lookup_item_memory: cannot find key: %d\n", key);
    return NULL;
  }
mem_error:
  fprintf(stderr, "lookup_item_memory: failed to allocate memory\n");
  return NULL;
}


x86 assembly (clang, -O0):
<lookup_item_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movsd  %xmm0,-0x28(%rbp)
cvtsi2sdl -0x18(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11af <lookup_item_memory+0x7f>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    118a <lookup_item_memory+0x5a>
jmp    11d6 <lookup_item_memory+0xa6>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f5 <lookup_item_memory+0xc5>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f5 <lookup_item_memory+0xc5>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_item_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cvtsi2sd %edx,%xmm1
mulsd  %xmm0,%xmm1
cvttsd2si %xmm1,%ebp
cmp    %esi,%ebp
jge    1173 <lookup_item_memory+0x43>
mov    %ecx,%r14d
mov    %rdi,%r15
shl    $0x3,%esi
mov    %esi,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1190 <lookup_item_memory+0x60>
mov    %rax,%rbx
movslq %ebp,%rax
mov    (%r15,%rax,8),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1040 <memcpy@plt>
jmp    11a9 <lookup_item_memory+0x79>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11a9 <lookup_item_memory+0x79>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"lookup_item_memory: cannot find key: %d\0A\00"
@.str.1 = [47 x i8] c"lookup_item_memory: failed to allocate memory\0A\00"
define double* @lookup_item_memory(double** %0, i32 %1, i32 %2, i32 %3, double %4) {
%6 = alloca double*
%7 = alloca double**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca double
%12 = alloca i32
%13 = alloca double*
store double** %0, double*** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store double %4, double* %11
%14 = load i32, i32* %9
%15 = sitofp i32 %14 to double
%16 = load double, double* %11
%17 = fmul double %15, %16
%18 = fptosi double %17 to i32
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = load i32, i32* %8
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %41
22: ; preds = %5
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = mul i64 %24, 8
%26 = trunc i64 %25 to i32
%27 = call double* @malloc(i32 %26)
store double* %27, double** %13
%28 = load double*, double** %13
%29 = icmp ne double* %28, null
br i1 %29, label %31, label %30
30: ; preds = %22
br label %45
31: ; preds = %22
%32 = load double*, double** %13
%33 = load double**, double*** %7
%34 = load i32, i32* %12
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double*, double** %33, i64 %35
%37 = load double*, double** %36
%38 = load i32, i32* %10
%39 = call i32 @memcpy(double* %32, double* %37, i32 %38)
%40 = load double*, double** %13
store double* %40, double** %6
br label %48
41: ; preds = %5
%42 = load i32, i32* @stderr
%43 = load i32, i32* %12
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %43)
store double* null, double** %6
br label %48
45: ; preds = %30
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store double* null, double** %6
br label %48
48: ; preds = %45, %41, %31
%49 = load double*, double** %6
ret double* %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"lookup_item_memory: cannot find key: %d\0A\00"
@.str.1 = [47 x i8] c"lookup_item_memory: failed to allocate memory\0A\00"
define double* @lookup_item_memory(double** nocapture %0, i32 %1, i32 %2, i32 %3, double %4) {
%6 = sitofp i32 %2 to double
%7 = fmul double %6, %4
%8 = fptosi double %7 to i32
%9 = icmp slt i32 %8, %1
br i1 %9, label %10, label %19
10: ; preds = %5
%11 = shl i32 %1, 3
%12 = tail call double* @malloc(i32 %11)
%13 = icmp eq double* %12, null
br i1 %13, label %22, label %14
14: ; preds = %10
%15 = sext i32 %8 to i64
%16 = getelementptr inbounds double*, double** %0, i64 %15
%17 = load double*, double** %16
%18 = tail call i32 @memcpy(double* %12, double* %17, i32 %3)
br label %25
19: ; preds = %5
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %25
22: ; preds = %10
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %14, %22, %19
%26 = phi double* [ null, %22 ], [ %12, %14 ], [ null, %19 ]
ret double* %26
}