void xnarch_init_llmulshft(const unsigned m_in, const unsigned d_in,
                           unsigned *m_out, unsigned *s_out) {
  volatile unsigned vol_d = d_in;
  unsigned long long mult;
  *s_out = 31;
  while (1) {
    mult = ((unsigned long long)m_in) << *s_out;
    __rthal_do_div(&(mult), (vol_d));
    if (mult <= 0x7FFFFFFF)
      break;
    (*s_out)--;
  }
  *m_out = (unsigned)mult;
}


x86 assembly (clang, -O0):
<xnarch_init_llmulshft>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1f,(%rax)
mov    -0x4(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
shl    %cl,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%esi
lea    -0x28(%rbp),%rdi
mov    $0x0,%al
call   1030 <__rthal_do_div@plt>
cmpq   $0x7fffffff,-0x28(%rbp)
7f
ja     1167 <xnarch_init_llmulshft+0x57>
jmp    1177 <xnarch_init_llmulshft+0x67>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
jmp    1136 <xnarch_init_llmulshft+0x26>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xnarch_init_llmulshft>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %esi,0xc(%rsp)
mov    %edi,%r12d
movl   $0x1f,(%rcx)
mov    %r12,%rax
shl    $0x1f,%rax
mov    %rax,(%rsp)
mov    0xc(%rsp),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   1030 <__rthal_do_div@plt>
mov    (%rsp),%rax
test   $0xffffffff80000000,%rax
je     1188 <xnarch_init_llmulshft+0x78>
mov    $0x80000000,%r13d
mov    %rsp,%r15
xchg   %ax,%ax
mov    (%rbx),%ecx
add    $0xffffffff,%ecx
mov    %r12,%rax
shl    %cl,%rax
mov    %ecx,(%rbx)
mov    %rax,(%rsp)
mov    0xc(%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <__rthal_do_div@plt>
mov    (%rsp),%rax
cmp    %r13,%rax
jae    1160 <xnarch_init_llmulshft+0x50>
mov    %eax,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @xnarch_init_llmulshft(i32 %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%11 = load i32, i32* %6
store volatile i32 %11, i32* %9
%12 = load i32*, i32** %8
store i32 31, i32* %12
br label %13
13: ; preds = %4, %25
%14 = load i32, i32* %5
%15 = zext i32 %14 to i64
%16 = load i32*, i32** %8
%17 = load i32, i32* %16
%18 = zext i32 %17 to i64
%19 = shl i64 %15, %18
store i64 %19, i64* %10
%20 = load volatile i32, i32* %9
%21 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @__rthal_do_div to i32 (i64*, i32, ...)*)(i64* %10, i32 %20)
%22 = load i64, i64* %10
%23 = icmp ule i64 %22, 2147483647
br i1 %23, label %24, label %25
24: ; preds = %13
br label %29
25: ; preds = %13
%26 = load i32*, i32** %8
%27 = load i32, i32* %26
%28 = add i32 %27, -1
store i32 %28, i32* %26
br label %13
29: ; preds = %24
%30 = load i64, i64* %10
%31 = trunc i64 %30 to i32
%32 = load i32*, i32** %7
store i32 %31, i32* %32
ret void
}

LLVM IR (-O2):
define void @xnarch_init_llmulshft(i32 %0, i32 %1, i32* nocapture writeonly %2, i32* nocapture %3) {
%5 = alloca i32
%6 = alloca i64
%7 = bitcast i32* %5 to i8*
store volatile i32 %1, i32* %5
%8 = bitcast i64* %6 to i8*
%9 = zext i32 %0 to i64
store i32 31, i32* %3
%10 = shl nuw nsw i64 %9, 31
store i64 %10, i64* %6
%11 = load volatile i32, i32* %5
%12 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @__rthal_do_div to i32 (i64*, i32, ...)*)(i64* %6, i32 %11)
%13 = load i64, i64* %6
%14 = icmp ult i64 %13, 2147483648
br i1 %14, label %24, label %15
15: ; preds = %4, %15
%16 = load i32, i32* %3
%17 = add i32 %16, -1
store i32 %17, i32* %3
%18 = zext i32 %17 to i64
%19 = shl i64 %9, %18
store i64 %19, i64* %6
%20 = load volatile i32, i32* %5
%21 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @__rthal_do_div to i32 (i64*, i32, ...)*)(i64* %6, i32 %20)
%22 = load i64, i64* %6
%23 = icmp ult i64 %22, 2147483648
br i1 %23, label %24, label %15
24: ; preds = %15, %4
%25 = phi i64 [ %13, %4 ], [ %22, %15 ]
%26 = trunc i64 %25 to i32
store i32 %26, i32* %2
ret void
}