#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(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 >= 0 && 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
sar    %cl,%eax
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x4(%rbp)
mov    %al,-0xd(%rbp)
jl     1164 <stbi__shiftsigned+0x54>
cmpl   $0x100,-0x4(%rbp)
setl   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    $0x8,%ecx
sub    -0xc(%rbp),%ecx
mov    -0x4(%rbp),%eax
sar    %cl,%eax
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xe(%rbp)
jl     119a <stbi__shiftsigned+0x8a>
cmpl   $0x8,-0xc(%rbp)
setle  %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x4(%rbp),%eax
movslq -0xc(%rbp),%rdx
lea    0x2e7b(%rip),%rcx
imul   (%rcx,%rdx,4),%eax
movslq -0xc(%rbp),%rdx
lea    0x2e9c(%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
sar    %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
sar    %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 = ashr i32 %16, %15
store i32 %17, i32* %4
br label %18
18: ; preds = %14, %9
%19 = load i32, i32* %4
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 256
br label %24
24: ; preds = %21, %18
%25 = phi i1 [ false, %18 ], [ %23, %21 ]
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32, i32* %6
%29 = sub nsw i32 8, %28
%30 = load i32, i32* %4
%31 = ashr i32 %30, %29
store i32 %31, i32* %4
%32 = load i32, i32* %6
%33 = icmp sge i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = load i32, i32* %6
%36 = icmp sle i32 %35, 8
br label %37
37: ; preds = %34, %24
%38 = phi i1 [ false, %24 ], [ %36, %34 ]
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32, i32* %4
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [9 x i32], [9 x i32]* @stbi__shiftsigned.mul_table, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = mul i32 %41, %45
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [9 x i32], [9 x i32]* @stbi__shiftsigned.shift_table, i64 0, i64 %48
%50 = load i32, i32* %49
%51 = ashr i32 %46, %50
ret i32 %51
}

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 = ashr 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 = ashr 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
}