#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__ RAND_MAX;
scalar_t__ rand();
long xp_random(int n) {
  double f = 0;
  int ret;
  if (n < 2)
    return (0);
  do {
    f = (double)rand() / (double)(RAND_MAX + 1);
    ret = (int)(n * f);
  } while (ret == n);
  return (ret);
}


x86 assembly (clang, -O0):
<xp_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x2,-0xc(%rbp)
jge    113a <xp_random+0x2a>
movq   $0x0,-0x8(%rbp)
jmp    118c <xp_random+0x7c>
jmp    113f <xp_random+0x2f>
mov    $0x0,%al
call   1030 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e96(%rip),%rax
mov    (%rax),%rax
add    $0x1,%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     113f <xp_random+0x2f>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xp_random>:
cmp    $0x2,%edi
jge    1118 <xp_random+0x8>
xor    %eax,%eax
ret
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
cvtsi2sd %edi,%xmm0
movsd  %xmm0,(%rsp)
mov    0x2eba(%rip),%r14
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mov    (%r14),%rax
add    $0x1,%rax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
mulsd  (%rsp),%xmm0
cvttsd2si %xmm0,%eax
cmp    %ebx,%eax
je     1130 <xp_random+0x20>
cltq
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
define i64 @xp_random(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca double
%5 = alloca i32
store i32 %0, i32* %3
store double 0.000000e+00, double* %4
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %9
8: ; preds = %1
store i64 0, i64* %2
br label %29
9: ; preds = %1
br label %10
10: ; preds = %22, %9
%11 = call i64 (...) @rand()
%12 = sitofp i64 %11 to double
%13 = load i64, i64* @RAND_MAX
%14 = add nsw i64 %13, 1
%15 = sitofp i64 %14 to double
%16 = fdiv double %12, %15
store double %16, double* %4
%17 = load i32, i32* %3
%18 = sitofp i32 %17 to double
%19 = load double, double* %4
%20 = fmul double %18, %19
%21 = fptosi double %20 to i32
store i32 %21, i32* %5
br label %22
22: ; preds = %10
%23 = load i32, i32* %5
%24 = load i32, i32* %3
%25 = icmp eq i32 %23, %24
br i1 %25, label %10, label %26
26: ; preds = %22
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
store i64 %28, i64* %2
br label %29
29: ; preds = %26, %8
%30 = load i64, i64* %2
ret i64 %30
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
define i64 @xp_random(i32 %0) {
%2 = icmp slt i32 %0, 2
br i1 %2, label %17, label %3
3: ; preds = %1
%4 = sitofp i32 %0 to double
br label %5
5: ; preds = %3, %5
%6 = tail call i64 (...) @rand()
%7 = sitofp i64 %6 to double
%8 = load i64, i64* @RAND_MAX
%9 = add nsw i64 %8, 1
%10 = sitofp i64 %9 to double
%11 = fdiv double %7, %10
%12 = fmul double %11, %4
%13 = fptosi double %12 to i32
%14 = icmp eq i32 %13, %0
br i1 %14, label %5, label %15
15: ; preds = %5
%16 = sext i32 %13 to i64
br label %17
17: ; preds = %1, %15
%18 = phi i64 [ %16, %15 ], [ 0, %1 ]
ret i64 %18
}