#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 */
long genrand_int31();
long int NJ_genrand_int31_top(long int top) {
  long int overflow;
  long int r;
  long int retval;
  if (top <= 0) {
    return (0);
  } else {
    overflow = (0x7fffffffUL / top) * top;
  }
  while (1) {
    r = genrand_int31();
    if (r < overflow) {
      break;
    }
  }
  retval = r % top;
  return (retval);
}


x86 assembly (clang, -O0):
<NJ_genrand_int31_top>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jg     1134 <NJ_genrand_int31_top+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1188 <NJ_genrand_int31_top+0x78>
mov    $0x7fffffff,%eax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
imul   -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    114f <NJ_genrand_int31_top+0x3f>
mov    $0x0,%al
call   1030 <genrand_int31@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    116d <NJ_genrand_int31_top+0x5d>
jmp    1172 <NJ_genrand_int31_top+0x62>
jmp    114f <NJ_genrand_int31_top+0x3f>
mov    -0x20(%rbp),%rax
cqto
idivq  -0x10(%rbp)
mov    %rdx,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NJ_genrand_int31_top>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
jle    1162 <NJ_genrand_int31_top+0x52>
mov    %rdi,%r14
mov    $0x7fffffff,%ebx
cmp    $0x7fffffff,%rdi
ja     1136 <NJ_genrand_int31_top+0x26>
mov    $0x7fffffff,%eax
xor    %edx,%edx
div    %r14d
mov    %edx,%ebx
xor    $0x7fffffff,%rbx
nopl   (%rax)
xor    %eax,%eax
call   1030 <genrand_int31@plt>
cmp    %rbx,%rax
jge    1140 <NJ_genrand_int31_top+0x30>
mov    %rax,%rcx
or     %r14,%rcx
shr    $0x20,%rcx
je     1166 <NJ_genrand_int31_top+0x56>
cqto
idiv   %r14
mov    %rdx,%rax
jmp    116d <NJ_genrand_int31_top+0x5d>
xor    %eax,%eax
jmp    116d <NJ_genrand_int31_top+0x5d>
xor    %edx,%edx
div    %r14d
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @NJ_genrand_int31_top(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i64 %0, i64* %3
%7 = load i64, i64* %3
%8 = icmp sle i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i64 0, i64* %2
br label %28
10: ; preds = %1
%11 = load i64, i64* %3
%12 = udiv i64 2147483647, %11
%13 = load i64, i64* %3
%14 = mul i64 %12, %13
store i64 %14, i64* %4
br label %15
15: ; preds = %10
br label %16
16: ; preds = %15, %22
%17 = call i64 (...) @genrand_int31()
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = load i64, i64* %4
%20 = icmp slt i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %16
br label %23
22: ; preds = %16
br label %16
23: ; preds = %21
%24 = load i64, i64* %5
%25 = load i64, i64* %3
%26 = srem i64 %24, %25
store i64 %26, i64* %6
%27 = load i64, i64* %6
store i64 %27, i64* %2
br label %28
28: ; preds = %23, %9
%29 = load i64, i64* %2
ret i64 %29
}

LLVM IR (-O2):
define i64 @NJ_genrand_int31_top(i64 %0) {
%2 = icmp slt i64 %0, 1
br i1 %2, label %11, label %3
3: ; preds = %1
%4 = urem i64 2147483647, %0
%5 = xor i64 %4, 2147483647
br label %6
6: ; preds = %6, %3
%7 = tail call i64 (...) @genrand_int31()
%8 = icmp slt i64 %7, %5
br i1 %8, label %9, label %6
9: ; preds = %6
%10 = srem i64 %7, %0
br label %11
11: ; preds = %1, %9
%12 = phi i64 [ %10, %9 ], [ 0, %1 ]
ret i64 %12
}