#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 long long uint64_t;
typedef int /*<<< orphan*/ int64_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
long long ubits(unsigned char *, unsigned int, unsigned int, int);
int64_t sbits(signed char buf[], unsigned int start, unsigned int width,
              bool le) {
  uint64_t fld = ubits((unsigned char *)buf, start, width, le);
  assert(width > 0);
  if (fld & (1LL << (width - 1))) {
    fld |= (-1LL << (width - 1));
  }
  return (int64_t)fld;
}


x86 assembly (clang, -O0):
<sbits>:
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    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1030 <ubits@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x10(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     119c <sbits+0x7c>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
shl    %cl,%rax
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sbits>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
call   1030 <ubits@plt>
mov    %rax,%r14
xor    %edi,%edi
add    $0xffffffff,%ebx
setb   %dil
call   1040 <assert@plt>
mov    $0xffffffffffffffff,%rax
mov    %ebx,%ecx
shl    %cl,%rax
xor    %ecx,%ecx
bt     %rbx,%r14
cmovae %ecx,%eax
or     %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @sbits(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = load i32, i32* %8
%14 = call i64 @ubits(i8* %10, i32 %11, i32 %12, i32 %13)
store i64 %14, i64* %9
%15 = load i32, i32* %7
%16 = icmp ugt i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i64, i64* %9
%20 = load i32, i32* %7
%21 = sub i32 %20, 1
%22 = zext i32 %21 to i64
%23 = shl i64 1, %22
%24 = and i64 %19, %23
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %4
%27 = load i32, i32* %7
%28 = sub i32 %27, 1
%29 = zext i32 %28 to i64
%30 = shl i64 -1, %29
%31 = load i64, i64* %9
%32 = or i64 %31, %30
store i64 %32, i64* %9
br label %33
33: ; preds = %26, %4
%34 = load i64, i64* %9
%35 = trunc i64 %34 to i32
ret i32 %35
}

LLVM IR (-O2):
define i32 @sbits(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i64 @ubits(i8* %0, i32 %1, i32 %2, i32 %3)
%6 = icmp ne i32 %2, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = add i32 %2, -1
%10 = zext i32 %9 to i64
%11 = shl nuw i64 1, %10
%12 = and i64 %5, %11
%13 = icmp eq i64 %12, 0
%14 = shl nsw i64 -1, %10
%15 = select i1 %13, i64 0, i64 %14
%16 = or i64 %15, %5
%17 = trunc i64 %16 to i32
ret i32 %17
}