#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 RandomNumber(unsigned int, unsigned int);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ stderr;
int NURand(unsigned A, unsigned x, unsigned y) {
  static int first = 1;
  unsigned C, C_255, C_1023, C_8191;
  if (first) {
    C_255 = RandomNumber(0, 255);
    C_1023 = RandomNumber(0, 1023);
    C_8191 = RandomNumber(0, 8191);
    first = 0;
  }
  switch (A) {
  case 255:
    C = C_255;
    break;
  case 1023:
    C = C_1023;
    break;
  case 8191:
    C = C_8191;
    break;
  default:
    fprintf(stderr, "NURand: unexpected value (%d) of A used\n", A);
    abort();
  }
  return (int)(((RandomNumber(0, A) | RandomNumber(x, y)) + C) % (y - x + 1)) +
         x;
}


x86 assembly (clang, -O0):
<NURand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
cmpl   $0x0,0x2ef0(%rip)
je     1185 <NURand+0x55>
xor    %edi,%edi
mov    $0xff,%esi
call   1050 <RandomNumber@plt>
mov    %eax,-0x14(%rbp)
xor    %edi,%edi
mov    $0x3ff,%esi
call   1050 <RandomNumber@plt>
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
mov    $0x1fff,%esi
call   1050 <RandomNumber@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,0x2eb3(%rip)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xff,%eax
je     11c1 <NURand+0x91>
jmp    119b <NURand+0x6b>
mov    -0x20(%rbp),%eax
sub    $0x3ff,%eax
je     11cc <NURand+0x9c>
jmp    11ae <NURand+0x7e>
mov    -0x20(%rbp),%eax
sub    $0x1fff,%eax
je     11d7 <NURand+0xa7>
jmp    11e2 <NURand+0xb2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1201 <NURand+0xd1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1201 <NURand+0xd1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1201 <NURand+0xd1>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe0b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%esi
xor    %edi,%edi
call   1050 <RandomNumber@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <RandomNumber@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
or     %ecx,%eax
add    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    -0x8(%rbp),%ecx
add    $0x1,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,%eax
add    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NURand>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r13d
mov    %edi,%r14d
cmpb   $0x0,0x2ef2(%rip)
jne    117d <NURand+0x4d>
xor    %edi,%edi
mov    $0xff,%esi
call   1050 <RandomNumber@plt>
mov    %eax,%r15d
xor    %edi,%edi
mov    $0x3ff,%esi
call   1050 <RandomNumber@plt>
mov    %eax,%r12d
xor    %edi,%edi
mov    $0x1fff,%esi
call   1050 <RandomNumber@plt>
movb   $0x1,0x2ebf(%rip)
cmp    $0xff,%r14d
je     119e <NURand+0x6e>
mov    %eax,%r15d
cmp    $0x1fff,%r14d
je     119e <NURand+0x6e>
mov    %r12d,%r15d
cmp    $0x3ff,%r14d
jne    11d8 <NURand+0xa8>
xor    %edi,%edi
mov    %r14d,%esi
call   1050 <RandomNumber@plt>
mov    %eax,%ebx
mov    %r13d,%edi
mov    %ebp,%esi
call   1050 <RandomNumber@plt>
or     %ebx,%eax
add    %r15d,%eax
sub    %r13d,%ebp
add    $0x1,%ebp
xor    %edx,%edx
div    %ebp
add    %edx,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
mov    %r14d,%edx
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@NURand.first = internal global i32 1
@stderr = global i32 0
@.str = [41 x i8] c"NURand: unexpected value (%d) of A used\0A\00"
define i32 @NURand(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* @NURand.first
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = call i32 @RandomNumber(i32 0, i32 255)
store i32 %14, i32* %8
%15 = call i32 @RandomNumber(i32 0, i32 1023)
store i32 %15, i32* %9
%16 = call i32 @RandomNumber(i32 0, i32 8191)
store i32 %16, i32* %10
store i32 0, i32* @NURand.first
br label %17
17: ; preds = %13, %3
%18 = load i32, i32* %4
switch i32 %18, label %25 [
i32 255, label %19
i32 1023, label %21
i32 8191, label %23
]
19: ; preds = %17
%20 = load i32, i32* %8
store i32 %20, i32* %7
br label %30
21: ; preds = %17
%22 = load i32, i32* %9
store i32 %22, i32* %7
br label %30
23: ; preds = %17
%24 = load i32, i32* %10
store i32 %24, i32* %7
br label %30
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = load i32, i32* %4
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = call i32 (...) @abort()
unreachable
30: ; preds = %23, %21, %19
%31 = load i32, i32* %4
%32 = call i32 @RandomNumber(i32 0, i32 %31)
%33 = load i32, i32* %5
%34 = load i32, i32* %6
%35 = call i32 @RandomNumber(i32 %33, i32 %34)
%36 = or i32 %32, %35
%37 = load i32, i32* %7
%38 = add i32 %36, %37
%39 = load i32, i32* %6
%40 = load i32, i32* %5
%41 = sub i32 %39, %40
%42 = add i32 %41, 1
%43 = urem i32 %38, %42
%44 = load i32, i32* %5
%45 = add i32 %43, %44
ret i32 %45
}

LLVM IR (-O2):
@NURand.first = internal unnamed_addr global i1 false
@stderr = global i32 0
@.str = [41 x i8] c"NURand: unexpected value (%d) of A used\0A\00"
define i32 @NURand(i32 %0, i32 %1, i32 %2) {
%4 = load i1, i1* @NURand.first
br i1 %4, label %9, label %5
5: ; preds = %3
%6 = tail call i32 @RandomNumber(i32 0, i32 255)
%7 = tail call i32 @RandomNumber(i32 0, i32 1023)
%8 = tail call i32 @RandomNumber(i32 0, i32 8191)
store i1 true, i1* @NURand.first
br label %9
9: ; preds = %5, %3
%10 = phi i32 [ %6, %5 ], [ undef, %3 ]
%11 = phi i32 [ %7, %5 ], [ undef, %3 ]
%12 = phi i32 [ %8, %5 ], [ undef, %3 ]
switch i32 %0, label %15 [
i32 255, label %19
i32 1023, label %13
i32 8191, label %14
]
13: ; preds = %9
br label %19
14: ; preds = %9
br label %19
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %0)
%18 = tail call i32 (...) @abort()
unreachable
19: ; preds = %9, %14, %13
%20 = phi i32 [ %12, %14 ], [ %11, %13 ], [ %10, %9 ]
%21 = tail call i32 @RandomNumber(i32 0, i32 %0)
%22 = tail call i32 @RandomNumber(i32 %1, i32 %2)
%23 = or i32 %22, %21
%24 = add i32 %23, %20
%25 = sub i32 1, %1
%26 = add i32 %25, %2
%27 = urem i32 %24, %26
%28 = add i32 %27, %1
ret i32 %28
}