#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 */
scalar_t__ RAND_MAX;
int rand();
long longrands(long max_rand) {
  long newrand = 0x00l;
  long a = 0x00l;
  long b = 0x00l;
  long shiftvar = 0x00l;
  if (max_rand > (long)(RAND_MAX)) {
    a = (long)(rand());
    b = (long)(rand());
    shiftvar = max_rand - (long)(RAND_MAX);
    shiftvar = shiftvar % (long)(8);
    newrand = a | (b << shiftvar);
  } else {
    a = (long)(rand() % (int)(max_rand));
    newrand = a;
  }
  return newrand;
}


x86 assembly (clang, -O0):
<longrands>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2ea1(%rip),%rcx
cmp    (%rcx),%rax
jle    11ac <longrands+0x9c>
mov    $0x0,%al
call   1030 <rand@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e73(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    $0x8,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
shl    %cl,%rdx
mov    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11c9 <longrands+0xb9>
mov    $0x0,%al
call   1030 <rand@plt>
mov    -0x8(%rbp),%rcx
cltd
idiv   %ecx
movslq %edx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<longrands>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2ec9(%rip),%r14
mov    (%r14),%r15
xor    %eax,%eax
call   1030 <rand@plt>
cmp    %rbx,%r15
jge    114a <longrands+0x3a>
movslq %eax,%r15
xor    %eax,%eax
call   1030 <rand@plt>
cltq
sub    (%r14),%ebx
and    $0x7,%bl
mov    %ebx,%ecx
shl    %cl,%rax
or     %r15,%rax
jmp    1150 <longrands+0x40>
cltd
idiv   %ebx
movslq %edx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
define i64 @longrands(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i64 %0, i64* %2
store i64 0, i64* %3
store i64 0, i64* %4
store i64 0, i64* %5
store i64 0, i64* %6
%7 = load i64, i64* %2
%8 = load i64, i64* @RAND_MAX
%9 = icmp sgt i64 %7, %8
br i1 %9, label %10, label %25
10: ; preds = %1
%11 = call i32 (...) @rand()
%12 = sext i32 %11 to i64
store i64 %12, i64* %4
%13 = call i32 (...) @rand()
%14 = sext i32 %13 to i64
store i64 %14, i64* %5
%15 = load i64, i64* %2
%16 = load i64, i64* @RAND_MAX
%17 = sub nsw i64 %15, %16
store i64 %17, i64* %6
%18 = load i64, i64* %6
%19 = srem i64 %18, 8
store i64 %19, i64* %6
%20 = load i64, i64* %4
%21 = load i64, i64* %5
%22 = load i64, i64* %6
%23 = shl i64 %21, %22
%24 = or i64 %20, %23
store i64 %24, i64* %3
br label %32
25: ; preds = %1
%26 = call i32 (...) @rand()
%27 = load i64, i64* %2
%28 = trunc i64 %27 to i32
%29 = srem i32 %26, %28
%30 = sext i32 %29 to i64
store i64 %30, i64* %4
%31 = load i64, i64* %4
store i64 %31, i64* %3
br label %32
32: ; preds = %25, %10
%33 = load i64, i64* %3
ret i64 %33
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
define i64 @longrands(i64 %0) {
%2 = load i64, i64* @RAND_MAX
%3 = icmp slt i64 %2, %0
%4 = tail call i32 (...) @rand()
br i1 %3, label %5, label %14
5: ; preds = %1
%6 = sext i32 %4 to i64
%7 = tail call i32 (...) @rand()
%8 = sext i32 %7 to i64
%9 = load i64, i64* @RAND_MAX
%10 = sub nsw i64 %0, %9
%11 = and i64 %10, 7
%12 = shl nsw i64 %8, %11
%13 = or i64 %12, %6
br label %18
14: ; preds = %1
%15 = trunc i64 %0 to i32
%16 = srem i32 %4, %15
%17 = sext i32 %16 to i64
br label %18
18: ; preds = %14, %5
%19 = phi i64 [ %13, %5 ], [ %17, %14 ]
ret i64 %19
}