#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 drand2();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ printmat(double *, int, int);
int randis(double *a, int n) {
  int i;
  double t, y;
  static int nfirst = 0;
  ++nfirst;
  t = (drand2());
  for (i = 0; i < n; i++) {
    t -= a[i];
    if (t <= 0.0)
      return i;
  }
  if (nfirst == 1) {
    printf("pos t: %15.9f\n", t);
    for (i = 0; i < n; i++) {
      printf("zzrand %4d %9.3f\n", i, a[i]);
    }
  }
  printf("probable bug (randis)\n");
  printmat(a, 1, n);
  return n - 1;
}


x86 assembly (clang, -O0):
<randis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2ef7(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2eee(%rip)
mov    $0x0,%al
call   1040 <drand2@plt>
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b4 <randis+0x84>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x20(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     11a1 <randis+0x71>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <randis+0x110>
jmp    11a6 <randis+0x76>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1161 <randis+0x31>
cmpl   $0x1,0x2e81(%rip)
jne    1218 <randis+0xe8>
movsd  -0x20(%rbp),%xmm0
lea    0xe33(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1213 <randis+0xe3>
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe11(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11db <randis+0xab>
jmp    1218 <randis+0xe8>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0x1,%esi
call   1030 <printmat@plt>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randis>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r12
addl   $0x1,0x2eef(%rip)
xor    %eax,%eax
call   1050 <drand2@plt>
test   %r14d,%r14d
jle    11cd <randis+0x8d>
mov    %r14d,%ebx
xor    %eax,%eax
xorpd  %xmm1,%xmm1
nopw   0x0(%rax,%rax,1)
subsd  (%r12,%rax,8),%xmm0
ucomisd %xmm0,%xmm1
jae    1206 <randis+0xc6>
add    $0x1,%rax
cmp    %rax,%rbx
jne    1170 <randis+0x30>
cmpl   $0x1,0x2eb4(%rip)
jne    11e4 <randis+0xa4>
lea    0xe67(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
test   %r14d,%r14d
jle    11e4 <randis+0xa4>
xor    %ebp,%ebp
lea    0xe61(%rip),%r15
xchg   %ax,%ax
movsd  (%r12,%rbp,8),%xmm0
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0x1,%al
call   1060 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11b0 <randis+0x70>
jmp    11e4 <randis+0xa4>
cmpl   $0x1,0x2e70(%rip)
jne    11e4 <randis+0xa4>
lea    0xe23(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1040 <printmat@plt>
add    $0xffffffff,%r14d
jmp    1209 <randis+0xc9>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@randis.nfirst = internal global i32 0
@.str = [15 x i8] c"pos t: %15.9f\0A\00"
@.str.1 = [18 x i8] c"zzrand %4d %9.3f\0A\00"
@.str.2 = [23 x i8] c"probable bug (randis)\0A\00"
define i32 @randis(double* %0, i32 %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca double
store double* %0, double** %4
store i32 %1, i32* %5
%9 = load i32, i32* @randis.nfirst
%10 = add nsw i32 %9, 1
store i32 %10, i32* @randis.nfirst
%11 = call double (...) @drand2()
store double %11, double* %7
store i32 0, i32* %6
br label %12
12: ; preds = %29, %2
%13 = load i32, i32* %6
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %32
16: ; preds = %12
%17 = load double*, double** %4
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds double, double* %17, i64 %19
%21 = load double, double* %20
%22 = load double, double* %7
%23 = fsub double %22, %21
store double %23, double* %7
%24 = load double, double* %7
%25 = fcmp ole double %24, 0.000000e+00
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = load i32, i32* %6
store i32 %27, i32* %3
br label %61
28: ; preds = %16
br label %29
29: ; preds = %28
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %12
32: ; preds = %12
%33 = load i32, i32* @randis.nfirst
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %54
35: ; preds = %32
%36 = load double, double* %7
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), double %36)
store i32 0, i32* %6
br label %38
38: ; preds = %50, %35
%39 = load i32, i32* %6
%40 = load i32, i32* %5
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %53
42: ; preds = %38
%43 = load i32, i32* %6
%44 = load double*, double** %4
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %44, i64 %46
%48 = load double, double* %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %43, double %48)
br label %50
50: ; preds = %42
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %38
53: ; preds = %38
br label %54
54: ; preds = %53, %32
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%56 = load double*, double** %4
%57 = load i32, i32* %5
%58 = call i32 @printmat(double* %56, i32 1, i32 %57)
%59 = load i32, i32* %5
%60 = sub nsw i32 %59, 1
store i32 %60, i32* %3
br label %61
61: ; preds = %54, %26
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@randis.nfirst = internal unnamed_addr global i32 0
@.str = [15 x i8] c"pos t: %15.9f\0A\00"
@.str.1 = [18 x i8] c"zzrand %4d %9.3f\0A\00"
@str = [22 x i8] c"probable bug (randis)\00"
define i32 @randis(double* %0, i32 %1) {
%3 = load i32, i32* @randis.nfirst
%4 = add nsw i32 %3, 1
store i32 %4, i32* @randis.nfirst
%5 = tail call double (...) @drand2()
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %22
7: ; preds = %2
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %7, %16
%10 = phi i64 [ 0, %7 ], [ %17, %16 ]
%11 = phi double [ %5, %7 ], [ %14, %16 ]
%12 = getelementptr inbounds double, double* %0, i64 %10
%13 = load double, double* %12
%14 = fsub double %11, %13
%15 = fcmp ugt double %14, 0.000000e+00
br i1 %15, label %16, label %43
16: ; preds = %9
%17 = add nuw nsw i64 %10, 1
%18 = icmp eq i64 %17, %8
br i1 %18, label %19, label %9
19: ; preds = %16
%20 = load i32, i32* @randis.nfirst
%21 = icmp eq i32 %20, 1
br i1 %21, label %27, label %39
22: ; preds = %2
%23 = load i32, i32* @randis.nfirst
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %39
25: ; preds = %22
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), double %5)
br label %39
27: ; preds = %19
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), double %14)
br i1 %6, label %29, label %39
29: ; preds = %27
%30 = zext i32 %1 to i64
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %37, %31 ]
%33 = getelementptr inbounds double, double* %0, i64 %32
%34 = load double, double* %33
%35 = trunc i64 %32 to i32
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %35, double %34)
%37 = add nuw nsw i64 %32, 1
%38 = icmp eq i64 %37, %30
br i1 %38, label %39, label %31
39: ; preds = %31, %25, %22, %27, %19
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%41 = tail call i32 @printmat(double* %0, i32 1, i32 %1)
%42 = add nsw i32 %1, -1
br label %45
43: ; preds = %9
%44 = trunc i64 %10 to i32
br label %45
45: ; preds = %43, %39
%46 = phi i32 [ %42, %39 ], [ %44, %43 ]
ret i32 %46
}