#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 */
int /*<<< orphan*/ bitwrite(unsigned int *, unsigned int, int, int);
unsigned int rice_encode(unsigned int *buf, unsigned int pos, unsigned int val,
                         unsigned int nbits) {
  val -= (1);
  unsigned int w;
  bitwrite(buf, pos, nbits, val);
  pos += nbits;
  for (w = (val >> nbits); w > 0; w--) {
    bitwrite(buf, pos, 1, 1);
    pos++;
  }
  bitwrite(buf, pos, 1, 0);
  pos++;
  return pos;
}


x86 assembly (clang, -O0):
<rice_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%ecx
call   1030 <bitwrite@plt>
mov    -0x14(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x14(%rbp),%ecx
shr    %cl,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jbe    1188 <rice_encode+0x78>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1030 <bitwrite@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1154 <rice_encode+0x44>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <bitwrite@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rice_encode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
add    $0xffffffff,%ebp
mov    %ecx,%edx
mov    %ebp,%ecx
call   1030 <bitwrite@plt>
add    %r15d,%ebx
mov    %r15d,%ecx
shr    %cl,%ebp
test   %ebp,%ebp
je     115c <rice_encode+0x4c>
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1030 <bitwrite@plt>
add    $0x1,%ebx
add    $0xffffffff,%ebp
jne    1140 <rice_encode+0x30>
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <bitwrite@plt>
add    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rice_encode(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i32, i32* %7
%11 = sub i32 %10, 1
store i32 %11, i32* %7
%12 = load i32*, i32** %5
%13 = load i32, i32* %6
%14 = load i32, i32* %8
%15 = load i32, i32* %7
%16 = call i32 @bitwrite(i32* %12, i32 %13, i32 %14, i32 %15)
%17 = load i32, i32* %8
%18 = load i32, i32* %6
%19 = add i32 %18, %17
store i32 %19, i32* %6
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = lshr i32 %20, %21
store i32 %22, i32* %9
br label %23
23: ; preds = %32, %4
%24 = load i32, i32* %9
%25 = icmp ugt i32 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %23
%27 = load i32*, i32** %5
%28 = load i32, i32* %6
%29 = call i32 @bitwrite(i32* %27, i32 %28, i32 1, i32 1)
%30 = load i32, i32* %6
%31 = add i32 %30, 1
store i32 %31, i32* %6
br label %32
32: ; preds = %26
%33 = load i32, i32* %9
%34 = add i32 %33, -1
store i32 %34, i32* %9
br label %23
35: ; preds = %23
%36 = load i32*, i32** %5
%37 = load i32, i32* %6
%38 = call i32 @bitwrite(i32* %36, i32 %37, i32 1, i32 0)
%39 = load i32, i32* %6
%40 = add i32 %39, 1
store i32 %40, i32* %6
%41 = load i32, i32* %6
ret i32 %41
}

LLVM IR (-O2):
define i32 @rice_encode(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = add i32 %2, -1
%6 = tail call i32 @bitwrite(i32* %0, i32 %1, i32 %3, i32 %5)
%7 = add i32 %3, %1
%8 = lshr i32 %5, %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %4, %10
%11 = phi i32 [ %15, %10 ], [ %8, %4 ]
%12 = phi i32 [ %14, %10 ], [ %7, %4 ]
%13 = tail call i32 @bitwrite(i32* %0, i32 %12, i32 1, i32 1)
%14 = add i32 %12, 1
%15 = add i32 %11, -1
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %10
17: ; preds = %10, %4
%18 = phi i32 [ %7, %4 ], [ %14, %10 ]
%19 = tail call i32 @bitwrite(i32* %0, i32 %18, i32 1, i32 0)
%20 = add i32 %18, 1
ret i32 %20
}