#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__ ceil(double);
double log1p(double);
double randexp();
long randsubseq(long N, long max_samples, double p, long *samples) {
  double L = -1.0 / log1p(-p);
  long i = -1;
  long j = 0;
  while (1) {
    double s = randexp() * L;
    if (s + i >= N - 1) {
      return j;
    }
    i += (long)ceil(s);
    samples[j] = i;
    j++;
    if (j >= max_samples) {
      return j;
    }
  }
}


x86 assembly (clang, -O0):
<randsubseq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    %rdx,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1050 <log1p@plt>
movaps %xmm0,%xmm1
movsd  0xe8b(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movq   $0xffffffffffffffff,-0x38(%rbp)
ff
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   1040 <randexp@plt>
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
cvtsi2sdq -0x38(%rbp),%xmm1
addsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
cvtsi2sd %rax,%xmm1
ucomisd %xmm1,%xmm0
jb     11d2 <randsubseq+0xa2>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <randsubseq+0xf0>
movsd  -0x48(%rbp),%xmm0
call   1030 <ceil@plt>
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jl     121b <randsubseq+0xeb>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <randsubseq+0xf0>
jmp    118e <randsubseq+0x5e>
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randsubseq>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xorpd  0xeb4(%rip),%xmm0
call   1050 <log1p@plt>
movsd  0xeb7(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
add    $0xffffffffffffffff,%rbx
xorps  %xmm0,%xmm0
cvtsi2sd %rbx,%xmm0
movsd  %xmm0,0x8(%rsp)
cmp    $0x2,%r15
mov    $0x1,%r12d
cmovge %r15,%r12
mov    $0xffffffffffffffff,%rbx
xor    %r15d,%r15d
nopl   (%rax)
xor    %eax,%eax
call   1040 <randexp@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %rbx,%xmm1
mulsd  0x10(%rsp),%xmm0
addsd  %xmm0,%xmm1
ucomisd 0x8(%rsp),%xmm1
jae    11c9 <randsubseq+0x99>
call   1030 <ceil@plt>
add    %rax,%rbx
mov    %rbx,(%r14,%r15,8)
add    $0x1,%r15
cmp    %r15,%r12
jne    1190 <randsubseq+0x60>
mov    %r12,%r15
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @randsubseq(i64 %0, i64 %1, double %2, i64* %3) {
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca i64*
%10 = alloca double
%11 = alloca i64
%12 = alloca i64
%13 = alloca double
store i64 %0, i64* %6
store i64 %1, i64* %7
store double %2, double* %8
store i64* %3, i64** %9
%14 = load double, double* %8
%15 = fneg double %14
%16 = call double @log1p(double %15)
%17 = fdiv double -1.000000e+00, %16
store double %17, double* %10
store i64 -1, i64* %11
store i64 0, i64* %12
br label %18
18: ; preds = %4, %48
%19 = call double (...) @randexp()
%20 = load double, double* %10
%21 = fmul double %19, %20
store double %21, double* %13
%22 = load double, double* %13
%23 = load i64, i64* %11
%24 = sitofp i64 %23 to double
%25 = fadd double %22, %24
%26 = load i64, i64* %6
%27 = sub nsw i64 %26, 1
%28 = sitofp i64 %27 to double
%29 = fcmp oge double %25, %28
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = load i64, i64* %12
store i64 %31, i64* %5
br label %49
32: ; preds = %18
%33 = load double, double* %13
%34 = call i64 @ceil(double %33)
%35 = load i64, i64* %11
%36 = add nsw i64 %35, %34
store i64 %36, i64* %11
%37 = load i64, i64* %11
%38 = load i64*, i64** %9
%39 = load i64, i64* %12
%40 = getelementptr inbounds i64, i64* %38, i64 %39
store i64 %37, i64* %40
%41 = load i64, i64* %12
%42 = add nsw i64 %41, 1
store i64 %42, i64* %12
%43 = load i64, i64* %12
%44 = load i64, i64* %7
%45 = icmp sge i64 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %32
%47 = load i64, i64* %12
store i64 %47, i64* %5
br label %49
48: ; preds = %32
br label %18
49: ; preds = %46, %30
%50 = load i64, i64* %5
ret i64 %50
}

LLVM IR (-O2):
define i64 @randsubseq(i64 %0, i64 %1, double %2, i64* nocapture writeonly %3) {
%5 = fneg double %2
%6 = tail call double @log1p(double %5)
%7 = fdiv double -1.000000e+00, %6
%8 = add nsw i64 %0, -1
%9 = sitofp i64 %8 to double
%10 = call i64 @llvm.smax.i64(i64 %1, i64 1)
br label %11
11: ; preds = %19, %4
%12 = phi i64 [ -1, %4 ], [ %21, %19 ]
%13 = phi i64 [ 0, %4 ], [ %23, %19 ]
%14 = tail call double (...) @randexp()
%15 = fmul double %7, %14
%16 = sitofp i64 %12 to double
%17 = fadd double %15, %16
%18 = fcmp ult double %17, %9
br i1 %18, label %19, label %25
19: ; preds = %11
%20 = tail call i64 @ceil(double %15)
%21 = add nsw i64 %20, %12
%22 = getelementptr inbounds i64, i64* %3, i64 %13
store i64 %21, i64* %22
%23 = add nuw nsw i64 %13, 1
%24 = icmp eq i64 %23, %10
br i1 %24, label %25, label %11
25: ; preds = %11, %19
%26 = phi i64 [ %10, %19 ], [ %13, %11 ]
ret i64 %26
}