#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);
    if (force_exit) {
    }
    return 1;
  }
  if (!(crc32_diff & (crc32_diff - 1))) {
    *input_crc32 = computed_crc32;
    if (force_exit) {
    }
    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    1246 <crc32_check_and_repair+0x106>
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    11eb <crc32_check_and_repair+0xab>
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>
cmpl   $0x0,-0x24(%rbp)
je     11df <crc32_check_and_repair+0x9f>
jmp    11df <crc32_check_and_repair+0x9f>
movl   $0x1,-0x4(%rbp)
jmp    1246 <crc32_check_and_repair+0x106>
mov    -0x2c(%rbp),%eax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    1223 <crc32_check_and_repair+0xe3>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x24(%rbp)
je     1217 <crc32_check_and_repair+0xd7>
jmp    1217 <crc32_check_and_repair+0xd7>
movl   $0x2,-0x4(%rbp)
jmp    1246 <crc32_check_and_repair+0x106>
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 %68
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 %47, 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)
%43 = load i32, i32* %9
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %34
br label %46
46: ; preds = %45, %34
store i32 1, i32* %5
br label %68
47: ; preds = %24
%48 = load i32, i32* %11
%49 = load i32, i32* %11
%50 = sub i32 %49, 1
%51 = and i32 %48, %50
%52 = icmp ne i32 %51, 0
br i1 %52, label %60, label %53
53: ; preds = %47
%54 = load i32, i32* %10
%55 = load i32*, i32** %8
store i32 %54, i32* %55
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %53
br label %59
59: ; preds = %58, %53
store i32 2, i32* %5
br label %68
60: ; preds = %47
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
%63 = xor i1 %62, true
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
%66 = load i32, i32* %10
%67 = load i32*, i32** %8
store i32 %66, i32* %67
store i32 -1, i32* %5
br label %68
68: ; preds = %60, %59, %46, %23
%69 = load i32, i32* %5
ret i32 %69
}

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
}