#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 int uint32_t;
struct fb_bitfield {
  int length;
  int offset;
  scalar_t__ msb_right;
};

/* Variables and functions */
int /*<<< orphan*/ abort();
unsigned char normalize_rgb(uint32_t v, struct fb_bitfield const *col) {
  if (col->msb_right)
    abort();
  if (col->length > 8)
    abort();
  v >>= col->offset;
  v &= ((1 << col->length) - 1);
  v <<= 8 - col->length;
  return v;
}


x86 assembly (clang, -O0):
<normalize_rgb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1135 <normalize_rgb+0x25>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x8,(%rax)
jle    1149 <normalize_rgb+0x39>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x4(%rbp),%eax
sar    %cl,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
and    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x8,%ecx
sub    (%rax),%ecx
mov    -0x4(%rbp),%eax
shl    %cl,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
movzbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalize_rgb>:
push   %rax
cmpq   $0x0,0x8(%rsi)
jne    112e <normalize_rgb+0x1e>
mov    (%rsi),%eax
cmp    $0x9,%eax
jge    112e <normalize_rgb+0x1e>
mov    0x4(%rsi),%cl
sar    %cl,%edi
mov    $0x8,%cl
sub    %al,%cl
shl    %cl,%edi
mov    %edi,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.fb_bitfield = type { i32, i32, i64 }
define zeroext i8 @normalize_rgb(i32 %0, %struct.fb_bitfield* %1) {
%3 = alloca i32
%4 = alloca %struct.fb_bitfield*
store i32 %0, i32* %3
store %struct.fb_bitfield* %1, %struct.fb_bitfield** %4
%5 = load %struct.fb_bitfield*, %struct.fb_bitfield** %4
%6 = getelementptr inbounds %struct.fb_bitfield, %struct.fb_bitfield* %5, i32 0, i32 2
%7 = load i64, i64* %6
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 (...) @abort()
unreachable
11: ; preds = %2
%12 = load %struct.fb_bitfield*, %struct.fb_bitfield** %4
%13 = getelementptr inbounds %struct.fb_bitfield, %struct.fb_bitfield* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = icmp sgt i32 %14, 8
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %11
%19 = load %struct.fb_bitfield*, %struct.fb_bitfield** %4
%20 = getelementptr inbounds %struct.fb_bitfield, %struct.fb_bitfield* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load i32, i32* %3
%23 = ashr i32 %22, %21
store i32 %23, i32* %3
%24 = load %struct.fb_bitfield*, %struct.fb_bitfield** %4
%25 = getelementptr inbounds %struct.fb_bitfield, %struct.fb_bitfield* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = shl i32 1, %26
%28 = sub nsw i32 %27, 1
%29 = load i32, i32* %3
%30 = and i32 %29, %28
store i32 %30, i32* %3
%31 = load %struct.fb_bitfield*, %struct.fb_bitfield** %4
%32 = getelementptr inbounds %struct.fb_bitfield, %struct.fb_bitfield* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = sub nsw i32 8, %33
%35 = load i32, i32* %3
%36 = shl i32 %35, %34
store i32 %36, i32* %3
%37 = load i32, i32* %3
%38 = trunc i32 %37 to i8
ret i8 %38
}

LLVM IR (-O2):
%struct.fb_bitfield = type { i32, i32, i64 }
define zeroext i8 @normalize_rgb(i32 %0, %struct.fb_bitfield* nocapture %1) {
%3 = getelementptr inbounds %struct.fb_bitfield, %struct.fb_bitfield* %1, i64 0, i32 2
%4 = load i64, i64* %3
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (...) @abort()
unreachable
8: ; preds = %2
%9 = getelementptr inbounds %struct.fb_bitfield, %struct.fb_bitfield* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp sgt i32 %10, 8
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 (...) @abort()
unreachable
14: ; preds = %8
%15 = getelementptr inbounds %struct.fb_bitfield, %struct.fb_bitfield* %1, i64 0, i32 1
%16 = load i32, i32* %15
%17 = ashr i32 %0, %16
%18 = sub nsw i32 8, %10
%19 = shl i32 %17, %18
%20 = trunc i32 %19 to i8
ret i8 %20
}