#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 */
typedef int uint8_t;
typedef int uint64_t;
typedef int uint32_t;

/* Variables and functions */
int UINT64_C(int);
int /*<<< orphan*/ assert(int);
int fls32(int);
void rgph_fastdiv_prepare(uint32_t div, uint32_t *m, uint8_t *s1, uint8_t *s2,
                          int branchless) {
  const int l = fls32(div - 1);
  const uint64_t nextpow2 = UINT64_C(1) << l;
  uint32_t rem, magic = ((nextpow2 - div) << 32) / div;
  *m = magic + 1;
  *s1 = (l > 1) ? 1 : l;
  *s2 = (l == 0) ? 0 : l - 1;
  if (branchless || div == 1)
    return;
  if (magic == 0) {
    *s1 -= 1;
    *s2 += 1;
  } else {
    magic = (magic + UINT64_C(0x100000000)) / 2;
    rem = (nextpow2 << 31) - magic * div;
    assert(rem > 0 && rem < div);
    if (rem > div - (nextpow2 / 2)) {
      *m = magic + 1;
      *s1 -= 1;
    }
  }
}


x86 assembly (clang, -O0):
<rgph_fastdiv_prepare>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x4(%rbp),%edi
sub    $0x1,%edi
call   1050 <fls32@plt>
mov    %eax,-0x28(%rbp)
mov    $0x1,%edi
call   1030 <UINT64_C@plt>
mov    -0x28(%rbp),%ecx
shl    %cl,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
sub    -0x4(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
cltd
idivl  -0x4(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x1,-0x28(%rbp)
jle    11a2 <rgph_fastdiv_prepare+0x72>
mov    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11a8 <rgph_fastdiv_prepare+0x78>
mov    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x28(%rbp)
jne    11c5 <rgph_fastdiv_prepare+0x95>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    11ce <rgph_fastdiv_prepare+0x9e>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x24(%rbp)
jne    11eb <rgph_fastdiv_prepare+0xbb>
cmpl   $0x1,-0x4(%rbp)
jne    11f0 <rgph_fastdiv_prepare+0xc0>
jmp    12ad <rgph_fastdiv_prepare+0x17d>
cmpl   $0x0,-0x34(%rbp)
jne    1215 <rgph_fastdiv_prepare+0xe5>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    12ad <rgph_fastdiv_prepare+0x17d>
mov    -0x34(%rbp),%eax
mov    %eax,-0x44(%rbp)
xor    %edi,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
shl    $0x1f,%eax
mov    -0x34(%rbp),%ecx
imul   -0x4(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x30(%rbp)
mov    %al,-0x3d(%rbp)
jle    1261 <rgph_fastdiv_prepare+0x131>
mov    -0x30(%rbp),%eax
cmp    -0x4(%rbp),%eax
setl   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x4(%rbp),%ecx
mov    -0x2c(%rbp),%eax
mov    $0x2,%esi
cltd
idiv   %esi
mov    %eax,%edx
mov    -0x48(%rbp),%eax
sub    %edx,%ecx
cmp    %ecx,%eax
jle    12a8 <rgph_fastdiv_prepare+0x178>
mov    -0x34(%rbp),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,(%rax)
jmp    12ad <rgph_fastdiv_prepare+0x17d>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rgph_fastdiv_prepare>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %rdx,%r12
mov    %edi,%r13d
lea    -0x1(%r13),%edi
call   1050 <fls32@plt>
mov    %eax,%ebp
mov    $0x1,%ebx
mov    $0x1,%edi
call   1030 <UINT64_C@plt>
test   %ebp,%ebp
cmovle %ebp,%ebx
mov    %ebx,(%r12)
xor    %eax,%eax
sub    $0x1,%ebp
cmovb  %eax,%ebp
mov    %ebp,(%r15)
cmp    $0x1,%r13d
je     119a <rgph_fastdiv_prepare+0x6a>
test   %r14d,%r14d
jne    119a <rgph_fastdiv_prepare+0x6a>
xor    %edi,%edi
call   1030 <UINT64_C@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <assert@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @rgph_fastdiv_prepare(i32 %0, i32* %1, i32* %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 %4, i32* %10
%15 = load i32, i32* %6
%16 = sub nsw i32 %15, 1
%17 = call i32 @fls32(i32 %16)
store i32 %17, i32* %11
%18 = call i32 @UINT64_C(i32 1)
%19 = load i32, i32* %11
%20 = shl i32 %18, %19
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = load i32, i32* %6
%23 = sub nsw i32 %21, %22
%24 = shl i32 %23, 32
%25 = load i32, i32* %6
%26 = sdiv i32 %24, %25
store i32 %26, i32* %14
%27 = load i32, i32* %14
%28 = add nsw i32 %27, 1
%29 = load i32*, i32** %7
store i32 %28, i32* %29
%30 = load i32, i32* %11
%31 = icmp sgt i32 %30, 1
br i1 %31, label %32, label %33
32: ; preds = %5
br label %35
33: ; preds = %5
%34 = load i32, i32* %11
br label %35
35: ; preds = %33, %32
%36 = phi i32 [ 1, %32 ], [ %34, %33 ]
%37 = load i32*, i32** %8
store i32 %36, i32* %37
%38 = load i32, i32* %11
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
br label %44
41: ; preds = %35
%42 = load i32, i32* %11
%43 = sub nsw i32 %42, 1
br label %44
44: ; preds = %41, %40
%45 = phi i32 [ 0, %40 ], [ %43, %41 ]
%46 = load i32*, i32** %9
store i32 %45, i32* %46
%47 = load i32, i32* %10
%48 = icmp ne i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %44
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, 1
br i1 %51, label %52, label %53
52: ; preds = %49, %44
br label %98
53: ; preds = %49
%54 = load i32, i32* %14
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %53
%57 = load i32*, i32** %8
%58 = load i32, i32* %57
%59 = sub nsw i32 %58, 1
store i32 %59, i32* %57
%60 = load i32*, i32** %9
%61 = load i32, i32* %60
%62 = add nsw i32 %61, 1
store i32 %62, i32* %60
br label %98
63: ; preds = %53
%64 = load i32, i32* %14
%65 = call i32 @UINT64_C(i32 0)
%66 = add nsw i32 %64, %65
%67 = sdiv i32 %66, 2
store i32 %67, i32* %14
%68 = load i32, i32* %12
%69 = shl i32 %68, 31
%70 = load i32, i32* %14
%71 = load i32, i32* %6
%72 = mul nsw i32 %70, %71
%73 = sub nsw i32 %69, %72
store i32 %73, i32* %13
%74 = load i32, i32* %13
%75 = icmp sgt i32 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %63
%77 = load i32, i32* %13
%78 = load i32, i32* %6
%79 = icmp slt i32 %77, %78
br label %80
80: ; preds = %76, %63
%81 = phi i1 [ false, %63 ], [ %79, %76 ]
%82 = zext i1 %81 to i32
%83 = call i32 @assert(i32 %82)
%84 = load i32, i32* %13
%85 = load i32, i32* %6
%86 = load i32, i32* %12
%87 = sdiv i32 %86, 2
%88 = sub nsw i32 %85, %87
%89 = icmp sgt i32 %84, %88
br i1 %89, label %90, label %97
90: ; preds = %80
%91 = load i32, i32* %14
%92 = add nsw i32 %91, 1
%93 = load i32*, i32** %7
store i32 %92, i32* %93
%94 = load i32*, i32** %8
%95 = load i32, i32* %94
%96 = sub nsw i32 %95, 1
store i32 %96, i32* %94
br label %97
97: ; preds = %90, %80
br label %98
98: ; preds = %52, %97, %56
ret void
}

LLVM IR (-O2):
define void @rgph_fastdiv_prepare(i32 %0, i32* nocapture readnone %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3, i32 %4) {
%6 = add nsw i32 %0, -1
%7 = tail call i32 @fls32(i32 %6)
%8 = tail call i32 @UINT64_C(i32 1)
%9 = icmp slt i32 %7, 1
%10 = select i1 %9, i32 %7, i32 1
store i32 %10, i32* %2
%11 = icmp eq i32 %7, 0
%12 = add nsw i32 %7, -1
%13 = select i1 %11, i32 0, i32 %12
store i32 %13, i32* %3
%14 = icmp ne i32 %4, 0
%15 = icmp eq i32 %0, 1
%16 = or i1 %15, %14
br i1 %16, label %20, label %17
17: ; preds = %5
%18 = tail call i32 @UINT64_C(i32 0)
%19 = tail call i32 @assert(i32 poison)
br label %20
20: ; preds = %5, %17
ret void
}