#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 */
struct ottery_state {
  int dummy;
};

/* Variables and functions */
unsigned int UINT_MAX;
unsigned int ottery_st_rand_unsigned_nolock(struct ottery_state *);
unsigned ottery_st_rand_range_nolock(struct ottery_state *st, unsigned upper) {
  unsigned lim = upper + 1;
  unsigned divisor = lim ? (UINT_MAX / lim) : 1;
  unsigned n;
  do {
    n = (ottery_st_rand_unsigned_nolock(st) / divisor);
  } while (n > upper);
  return n;
}


x86 assembly (clang, -O0):
<ottery_st_rand_range_nolock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1148 <ottery_st_rand_range_nolock+0x38>
mov    0x2eaf(%rip),%rax
mov    (%rax),%eax
xor    %edx,%edx
divl   -0x10(%rbp)
mov    %eax,-0x1c(%rbp)
jmp    1155 <ottery_st_rand_range_nolock+0x45>
mov    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1155 <ottery_st_rand_range_nolock+0x45>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <ottery_st_rand_unsigned_nolock@plt>
xor    %edx,%edx
divl   -0x14(%rbp)
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
ja     115b <ottery_st_rand_range_nolock+0x4b>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ottery_st_rand_range_nolock>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%ecx
mov    $0x1,%ebp
inc    %ecx
je     1140 <ottery_st_rand_range_nolock+0x30>
mov    0x2ebd(%rip),%rax
mov    (%rax),%eax
xor    %edx,%edx
div    %ecx
mov    %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1030 <ottery_st_rand_unsigned_nolock@plt>
xor    %edx,%edx
div    %ebp
cmp    %ebx,%eax
ja     1140 <ottery_st_rand_range_nolock+0x30>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ottery_state = type { i32 }
@UINT_MAX = global i32 0
define i32 @ottery_st_rand_range_nolock(%struct.ottery_state* %0, i32 %1) {
%3 = alloca %struct.ottery_state*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store %struct.ottery_state* %0, %struct.ottery_state** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = add i32 %8, 1
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @UINT_MAX
%14 = load i32, i32* %5
%15 = udiv i32 %13, %14
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %12
%18 = phi i32 [ %15, %12 ], [ 1, %16 ]
store i32 %18, i32* %6
br label %19
19: ; preds = %24, %17
%20 = load %struct.ottery_state*, %struct.ottery_state** %3
%21 = call i32 @ottery_st_rand_unsigned_nolock(%struct.ottery_state* %20)
%22 = load i32, i32* %6
%23 = udiv i32 %21, %22
store i32 %23, i32* %7
br label %24
24: ; preds = %19
%25 = load i32, i32* %7
%26 = load i32, i32* %4
%27 = icmp ugt i32 %25, %26
br i1 %27, label %19, label %28
28: ; preds = %24
%29 = load i32, i32* %7
ret i32 %29
}

LLVM IR (-O2):
%struct.ottery_state = type { i32 }
@UINT_MAX = global i32 0
define i32 @ottery_st_rand_range_nolock(%struct.ottery_state* %0, i32 %1) {
%3 = add i32 %1, 1
%4 = icmp eq i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @UINT_MAX
%7 = udiv i32 %6, %3
br label %8
8: ; preds = %2, %5
%9 = phi i32 [ %7, %5 ], [ 1, %2 ]
br label %10
10: ; preds = %10, %8
%11 = tail call i32 @ottery_st_rand_unsigned_nolock(%struct.ottery_state* %0)
%12 = udiv i32 %11, %9
%13 = icmp ugt i32 %12, %1
br i1 %13, label %10, label %14
14: ; preds = %10
ret i32 %12
}