#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 */
scalar_t__ earth_range(double);
int glbStaceyProfile(double, int, double **, double **);
int /*<<< orphan*/ glb_free(double *);
scalar_t__ glb_malloc(int);
int glbAverageDensityProfile(double baseline, double **length,
                             double **density) {
  double sum, *l, *d, res;
  int s, i, st = 1000;
  sum = 0.0;
  if (earth_range(baseline) != 0)
    return -1;
  s = glbStaceyProfile(baseline, st, &l, &d);
  if (s != 0)
    return -1;
  for (i = 0; i < st; i++)
    sum += d[i];
  glb_free(l);
  glb_free(d);
  res = (double)sum / st;
  l = (double *)glb_malloc(sizeof(double));
  d = (double *)glb_malloc(sizeof(double));
  l[0] = baseline;
  d[0] = res;
  *density = d;
  *length = l;
  return 0;
}


x86 assembly (clang, -O0):
<glbAverageDensityProfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
movl   $0x3e8,-0x4c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <earth_range@plt>
cmp    $0x0,%rax
je     1184 <glbAverageDensityProfile+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <glbAverageDensityProfile+0x122>
movsd  -0x10(%rbp),%xmm0
mov    -0x4c(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1030 <glbStaceyProfile@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     11b2 <glbAverageDensityProfile+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <glbAverageDensityProfile+0x122>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    11ea <glbAverageDensityProfile+0xaa>
mov    -0x38(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11b9 <glbAverageDensityProfile+0x79>
mov    -0x30(%rbp),%rdi
call   1060 <glb_free@plt>
mov    -0x38(%rbp),%rdi
call   1060 <glb_free@plt>
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x4c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    $0x8,%edi
call   1040 <glb_malloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x8,%edi
call   1040 <glb_malloc@plt>
mov    %rax,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm0
mov    -0x30(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x40(%rbp),%xmm0
mov    -0x38(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glbAverageDensityProfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movsd  %xmm0,0x10(%rsp)
call   1050 <earth_range@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    123f <glbAverageDensityProfile+0xff>
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
movsd  0x10(%rsp),%xmm0
mov    $0x3e8,%edi
call   1030 <glbStaceyProfile@plt>
test   %eax,%eax
jne    123f <glbAverageDensityProfile+0xff>
xorpd  %xmm0,%xmm0
mov    $0x7,%eax
mov    0x18(%rsp),%rcx
nopw   0x0(%rax,%rax,1)
addsd  -0x38(%rcx,%rax,8),%xmm0
addsd  -0x30(%rcx,%rax,8),%xmm0
addsd  -0x28(%rcx,%rax,8),%xmm0
addsd  -0x20(%rcx,%rax,8),%xmm0
addsd  -0x18(%rcx,%rax,8),%xmm0
addsd  -0x10(%rcx,%rax,8),%xmm0
addsd  -0x8(%rcx,%rax,8),%xmm0
addsd  (%rcx,%rax,8),%xmm0
add    $0x8,%rax
cmp    $0x3ef,%rax
jne    11a0 <glbAverageDensityProfile+0x60>
mov    0x8(%rsp),%rdi
movsd  %xmm0,(%rsp)
call   1060 <glb_free@plt>
mov    0x18(%rsp),%rdi
call   1060 <glb_free@plt>
movsd  (%rsp),%xmm0
divsd  0xdff(%rip),%xmm0
movsd  %xmm0,(%rsp)
mov    $0x8,%edi
call   1040 <glb_malloc@plt>
mov    %rax,0x8(%rsp)
mov    $0x8,%edi
call   1040 <glb_malloc@plt>
mov    0x8(%rsp),%rcx
movsd  0x10(%rsp),%xmm0
movsd  %xmm0,(%rcx)
movsd  (%rsp),%xmm0
movsd  %xmm0,(%rax)
mov    %rax,(%r14)
mov    %rcx,(%rbx)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @glbAverageDensityProfile(double %0, double** %1, double** %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca double**
%7 = alloca double**
%8 = alloca double
%9 = alloca double*
%10 = alloca double*
%11 = alloca double
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store double %0, double* %5
store double** %1, double*** %6
store double** %2, double*** %7
store i32 1000, i32* %14
store double 0.000000e+00, double* %8
%15 = load double, double* %5
%16 = call i64 @earth_range(double %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %65
19: ; preds = %3
%20 = load double, double* %5
%21 = load i32, i32* %14
%22 = call i32 @glbStaceyProfile(double %20, i32 %21, double** %9, double** %10)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -1, i32* %4
br label %65
26: ; preds = %19
store i32 0, i32* %13
br label %27
27: ; preds = %39, %26
%28 = load i32, i32* %13
%29 = load i32, i32* %14
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %42
31: ; preds = %27
%32 = load double*, double** %10
%33 = load i32, i32* %13
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %32, i64 %34
%36 = load double, double* %35
%37 = load double, double* %8
%38 = fadd double %37, %36
store double %38, double* %8
br label %39
39: ; preds = %31
%40 = load i32, i32* %13
%41 = add nsw i32 %40, 1
store i32 %41, i32* %13
br label %27
42: ; preds = %27
%43 = load double*, double** %9
%44 = call i32 @glb_free(double* %43)
%45 = load double*, double** %10
%46 = call i32 @glb_free(double* %45)
%47 = load double, double* %8
%48 = load i32, i32* %14
%49 = sitofp i32 %48 to double
%50 = fdiv double %47, %49
store double %50, double* %11
%51 = call i64 @glb_malloc(i32 8)
%52 = inttoptr i64 %51 to double*
store double* %52, double** %9
%53 = call i64 @glb_malloc(i32 8)
%54 = inttoptr i64 %53 to double*
store double* %54, double** %10
%55 = load double, double* %5
%56 = load double*, double** %9
%57 = getelementptr inbounds double, double* %56, i64 0
store double %55, double* %57
%58 = load double, double* %11
%59 = load double*, double** %10
%60 = getelementptr inbounds double, double* %59, i64 0
store double %58, double* %60
%61 = load double*, double** %10
%62 = load double**, double*** %7
store double* %61, double** %62
%63 = load double*, double** %9
%64 = load double**, double*** %6
store double* %63, double** %64
store i32 0, i32* %4
br label %65
65: ; preds = %42, %25, %18
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
define i32 @glbAverageDensityProfile(double %0, double** nocapture writeonly %1, double** nocapture writeonly %2) {
%4 = alloca double*
%5 = alloca double*
%6 = bitcast double** %4 to i8*
%7 = bitcast double** %5 to i8*
%8 = tail call i64 @earth_range(double %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %62
10: ; preds = %3
%11 = call i32 @glbStaceyProfile(double %0, i32 1000, double** %4, double** %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %62
13: ; preds = %10
%14 = load double*, double** %5
br label %15
15: ; preds = %15, %13
%16 = phi i64 [ 0, %13 ], [ %49, %15 ]
%17 = phi double [ 0.000000e+00, %13 ], [ %48, %15 ]
%18 = getelementptr inbounds double, double* %14, i64 %16
%19 = load double, double* %18
%20 = fadd double %17, %19
%21 = or i64 %16, 1
%22 = getelementptr inbounds double, double* %14, i64 %21
%23 = load double, double* %22
%24 = fadd double %20, %23
%25 = or i64 %16, 2
%26 = getelementptr inbounds double, double* %14, i64 %25
%27 = load double, double* %26
%28 = fadd double %24, %27
%29 = or i64 %16, 3
%30 = getelementptr inbounds double, double* %14, i64 %29
%31 = load double, double* %30
%32 = fadd double %28, %31
%33 = or i64 %16, 4
%34 = getelementptr inbounds double, double* %14, i64 %33
%35 = load double, double* %34
%36 = fadd double %32, %35
%37 = or i64 %16, 5
%38 = getelementptr inbounds double, double* %14, i64 %37
%39 = load double, double* %38
%40 = fadd double %36, %39
%41 = or i64 %16, 6
%42 = getelementptr inbounds double, double* %14, i64 %41
%43 = load double, double* %42
%44 = fadd double %40, %43
%45 = or i64 %16, 7
%46 = getelementptr inbounds double, double* %14, i64 %45
%47 = load double, double* %46
%48 = fadd double %44, %47
%49 = add nuw nsw i64 %16, 8
%50 = icmp eq i64 %49, 1000
br i1 %50, label %51, label %15
51: ; preds = %15
%52 = load double*, double** %4
%53 = call i32 @glb_free(double* %52)
%54 = load double*, double** %5
%55 = call i32 @glb_free(double* %54)
%56 = fdiv double %48, 1.000000e+03
%57 = call i64 @glb_malloc(i32 8)
%58 = inttoptr i64 %57 to double*
store double* %58, double** %4
%59 = call i64 @glb_malloc(i32 8)
%60 = inttoptr i64 %59 to double*
%61 = load double*, double** %4
store double %0, double* %61
store double %56, double* %60
store double* %60, double** %2
store double* %61, double** %1
br label %62
62: ; preds = %10, %3, %51
%63 = phi i32 [ 0, %51 ], [ -1, %3 ], [ -1, %10 ]
ret i32 %63
}