#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 */
unsigned int RAND_MAX;
unsigned int rand();
unsigned rand_mod(unsigned mod) {
  unsigned rnd, top = RAND_MAX;
  top -= top % mod;
  while (top <= (rnd = rand()))
    continue;
  rnd = 1.0 * mod * rnd / (1.0 + top);
  return rnd;
}


x86 assembly (clang, -O0):
<rand_mod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ec6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
xor    %edx,%edx
divl   -0x4(%rbp)
mov    -0xc(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
mov    %ecx,-0x8(%rbp)
cmp    %ecx,%eax
ja     1159 <rand_mod+0x49>
jmp    1137 <rand_mod+0x27>
mov    -0x4(%rbp),%eax
cvtsi2sd %rax,%xmm0
mov    -0x8(%rbp),%eax
cvtsi2sd %rax,%xmm1
mulsd  %xmm1,%xmm0
mov    -0xc(%rbp),%eax
cvtsi2sd %rax,%xmm1
movsd  0xe83(%rip),%xmm2
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_mod>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2ecc(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
xor    %edx,%edx
div    %edi
sub    %edx,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rand@plt>
cmp    %eax,%ebx
jbe    1130 <rand_mod+0x20>
mov    %ebp,%ecx
cvtsi2sd %rcx,%xmm0
mov    %eax,%eax
cvtsi2sd %rax,%xmm1
mov    %ebx,%eax
cvtsi2sd %rax,%xmm2
addsd  0xea8(%rip),%xmm2
mulsd  %xmm0,%xmm1
divsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
define i32 @rand_mod(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @RAND_MAX
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = load i32, i32* %2
%8 = urem i32 %6, %7
%9 = load i32, i32* %4
%10 = sub i32 %9, %8
store i32 %10, i32* %4
br label %11
11: ; preds = %15, %1
%12 = load i32, i32* %4
%13 = call i32 (...) @rand()
store i32 %13, i32* %3
%14 = icmp ule i32 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
br label %11
16: ; preds = %11
%17 = load i32, i32* %2
%18 = uitofp i32 %17 to double
%19 = fmul double 1.000000e+00, %18
%20 = load i32, i32* %3
%21 = uitofp i32 %20 to double
%22 = fmul double %19, %21
%23 = load i32, i32* %4
%24 = uitofp i32 %23 to double
%25 = fadd double 1.000000e+00, %24
%26 = fdiv double %22, %25
%27 = fptoui double %26 to i32
store i32 %27, i32* %3
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
define i32 @rand_mod(i32 %0) {
%2 = load i32, i32* @RAND_MAX
%3 = urem i32 %2, %0
%4 = sub i32 %2, %3
br label %5
5: ; preds = %5, %1
%6 = tail call i32 (...) @rand()
%7 = icmp ugt i32 %4, %6
br i1 %7, label %8, label %5
8: ; preds = %5
%9 = uitofp i32 %0 to double
%10 = uitofp i32 %6 to double
%11 = fmul double %9, %10
%12 = uitofp i32 %4 to double
%13 = fadd double %12, 1.000000e+00
%14 = fdiv double %11, %13
%15 = fptoui double %14 to i32
ret i32 %15
}