#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 */
scalar_t__ Ten90ModeSChecksum(unsigned char *, int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int Ten90FixSingleBitErrors(unsigned char *msg, int msg_number_bits) {
  int j;
  unsigned char aux[14];
  memcpy(aux, msg, msg_number_bits / 8);
  for (j = 5; j < msg_number_bits; j++) {
    int byte_idx = j / 8;
    int bitmask = 1 << (7 - (j & 7));
    aux[byte_idx] ^= bitmask;
    if (0 == Ten90ModeSChecksum(aux, msg_number_bits)) {
      msg[byte_idx] = aux[byte_idx];
      return j;
    }
    aux[byte_idx] ^= bitmask;
  }
  return (-1);
}


x86 assembly (clang, -O0):
<Ten90FixSingleBitErrors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x26(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1040 <memcpy@plt>
movl   $0x5,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11eb <Ten90FixSingleBitErrors+0xcb>
mov    -0x18(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
and    $0x7,%eax
mov    $0x7,%ecx
sub    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edx
movslq -0x2c(%rbp),%rax
movzbl -0x26(%rbp,%rax,1),%ecx
xor    %edx,%ecx
mov    %cl,-0x26(%rbp,%rax,1)
lea    -0x26(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <Ten90ModeSChecksum@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11cb <Ten90FixSingleBitErrors+0xab>
movslq -0x2c(%rbp),%rax
mov    -0x26(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f2 <Ten90FixSingleBitErrors+0xd2>
mov    -0x30(%rbp),%edx
movslq -0x2c(%rbp),%rax
movzbl -0x26(%rbp,%rax,1),%ecx
xor    %edx,%ecx
mov    %cl,-0x26(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1150 <Ten90FixSingleBitErrors+0x30>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Ten90FixSingleBitErrors>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r12d
mov    %rdi,%r14
lea    0x7(%r12),%edx
test   %esi,%esi
cmovns %esi,%edx
sar    $0x3,%edx
lea    0xa(%rsp),%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
cmp    $0x6,%r12d
jl     1199 <Ten90FixSingleBitErrors+0x79>
mov    $0x5,%r13d
lea    0xa(%rsp),%r15
nop
mov    %r13d,%ebx
mov    %r13d,%ecx
not    %cl
and    $0x7,%cl
mov    $0x1,%ebp
shl    %cl,%ebp
shr    $0x3,%rbx
xor    %bpl,0xa(%rsp,%rbx,1)
mov    %r15,%rdi
mov    %r12d,%esi
call   1030 <Ten90ModeSChecksum@plt>
test   %rax,%rax
je     11a1 <Ten90FixSingleBitErrors+0x81>
xor    %bpl,0xa(%rsp,%rbx,1)
add    $0x1,%r13d
cmp    %r13d,%r12d
jne    1160 <Ten90FixSingleBitErrors+0x40>
mov    $0xffffffff,%r13d
jmp    11a9 <Ten90FixSingleBitErrors+0x89>
mov    0xa(%rsp,%rbx,1),%al
mov    %al,(%r14,%rbx,1)
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @Ten90FixSingleBitErrors(i8* %0, i32 %1) {
%3 = alloca [14 x i8]
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%5 = sdiv i32 %1, 8
%6 = call i32 @memcpy(i8* %4, i8* %0, i32 %5)
%7 = icmp sgt i32 %1, 5
br i1 %7, label %8, label %30
8: ; preds = %2, %25
%9 = phi i32 [ %28, %25 ], [ 5, %2 ]
%10 = lshr i32 %9, 3
%11 = and i32 %9, 7
%12 = xor i32 %11, 7
%13 = shl nuw i32 1, %12
%14 = zext i32 %10 to i64
%15 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = trunc i32 %13 to i8
%18 = xor i8 %16, %17
store i8 %18, i8* %15
%19 = call i64 @Ten90ModeSChecksum(i8* %4, i32 %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %8
%22 = zext i32 %10 to i64
%23 = load i8, i8* %15
%24 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 %23, i8* %24
br label %30
25: ; preds = %8
%26 = load i8, i8* %15
%27 = xor i8 %26, %17
store i8 %27, i8* %15
%28 = add nuw nsw i32 %9, 1
%29 = icmp eq i32 %28, %1
br i1 %29, label %30, label %8
30: ; preds = %25, %2, %21
%31 = phi i32 [ %9, %21 ], [ -1, %2 ], [ -1, %25 ]
ret i32 %31
}