#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*/ assert(int);
double *gmalloc(int);
void vector_take(int n, double *v, int m, int *p, double **u) {
  int i;
  if (!*u)
    *u = gmalloc(sizeof(double) * m);
  for (i = 0; i < m; i++) {
    assert(p[i] < n && p[i] >= 0);
    (*u)[i] = v[p[i]];
  }
}


x86 assembly (clang, -O0):
<vector_take>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1161 <vector_take+0x41>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <gmalloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11df <vector_take+0xbf>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
xor    %eax,%eax
cmp    -0x4(%rbp),%ecx
mov    %al,-0x2d(%rbp)
jge    119f <vector_take+0x7f>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
setge  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
movslq (%rcx,%rdx,4),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movslq -0x2c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1168 <vector_take+0x48>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vector_take>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %rsi,%r15
mov    %edi,%r13d
cmpq   $0x0,(%r8)
je     1147 <vector_take+0x27>
test   %r12d,%r12d
jg     115c <vector_take+0x3c>
jmp    11a3 <vector_take+0x83>
lea    0x0(,%r12,8),%edi
call   1030 <gmalloc@plt>
mov    %rax,(%r14)
test   %r12d,%r12d
jle    11a3 <vector_take+0x83>
mov    %r12d,%r12d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,4),%eax
cmp    %r13d,%eax
setl   %cl
test   %eax,%eax
setns  %al
and    %cl,%al
movzbl %al,%edi
call   1040 <assert@plt>
movslq (%rbx,%rbp,4),%rax
movsd  (%r15,%rax,8),%xmm0
mov    (%r14),%rax
movsd  %xmm0,(%rax,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1170 <vector_take+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @vector_take(i32 %0, double* %1, i32 %2, i32* %3, double** %4) {
%6 = alloca i32
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca double**
%11 = alloca i32
store i32 %0, i32* %6
store double* %1, double** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store double** %4, double*** %10
%12 = load double**, double*** %10
%13 = load double*, double** %12
%14 = icmp ne double* %13, null
br i1 %14, label %22, label %15
15: ; preds = %5
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = mul i64 8, %17
%19 = trunc i64 %18 to i32
%20 = call double* @gmalloc(i32 %19)
%21 = load double**, double*** %10
store double* %20, double** %21
br label %22
22: ; preds = %15, %5
store i32 0, i32* %11
br label %23
23: ; preds = %60, %22
%24 = load i32, i32* %11
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %63
27: ; preds = %23
%28 = load i32*, i32** %9
%29 = load i32, i32* %11
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %42
35: ; preds = %27
%36 = load i32*, i32** %9
%37 = load i32, i32* %11
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = icmp sge i32 %40, 0
br label %42
42: ; preds = %35, %27
%43 = phi i1 [ false, %27 ], [ %41, %35 ]
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = load double*, double** %7
%47 = load i32*, i32** %9
%48 = load i32, i32* %11
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = load i32, i32* %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds double, double* %46, i64 %52
%54 = load double, double* %53
%55 = load double**, double*** %10
%56 = load double*, double** %55
%57 = load i32, i32* %11
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds double, double* %56, i64 %58
store double %54, double* %59
br label %60
60: ; preds = %42
%61 = load i32, i32* %11
%62 = add nsw i32 %61, 1
store i32 %62, i32* %11
br label %23
63: ; preds = %23
ret void
}

LLVM IR (-O2):
define void @vector_take(i32 %0, double* nocapture %1, i32 %2, i32* nocapture %3, double** nocapture %4) {
%6 = load double*, double** %4
%7 = icmp eq double* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = shl i32 %2, 3
%10 = tail call double* @gmalloc(i32 %9)
store double* %10, double** %4
br label %11
11: ; preds = %8, %5
%12 = icmp sgt i32 %2, 0
br i1 %12, label %13, label %32
13: ; preds = %11
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %30, %15 ]
%17 = getelementptr inbounds i32, i32* %3, i64 %16
%18 = load i32, i32* %17
%19 = icmp slt i32 %18, %0
%20 = icmp sgt i32 %18, -1
%21 = and i1 %19, %20
%22 = zext i1 %21 to i32
%23 = tail call i32 @assert(i32 %22)
%24 = load i32, i32* %17
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds double, double* %1, i64 %25
%27 = load double, double* %26
%28 = load double*, double** %4
%29 = getelementptr inbounds double, double* %28, i64 %16
store double %27, double* %29
%30 = add nuw nsw i64 %16, 1
%31 = icmp eq i64 %30, %14
br i1 %31, label %32, label %15
32: ; preds = %15, %11
ret void
}