#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*/ abort();
double rand_uniform();
int rand_pickd(double *p, int n) {
  double t, r;
  int i;
  t = 0;
  for (i = 0; i < n; i++) {
    if (p[i] < 0)
      abort();
    t += p[i];
  }
  if (t <= 0)
    abort();
  r = t * rand_uniform();
  for (i = 0; i < n; i++) {
    r -= p[i];
    if (r < 0)
      return i;
  }
  for (i = 0; i < n; i++) {
    if (p[i] > 0)
      return i;
  }
  abort();
}


x86 assembly (clang, -O0):
<rand_pickd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118c <rand_pickd+0x6c>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
xorps  %xmm0,%xmm0
ucomisd (%rax,%rcx,8),%xmm0
jbe    1167 <rand_pickd+0x47>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    113e <rand_pickd+0x1e>
xorps  %xmm0,%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     11a1 <rand_pickd+0x81>
mov    $0x0,%al
call   1030 <abort@plt>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    $0x0,%al
call   1040 <rand_uniform@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    121d <rand_pickd+0xfd>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x28(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    120a <rand_pickd+0xea>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <rand_pickd+0x14f>
jmp    120f <rand_pickd+0xef>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ca <rand_pickd+0xaa>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1268 <rand_pickd+0x148>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1255 <rand_pickd+0x135>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <rand_pickd+0x14f>
jmp    125a <rand_pickd+0x13a>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1224 <rand_pickd+0x104>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_pickd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
jle    11d4 <rand_pickd+0xb4>
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %esi,%ebp
xorpd  %xmm0,%xmm0
xor    %eax,%eax
xorpd  %xmm2,%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  (%rbx,%rax,8),%xmm1
ucomisd %xmm1,%xmm0
ja     11d4 <rand_pickd+0xb4>
addsd  %xmm1,%xmm2
add    $0x1,%rax
cmp    %rax,%rbp
jne    1150 <rand_pickd+0x30>
xorpd  %xmm0,%xmm0
movsd  %xmm2,0x8(%rsp)
ucomisd %xmm2,%xmm0
jae    11d4 <rand_pickd+0xb4>
xor    %eax,%eax
call   1040 <rand_uniform@plt>
test   %r14d,%r14d
jle    11d4 <rand_pickd+0xb4>
movsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm1
xor    %eax,%eax
xorpd  %xmm0,%xmm0
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
subsd  (%rbx,%rax,8),%xmm1
ucomisd %xmm1,%xmm0
ja     11db <rand_pickd+0xbb>
add    $0x1,%rax
cmp    %rax,%rbp
jne    11a0 <rand_pickd+0x80>
test   %r14d,%r14d
jle    11d4 <rand_pickd+0xb4>
xor    %eax,%eax
xorpd  %xmm0,%xmm0
nop
movsd  (%rbx,%rax,8),%xmm1
ucomisd %xmm0,%xmm1
ja     11db <rand_pickd+0xbb>
add    $0x1,%rax
cmp    %rax,%rbp
jne    11c0 <rand_pickd+0xa0>
xor    %eax,%eax
call   1030 <abort@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rand_pickd(double* %0, i32 %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32
%6 = alloca double
%7 = alloca double
%8 = alloca i32
store double* %0, double** %4
store i32 %1, i32* %5
store double 0.000000e+00, double* %6
store i32 0, i32* %8
br label %9
9: ; preds = %30, %2
%10 = load i32, i32* %8
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %33
13: ; preds = %9
%14 = load double*, double** %4
%15 = load i32, i32* %8
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds double, double* %14, i64 %16
%18 = load double, double* %17
%19 = fcmp olt double %18, 0.000000e+00
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 (...) @abort()
unreachable
22: ; preds = %13
%23 = load double*, double** %4
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds double, double* %23, i64 %25
%27 = load double, double* %26
%28 = load double, double* %6
%29 = fadd double %28, %27
store double %29, double* %6
br label %30
30: ; preds = %22
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 1
store i32 %32, i32* %8
br label %9
33: ; preds = %9
%34 = load double, double* %6
%35 = fcmp ole double %34, 0.000000e+00
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (...) @abort()
unreachable
38: ; preds = %33
%39 = load double, double* %6
%40 = call double (...) @rand_uniform()
%41 = fmul double %39, %40
store double %41, double* %7
store i32 0, i32* %8
br label %42
42: ; preds = %59, %38
%43 = load i32, i32* %8
%44 = load i32, i32* %5
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %62
46: ; preds = %42
%47 = load double*, double** %4
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds double, double* %47, i64 %49
%51 = load double, double* %50
%52 = load double, double* %7
%53 = fsub double %52, %51
store double %53, double* %7
%54 = load double, double* %7
%55 = fcmp olt double %54, 0.000000e+00
br i1 %55, label %56, label %58
56: ; preds = %46
%57 = load i32, i32* %8
store i32 %57, i32* %3
br label %82
58: ; preds = %46
br label %59
59: ; preds = %58
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
br label %42
62: ; preds = %42
store i32 0, i32* %8
br label %63
63: ; preds = %77, %62
%64 = load i32, i32* %8
%65 = load i32, i32* %5
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %80
67: ; preds = %63
%68 = load double*, double** %4
%69 = load i32, i32* %8
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds double, double* %68, i64 %70
%72 = load double, double* %71
%73 = fcmp ogt double %72, 0.000000e+00
br i1 %73, label %74, label %76
74: ; preds = %67
%75 = load i32, i32* %8
store i32 %75, i32* %3
br label %82
76: ; preds = %67
br label %77
77: ; preds = %76
%78 = load i32, i32* %8
%79 = add nsw i32 %78, 1
store i32 %79, i32* %8
br label %63
80: ; preds = %63
%81 = call i32 (...) @abort()
unreachable
82: ; preds = %74, %56
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
define i32 @rand_pickd(double* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %20
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %14
%7 = phi i64 [ 0, %4 ], [ %16, %14 ]
%8 = phi double [ 0.000000e+00, %4 ], [ %15, %14 ]
%9 = getelementptr inbounds double, double* %0, i64 %7
%10 = load double, double* %9
%11 = fcmp olt double %10, 0.000000e+00
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 (...) @abort()
unreachable
14: ; preds = %6
%15 = fadd double %8, %10
%16 = add nuw nsw i64 %7, 1
%17 = icmp eq i64 %16, %5
br i1 %17, label %18, label %6
18: ; preds = %14
%19 = fcmp ugt double %15, 0.000000e+00
br i1 %19, label %22, label %20
20: ; preds = %2, %18
%21 = tail call i32 (...) @abort()
unreachable
22: ; preds = %18
%23 = tail call double (...) @rand_uniform()
br i1 %3, label %24, label %48
24: ; preds = %22
%25 = fmul double %15, %23
%26 = zext i32 %1 to i64
br label %30
27: ; preds = %37
br i1 %3, label %28, label %48
28: ; preds = %27
%29 = zext i32 %1 to i64
br label %40
30: ; preds = %24, %37
%31 = phi i64 [ 0, %24 ], [ %38, %37 ]
%32 = phi double [ %25, %24 ], [ %35, %37 ]
%33 = getelementptr inbounds double, double* %0, i64 %31
%34 = load double, double* %33
%35 = fsub double %32, %34
%36 = fcmp olt double %35, 0.000000e+00
br i1 %36, label %50, label %37
37: ; preds = %30
%38 = add nuw nsw i64 %31, 1
%39 = icmp eq i64 %38, %26
br i1 %39, label %27, label %30
40: ; preds = %28, %45
%41 = phi i64 [ 0, %28 ], [ %46, %45 ]
%42 = getelementptr inbounds double, double* %0, i64 %41
%43 = load double, double* %42
%44 = fcmp ogt double %43, 0.000000e+00
br i1 %44, label %50, label %45
45: ; preds = %40
%46 = add nuw nsw i64 %41, 1
%47 = icmp eq i64 %46, %29
br i1 %47, label %48, label %40
48: ; preds = %45, %22, %27
%49 = tail call i32 (...) @abort()
unreachable
50: ; preds = %30, %40
%51 = phi i64 [ %41, %40 ], [ %31, %30 ]
%52 = trunc i64 %51 to i32
ret i32 %52
}