#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);
unsigned int compute_crc32(void *, int);
int find_corrupted_bit(int, unsigned int const);
int repair_bit(void *, int, int);
int crc32_check_and_repair(void *input, int l, unsigned *input_crc32,
                           int force_exit) {
  unsigned computed_crc32 = compute_crc32(input, l);
  const unsigned crc32_diff = computed_crc32 ^ (*input_crc32);
  if (!crc32_diff) {
    return 0;
  }
  int k = find_corrupted_bit(l, crc32_diff);
  int r = repair_bit(input, l, k);
  if (!r) {
    assert(compute_crc32(input, l) == *input_crc32);
    return 1;
  }
  if (!(crc32_diff & (crc32_diff - 1))) {
    *input_crc32 = computed_crc32;
    return 2;
  }
  assert(!force_exit);
  *input_crc32 = computed_crc32;
  return -1;
}


x86 assembly (clang, -O0):
<crc32_check_and_repair>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <compute_crc32@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
xor    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1187 <crc32_check_and_repair+0x47>
movl   $0x0,-0x4(%rbp)
jmp    1228 <crc32_check_and_repair+0xe8>
mov    -0x14(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1040 <find_corrupted_bit@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1030 <repair_bit@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11dc <crc32_check_and_repair+0x9c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <compute_crc32@plt>
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x1,-0x4(%rbp)
jmp    1228 <crc32_check_and_repair+0xe8>
mov    -0x2c(%rbp),%eax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    1205 <crc32_check_and_repair+0xc5>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    1228 <crc32_check_and_repair+0xe8>
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crc32_check_and_repair>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%r13
call   1050 <compute_crc32@plt>
mov    (%r15),%ebp
xor    %eax,%ebp
je     118f <crc32_check_and_repair+0x4f>
mov    %eax,%r12d
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <find_corrupted_bit@plt>
mov    %r13,%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <repair_bit@plt>
test   %eax,%eax
je     1193 <crc32_check_and_repair+0x53>
lea    -0x1(%rbp),%eax
test   %eax,%ebp
jne    11b2 <crc32_check_and_repair+0x72>
mov    %r12d,(%r15)
mov    $0x2,%eax
jmp    11c8 <crc32_check_and_repair+0x88>
xor    %eax,%eax
jmp    11c8 <crc32_check_and_repair+0x88>
mov    %r13,%rdi
mov    %ebx,%esi
call   1050 <compute_crc32@plt>
xor    %edi,%edi
cmp    (%r15),%eax
sete   %dil
call   1060 <assert@plt>
mov    $0x1,%eax
jmp    11c8 <crc32_check_and_repair+0x88>
xor    %edi,%edi
test   %r14d,%r14d
sete   %dil
call   1060 <assert@plt>
mov    %r12d,(%r15)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @crc32_check_and_repair(i8* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @compute_crc32(i8* %14, i32 %15)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = load i32*, i32** %8
%19 = load i32, i32* %18
%20 = xor i32 %17, %19
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %4
store i32 0, i32* %5
br label %60
24: ; preds = %4
%25 = load i32, i32* %7
%26 = load i32, i32* %11
%27 = call i32 @find_corrupted_bit(i32 %25, i32 %26)
store i32 %27, i32* %12
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = load i32, i32* %12
%31 = call i32 @repair_bit(i8* %28, i32 %29, i32 %30)
store i32 %31, i32* %13
%32 = load i32, i32* %13
%33 = icmp ne i32 %32, 0
br i1 %33, label %43, label %34
34: ; preds = %24
%35 = load i8*, i8** %6
%36 = load i32, i32* %7
%37 = call i32 @compute_crc32(i8* %35, i32 %36)
%38 = load i32*, i32** %8
%39 = load i32, i32* %38
%40 = icmp eq i32 %37, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
store i32 1, i32* %5
br label %60
43: ; preds = %24
%44 = load i32, i32* %11
%45 = load i32, i32* %11
%46 = sub i32 %45, 1
%47 = and i32 %44, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %43
%50 = load i32, i32* %10
%51 = load i32*, i32** %8
store i32 %50, i32* %51
store i32 2, i32* %5
br label %60
52: ; preds = %43
%53 = load i32, i32* %9
%54 = icmp ne i32 %53, 0
%55 = xor i1 %54, true
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
%58 = load i32, i32* %10
%59 = load i32*, i32** %8
store i32 %58, i32* %59
store i32 -1, i32* %5
br label %60
60: ; preds = %52, %49, %34, %23
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
define i32 @crc32_check_and_repair(i8* %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = tail call i32 @compute_crc32(i8* %0, i32 %1)
%6 = load i32, i32* %2
%7 = xor i32 %6, %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %27, label %9
9: ; preds = %4
%10 = tail call i32 @find_corrupted_bit(i32 %1, i32 %7)
%11 = tail call i32 @repair_bit(i8* %0, i32 %1, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = tail call i32 @compute_crc32(i8* %0, i32 %1)
%15 = load i32, i32* %2
%16 = icmp eq i32 %14, %15
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
br label %27
19: ; preds = %9
%20 = tail call i32 @llvm.ctpop.i32(i32 %7)
%21 = icmp ult i32 %20, 2
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 %5, i32* %2
br label %27
23: ; preds = %19
%24 = icmp eq i32 %3, 0
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
store i32 %5, i32* %2
br label %27
27: ; preds = %13, %22, %23, %4
%28 = phi i32 [ 0, %4 ], [ -1, %23 ], [ 2, %22 ], [ 1, %13 ]
ret i32 %28
}