#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*/ assert(int);
int stbi__shiftsigned(unsigned int v, int shift, int bits) {
  static unsigned int mul_table[9] = {
      0, 0xff, 0x55, 0x49, 0x11, 0x21, 0x41, 0x81, 0x01,
  };
  static unsigned int shift_table[9] = {
      0, 0, 0, 1, 0, 2, 4, 6, 0,
  };
  if (shift < 0)
    v <<= -shift;
  else
    v >>= shift;
  assert(v < 256);
  v >>= (8 - bits);
  assert(bits >= 0 && bits <= 8);
  return (int)((unsigned)v * mul_table[bits]) >> shift_table[bits];
}


x86 assembly (clang, -O0):
<stbi__shiftsigned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    113d <stbi__shiftsigned+0x2d>
xor    %ecx,%ecx
sub    -0x8(%rbp),%ecx
mov    -0x4(%rbp),%eax
shl    %cl,%eax
mov    %eax,-0x4(%rbp)
jmp    1148 <stbi__shiftsigned+0x38>
mov    -0x8(%rbp),%ecx
mov    -0x4(%rbp),%eax
shr    %cl,%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x100,-0x4(%rbp)
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    $0x8,%ecx
sub    -0xc(%rbp),%ecx
mov    -0x4(%rbp),%eax
shr    %cl,%eax
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jl     1185 <stbi__shiftsigned+0x75>
cmpl   $0x8,-0xc(%rbp)
setle  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x4(%rbp),%eax
movslq -0xc(%rbp),%rdx
lea    0x2e90(%rip),%rcx
imul   (%rcx,%rdx,4),%eax
movslq -0xc(%rbp),%rdx
lea    0x2eb1(%rip),%rcx
mov    (%rcx,%rdx,4),%ecx
sar    %cl,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stbi__shiftsigned>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ecx
neg    %cl
mov    %edi,%ebx
shl    %cl,%ebx
mov    %esi,%ecx
shr    %cl,%edi
test   %esi,%esi
cmovns %edi,%ebx
mov    %edx,%ebp
xor    %edi,%edi
cmp    $0x100,%ebx
setb   %dil
call   1030 <assert@plt>
mov    $0x8,%cl
sub    %bpl,%cl
shr    %cl,%ebx
xor    %edi,%edi
cmp    $0x9,%ebp
setb   %dil
call   1030 <assert@plt>
movslq %ebp,%rax
lea    0xeaa(%rip),%rcx
imul   (%rcx,%rax,4),%ebx
lea    0xecf(%rip),%rcx
mov    (%rcx,%rax,4),%cl
sar    %cl,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stbi__shiftsigned.mul_table = internal global [9 x i32] [i32 0, i32 255, i32 85, i32 73, i32 17, i32 33, i32 65, i32 129, i32 1]
@stbi__shiftsigned.shift_table = internal global [9 x i32] [i32 0, i32 0, i32 0, i32 1, i32 0, i32 2, i32 4, i32 6, i32 0]
define i32 @stbi__shiftsigned(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* %5
%11 = sub nsw i32 0, %10
%12 = load i32, i32* %4
%13 = shl i32 %12, %11
store i32 %13, i32* %4
br label %18
14: ; preds = %3
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = lshr i32 %16, %15
store i32 %17, i32* %4
br label %18
18: ; preds = %14, %9
%19 = load i32, i32* %4
%20 = icmp ult i32 %19, 256
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32, i32* %6
%24 = sub nsw i32 8, %23
%25 = load i32, i32* %4
%26 = lshr i32 %25, %24
store i32 %26, i32* %4
%27 = load i32, i32* %6
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = load i32, i32* %6
%31 = icmp sle i32 %30, 8
br label %32
32: ; preds = %29, %18
%33 = phi i1 [ false, %18 ], [ %31, %29 ]
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* %4
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [9 x i32], [9 x i32]* @stbi__shiftsigned.mul_table, i64 0, i64 %38
%40 = load i32, i32* %39
%41 = mul i32 %36, %40
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [9 x i32], [9 x i32]* @stbi__shiftsigned.shift_table, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = ashr i32 %41, %45
ret i32 %46
}

LLVM IR (-O2):
@stbi__shiftsigned.mul_table = internal unnamed_addr constant [9 x i32] [i32 0, i32 255, i32 85, i32 73, i32 17, i32 33, i32 65, i32 129, i32 1]
@stbi__shiftsigned.shift_table = internal unnamed_addr constant [9 x i32] [i32 0, i32 0, i32 0, i32 1, i32 0, i32 2, i32 4, i32 6, i32 0]
define i32 @stbi__shiftsigned(i32 %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %1, 0
%5 = sub nsw i32 0, %1
%6 = shl i32 %0, %5
%7 = lshr i32 %0, %1
%8 = select i1 %4, i32 %6, i32 %7
%9 = icmp ult i32 %8, 256
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = sub nsw i32 8, %2
%13 = lshr i32 %8, %12
%14 = icmp ult i32 %2, 9
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = sext i32 %2 to i64
%18 = getelementptr inbounds [9 x i32], [9 x i32]* @stbi__shiftsigned.mul_table, i64 0, i64 %17
%19 = load i32, i32* %18
%20 = mul i32 %19, %13
%21 = getelementptr inbounds [9 x i32], [9 x i32]* @stbi__shiftsigned.shift_table, i64 0, i64 %17
%22 = load i32, i32* %21
%23 = ashr i32 %20, %22
ret i32 %23
}