#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 char *uint32_t;

/* Variables and functions */
char *ss_crc32c_hw_byte(char *, unsigned char const *, unsigned int);
uint32_t ss_crc32c_hw(uint32_t crc, const void *buf, int len) {
  unsigned int iquotient = len / sizeof(unsigned long);
  unsigned int iremainder = len % sizeof(unsigned long);
  unsigned long *ptmp = (unsigned long *)buf;
  while (iquotient--) {
    __asm__ __volatile__(".byte 0xf2, "
                         "0x48, "
                         "0xf, 0x38, 0xf1, 0xf1;"
                         : "=S"(crc)
                         : "0"(crc), "c"(*ptmp));
    ptmp++;
  }
  if (iremainder) {
    crc = ss_crc32c_hw_byte(crc, (unsigned char const *)ptmp, iremainder);
  }
  return crc;
}


x86 assembly (clang, -O0):
<ss_crc32c_hw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shr    $0x3,%rax
mov    %eax,-0x18(%rbp)
movslq -0x14(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0x0,%eax
je     117b <ss_crc32c_hw+0x6b>
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
crc32  %rcx,%rsi
mov    %rsi,-0x8(%rbp)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    1141 <ss_crc32c_hw+0x31>
cmpl   $0x0,-0x1c(%rbp)
je     1199 <ss_crc32c_hw+0x89>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <ss_crc32c_hw_byte@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_crc32c_hw>:
mov    %edx,%eax
mov    %rsi,%r8
mov    %rdi,%rsi
and    $0x7,%edx
cmp    $0x8,%eax
jb     1142 <ss_crc32c_hw+0x32>
sar    $0x3,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r8),%rcx
crc32  %rcx,%rsi
add    $0x8,%r8
add    $0xffffffff,%eax
jne    1130 <ss_crc32c_hw+0x20>
test   %edx,%edx
je     1151 <ss_crc32c_hw+0x41>
mov    %rsi,%rdi
mov    %r8,%rsi
jmp    1030 <ss_crc32c_hw_byte@plt>
mov    %rsi,%rax
ret

LLVM IR (-O0):
define i8* @ss_crc32c_hw(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = udiv i64 %11, 8
%13 = trunc i64 %12 to i32
store i32 %13, i32* %7
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = urem i64 %15, 8
%17 = trunc i64 %16 to i32
store i32 %17, i32* %8
%18 = load i8*, i8** %5
%19 = bitcast i8* %18 to i64*
store i64* %19, i64** %9
br label %20
20: ; preds = %24, %3
%21 = load i32, i32* %7
%22 = add i32 %21, -1
store i32 %22, i32* %7
%23 = icmp ne i32 %21, 0
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = load i8*, i8** %4
%26 = load i64*, i64** %9
%27 = load i64, i64* %26
%28 = call i8* asm sideeffect ".byte 0xf2, 0x48, 0xf, 0x38, 0xf1, 0xf1;", "={si},0,{cx},~{dirflag},~{fpsr},~{flags}"(i8* %25, i64 %27)
store i8* %28, i8** %4
%29 = load i64*, i64** %9
%30 = getelementptr inbounds i64, i64* %29, i32 1
store i64* %30, i64** %9
br label %20
31: ; preds = %20
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i8*, i8** %4
%36 = load i64*, i64** %9
%37 = bitcast i64* %36 to i8*
%38 = load i32, i32* %8
%39 = call i8* @ss_crc32c_hw_byte(i8* %35, i8* %37, i32 %38)
store i8* %39, i8** %4
br label %40
40: ; preds = %34, %31
%41 = load i8*, i8** %4
ret i8* %41
}

LLVM IR (-O2):
define i8* @ss_crc32c_hw(i8* %0, i8* %1, i32 %2) {
%4 = and i32 %2, 7
%5 = bitcast i8* %1 to i64*
%6 = icmp ult i32 %2, 8
br i1 %6, label %18, label %7
7: ; preds = %3
%8 = ashr i32 %2, 3
br label %9
9: ; preds = %7, %9
%10 = phi i64* [ %16, %9 ], [ %5, %7 ]
%11 = phi i8* [ %15, %9 ], [ %0, %7 ]
%12 = phi i32 [ %13, %9 ], [ %8, %7 ]
%13 = add i32 %12, -1
%14 = load i64, i64* %10
%15 = tail call i8* asm sideeffect ".byte 0xf2, 0x48, 0xf, 0x38, 0xf1, 0xf1;", "={si},0,{cx},~{dirflag},~{fpsr},~{flags}"(i8* %11, i64 %14)
%16 = getelementptr inbounds i64, i64* %10, i64 1
%17 = icmp eq i32 %13, 0
br i1 %17, label %18, label %9
18: ; preds = %9, %3
%19 = phi i8* [ %0, %3 ], [ %15, %9 ]
%20 = phi i64* [ %5, %3 ], [ %16, %9 ]
%21 = icmp eq i32 %4, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = bitcast i64* %20 to i8*
%24 = tail call i8* @ss_crc32c_hw_byte(i8* %19, i8* %23, i32 %4)
br label %25
25: ; preds = %22, %18
%26 = phi i8* [ %24, %22 ], [ %19, %18 ]
ret i8* %26
}