#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*/ bal1(double *, int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ copyarr(double *, double *, int);
int /*<<< orphan*/ fatalx(char *, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ ivzero(int *, int);
int ranbinom(int, double);
void ranmultinom(int *samp, int n, double *p, int len) {
  int x;
  double *pp;
  if (len == 0)
    return;
  ivzero(samp, len);
  if (n <= 0)
    return;
  if (len == 1) {
    samp[0] = n;
    return;
  }
  if ((pp = (double *)calloc((len), sizeof(double))) == NULL)
    fatalx("Unable to allocate %d unit(s) for item \n", len);
  copyarr(p, pp, len);
  bal1(pp, len);
  samp[0] = x = ranbinom(n, pp[0]);
  ranmultinom(samp + 1, n - x, p + 1, len - 1);
  free(pp);
}


x86 assembly (clang, -O0):
<ranmultinom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a5 <ranmultinom+0x25>
jmp    1263 <ranmultinom+0xe3>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <ivzero@plt>
cmpl   $0x0,-0xc(%rbp)
jg     11c0 <ranmultinom+0x40>
jmp    1263 <ranmultinom+0xe3>
cmpl   $0x1,-0x1c(%rbp)
jne    11d8 <ranmultinom+0x58>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    1263 <ranmultinom+0xe3>
mov    -0x1c(%rbp),%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1202 <ranmultinom+0x82>
mov    -0x1c(%rbp),%esi
lea    0xe03(%rip),%rdi
call   1050 <fatalx@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <copyarr@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <bal1@plt>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rax
movsd  (%rax),%xmm0
call   1090 <ranbinom@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
mov    -0xc(%rbp),%esi
sub    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
add    $0x8,%rdx
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
call   10a0 <ranmultinom@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ranmultinom>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %ecx,%ecx
je     11ac <ranmultinom+0x2c>
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbp
mov    %ecx,%esi
call   1040 <ivzero@plt>
test   %r15d,%r15d
jle    11ac <ranmultinom+0x2c>
cmp    $0x1,%ebx
jne    11b5 <ranmultinom+0x35>
mov    %r15d,0x0(%rbp)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11d7 <ranmultinom+0x57>
lea    0xe30(%rip),%rdi
mov    %ebx,%esi
call   1050 <fatalx@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1080 <copyarr@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1060 <bal1@plt>
movsd  (%r12),%xmm0
mov    %r15d,%edi
call   1090 <ranbinom@plt>
mov    %eax,0x0(%rbp)
add    $0x4,%rbp
sub    %eax,%r15d
add    $0x8,%r14
add    $0xffffffff,%ebx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
call   10a0 <ranmultinom@plt>
mov    %r12,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"Unable to allocate %d unit(s) for item \0A\00"
define void @ranmultinom(i32* %0, i32 %1, double* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca double*
store i32* %0, i32** %5
store i32 %1, i32* %6
store double* %2, double** %7
store i32 %3, i32* %8
%11 = load i32, i32* %8
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %4
br label %62
14: ; preds = %4
%15 = load i32*, i32** %5
%16 = load i32, i32* %8
%17 = call i32 @ivzero(i32* %15, i32 %16)
%18 = load i32, i32* %6
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
br label %62
21: ; preds = %14
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* %6
%26 = load i32*, i32** %5
%27 = getelementptr inbounds i32, i32* %26, i64 0
store i32 %25, i32* %27
br label %62
28: ; preds = %21
%29 = load i32, i32* %8
%30 = call i64 @calloc(i32 %29, i32 8)
%31 = inttoptr i64 %30 to double*
store double* %31, double** %10
%32 = icmp eq double* %31, null
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* %8
%35 = call i32 @fatalx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %33, %28
%37 = load double*, double** %7
%38 = load double*, double** %10
%39 = load i32, i32* %8
%40 = call i32 @copyarr(double* %37, double* %38, i32 %39)
%41 = load double*, double** %10
%42 = load i32, i32* %8
%43 = call i32 @bal1(double* %41, i32 %42)
%44 = load i32, i32* %6
%45 = load double*, double** %10
%46 = getelementptr inbounds double, double* %45, i64 0
%47 = load double, double* %46
%48 = call i32 @ranbinom(i32 %44, double %47)
store i32 %48, i32* %9
%49 = load i32*, i32** %5
%50 = getelementptr inbounds i32, i32* %49, i64 0
store i32 %48, i32* %50
%51 = load i32*, i32** %5
%52 = getelementptr inbounds i32, i32* %51, i64 1
%53 = load i32, i32* %6
%54 = load i32, i32* %9
%55 = sub nsw i32 %53, %54
%56 = load double*, double** %7
%57 = getelementptr inbounds double, double* %56, i64 1
%58 = load i32, i32* %8
%59 = sub nsw i32 %58, 1
%60 = load double*, double** %10
%61 = call i32 @free(double* %60)
br label %62
62: ; preds = %36, %24, %20, %13
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"Unable to allocate %d unit(s) for item \0A\00"
define void @ranmultinom(i32* %0, i32 %1, double* %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %18, label %6
6: ; preds = %4
%7 = tail call i32 @ivzero(i32* %0, i32 %3)
%8 = icmp slt i32 %1, 1
br i1 %8, label %18, label %9
9: ; preds = %6
%10 = icmp eq i32 %3, 1
br i1 %10, label %11, label %12
11: ; preds = %9
store i32 %1, i32* %0
br label %18
12: ; preds = %9
%13 = tail call i64 @calloc(i32 %3, i32 8)
%14 = inttoptr i64 %13 to double*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @fatalx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %19
18: ; preds = %11, %4, %6, %19
ret void
19: ; preds = %16, %12
%20 = tail call i32 @copyarr(double* %2, double* %14, i32 %3)
%21 = tail call i32 @bal1(double* %14, i32 %3)
%22 = load double, double* %14
%23 = tail call i32 @ranbinom(i32 %1, double %22)
store i32 %23, i32* %0
%24 = getelementptr inbounds i32, i32* %0, i64 1
%25 = sub nsw i32 %1, %23
%26 = getelementptr inbounds double, double* %2, i64 1
%27 = add nsw i32 %3, -1
tail call void @ranmultinom(i32* %24, i32 %25, double* %26, i32 %27)
%28 = tail call i32 @free(double* %14)
br label %18
}