#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 abs(long);
long next();
long int next_range(int range) {
  long int r = next();
  int i = 1;
  while (r > range - 1) {
    r /= i++;
    r -= 4;
    if (r < 0)
      r = abs(r);
  }
  return r;
}


x86 assembly (clang, -O0):
<next_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1040 <next@plt>
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jle    1197 <next_range+0x77>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
movslq %eax,%rcx
mov    -0x10(%rbp),%rax
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x4,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1192 <next_range+0x72>
mov    -0x10(%rbp),%rdi
call   1030 <abs@plt>
mov    %rax,-0x10(%rbp)
jmp    113d <next_range+0x1d>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_range>:
push   %rbx
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <next@plt>
add    $0xffffffff,%ebx
movslq %ebx,%rsi
cmp    %rsi,%rax
jle    1168 <next_range+0x58>
mov    $0x1,%edi
jmp    1154 <next_range+0x44>
nopl   0x0(%rax)
cqto
idiv   %rdi
mov    %rax,%rcx
add    $0x1,%rdi
lea    -0x4(%rcx),%rdx
lea    -0x4(%rcx),%rax
neg    %rax
cmp    $0x4,%rcx
cmovge %rdx,%rax
cmp    %rsi,%rax
jle    1168 <next_range+0x58>
mov    %rax,%rcx
or     %rdi,%rcx
shr    $0x20,%rcx
jne    1130 <next_range+0x20>
xor    %edx,%edx
div    %edi
mov    %eax,%ecx
jmp    1138 <next_range+0x28>
pop    %rbx
ret

LLVM IR (-O0):
define i64 @next_range(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i64 (...) @next()
store i64 %5, i64* %3
store i32 1, i32* %4
br label %6
6: ; preds = %25, %1
%7 = load i64, i64* %3
%8 = load i32, i32* %2
%9 = sub nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = icmp sgt i64 %7, %10
br i1 %11, label %12, label %26
12: ; preds = %6
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
store i32 %14, i32* %4
%15 = sext i32 %13 to i64
%16 = load i64, i64* %3
%17 = sdiv i64 %16, %15
store i64 %17, i64* %3
%18 = load i64, i64* %3
%19 = sub nsw i64 %18, 4
store i64 %19, i64* %3
%20 = load i64, i64* %3
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = load i64, i64* %3
%24 = call i64 @abs(i64 %23)
store i64 %24, i64* %3
br label %25
25: ; preds = %22, %12
br label %6
26: ; preds = %6
%27 = load i64, i64* %3
ret i64 %27
}

LLVM IR (-O2):
define i64 @next_range(i32 %0) {
%2 = tail call i64 (...) @next()
%3 = add nsw i32 %0, -1
%4 = sext i32 %3 to i64
%5 = icmp sgt i64 %2, %4
br i1 %5, label %6, label %16
6: ; preds = %1, %6
%7 = phi i64 [ %9, %6 ], [ 1, %1 ]
%8 = phi i64 [ %14, %6 ], [ %2, %1 ]
%9 = add nuw i64 %7, 1
%10 = sdiv i64 %8, %7
%11 = add nsw i64 %10, -4
%12 = icmp slt i64 %10, 4
%13 = sub i64 4, %10
%14 = select i1 %12, i64 %13, i64 %11
%15 = icmp sgt i64 %14, %4
br i1 %15, label %6, label %16
16: ; preds = %6, %1
%17 = phi i64 [ %2, %1 ], [ %14, %6 ]
ret i64 %17
}