#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 */
int RAND_MAX;
unsigned long rand();
int Rand(int MaxNum) {
  unsigned long RandNum;
  int ReturnValue = 0;
  if ((MaxNum > 0) && (MaxNum <= 1000)) {
    RandNum = rand();
    RandNum = (RandNum / 1024);
    ReturnValue = (int)((RandNum * MaxNum) / (RAND_MAX / 1024));
  } else {
    ReturnValue = -1;
  }
  return ReturnValue;
}


x86 assembly (clang, -O0):
<Rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    1187 <Rand+0x77>
cmpl   $0x3e8,-0x4(%rbp)
jg     1187 <Rand+0x77>
mov    $0x0,%al
call   1030 <rand@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
imul   %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x14(%rbp)
jmp    118e <Rand+0x7e>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Rand>:
push   %rbx
mov    %edi,%ebx
lea    -0x1(%rbx),%ecx
mov    $0xffffffff,%eax
cmp    $0x3e7,%ecx
ja     1162 <Rand+0x52>
xor    %eax,%eax
call   1030 <rand@plt>
mov    %rax,%rcx
shr    $0xa,%rcx
mov    %ebx,%eax
imul   %rcx,%rax
mov    0x2eaa(%rip),%rcx
mov    (%rcx),%edx
lea    0x3ff(%rdx),%ecx
test   %edx,%edx
cmovns %edx,%ecx
sar    $0xa,%ecx
movslq %ecx,%rsi
mov    %rax,%rdx
or     %rsi,%rdx
shr    $0x20,%rdx
je     1164 <Rand+0x54>
xor    %edx,%edx
div    %rsi
pop    %rbx
ret
xor    %edx,%edx
div    %ecx
pop    %rbx
ret

LLVM IR (-O0):
@RAND_MAX = global i32 0
define i32 @Rand(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%5 = load i32, i32* %2
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %23
7: ; preds = %1
%8 = load i32, i32* %2
%9 = icmp sle i32 %8, 1000
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = call i64 (...) @rand()
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = udiv i64 %12, 1024
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = load i32, i32* %2
%16 = sext i32 %15 to i64
%17 = mul i64 %14, %16
%18 = load i32, i32* @RAND_MAX
%19 = sdiv i32 %18, 1024
%20 = sext i32 %19 to i64
%21 = udiv i64 %17, %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %4
br label %24
23: ; preds = %7, %1
store i32 -1, i32* %4
br label %24
24: ; preds = %23, %10
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
define i32 @Rand(i32 %0) {
%2 = add i32 %0, -1
%3 = icmp ult i32 %2, 1000
br i1 %3, label %4, label %14
4: ; preds = %1
%5 = tail call i64 (...) @rand()
%6 = lshr i64 %5, 10
%7 = zext i32 %0 to i64
%8 = mul nuw i64 %6, %7
%9 = load i32, i32* @RAND_MAX
%10 = sdiv i32 %9, 1024
%11 = sext i32 %10 to i64
%12 = udiv i64 %8, %11
%13 = trunc i64 %12 to i32
br label %14
14: ; preds = %1, %4
%15 = phi i32 [ %13, %4 ], [ -1, %1 ]
ret i32 %15
}