#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 scalar_t__ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ free(scalar_t__ *);
scalar_t__ getCRC8(scalar_t__ *, int);
scalar_t__ *malloc(int);
int /*<<< orphan*/ memset(scalar_t__ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read_sector(int, int, scalar_t__ *, int, int);
int check_sector(int fd, int start, uint8_t *buf, int count, int spidev_size) {
  int ret = 0;
  uint8_t crc1, crc2;
  uint8_t *fbuf = NULL;
  fbuf = malloc(count);
  if (fbuf == NULL) {
    perror("malloc failed");
    return -1;
  }
  memset(fbuf, 0, count);
  ret = read_sector(fd, start, fbuf, count, spidev_size);
  if (ret < 0) {
    printf("fast read sector failed.\n");
    free(fbuf);
    return -1;
  }
  crc1 = getCRC8(buf, count);
  crc2 = getCRC8(fbuf, count);
  if (crc1 != crc2) {
    printf("crc check failed. 0x%x / 0x%x\n", crc1, crc2);
    free(fbuf);
    return -1;
  }
  free(fbuf);
  return 0;
}


x86 assembly (clang, -O0):
<check_sector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11c7 <check_sector+0x57>
lea    0xe4a(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <check_sector+0x114>
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
call   1060 <read_sector@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    121b <check_sector+0xab>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <check_sector+0x114>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <getCRC8@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1080 <getCRC8@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     1274 <check_sector+0x104>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <check_sector+0x114>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_sector>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %esi,%r12d
mov    %edi,%r13d
mov    %ecx,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1235 <check_sector+0xb5>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    %r13d,%edi
mov    %r12d,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
mov    %r15d,%r8d
call   1070 <read_sector@plt>
test   %eax,%eax
js     11f7 <check_sector+0x77>
mov    %r14,%rdi
mov    %ebp,%esi
call   1090 <getCRC8@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %ebp,%esi
call   1090 <getCRC8@plt>
cmp    %rax,%r14
jne    1205 <check_sector+0x85>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1226 <check_sector+0xa6>
lea    0xe2f(%rip),%rdi
call   1040 <puts@plt>
jmp    1219 <check_sector+0x99>
lea    0xe02(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
jmp    1221 <check_sector+0xa1>

LLVM IR (-O0):
@.str = [14 x i8] c"malloc failed\00"
@.str.1 = [26 x i8] c"fast read sector failed.\0A\00"
@.str.2 = [31 x i8] c"crc check failed. 0x%x / 0x%x\0A\00"
define i32 @check_sector(i32 %0, i32 %1, i64* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i64* %2, i64** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %12
store i64* null, i64** %15
%16 = load i32, i32* %10
%17 = call i64* @malloc(i32 %16)
store i64* %17, i64** %15
%18 = load i64*, i64** %15
%19 = icmp eq i64* %18, null
br i1 %19, label %20, label %22
20: ; preds = %5
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %57
22: ; preds = %5
%23 = load i64*, i64** %15
%24 = load i32, i32* %10
%25 = call i32 @memset(i64* %23, i32 0, i32 %24)
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = load i64*, i64** %15
%29 = load i32, i32* %10
%30 = load i32, i32* %11
%31 = call i32 @read_sector(i32 %26, i32 %27, i64* %28, i32 %29, i32 %30)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %22
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%36 = load i64*, i64** %15
%37 = call i32 @free(i64* %36)
store i32 -1, i32* %6
br label %57
38: ; preds = %22
%39 = load i64*, i64** %9
%40 = load i32, i32* %10
%41 = call i64 @getCRC8(i64* %39, i32 %40)
store i64 %41, i64* %13
%42 = load i64*, i64** %15
%43 = load i32, i32* %10
%44 = call i64 @getCRC8(i64* %42, i32 %43)
store i64 %44, i64* %14
%45 = load i64, i64* %13
%46 = load i64, i64* %14
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %54
48: ; preds = %38
%49 = load i64, i64* %13
%50 = load i64, i64* %14
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %49, i64 %50)
%52 = load i64*, i64** %15
%53 = call i32 @free(i64* %52)
store i32 -1, i32* %6
br label %57
54: ; preds = %38
%55 = load i64*, i64** %15
%56 = call i32 @free(i64* %55)
store i32 0, i32* %6
br label %57
57: ; preds = %54, %48, %34, %20
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
@.str = [14 x i8] c"malloc failed\00"
@.str.2 = [31 x i8] c"crc check failed. 0x%x / 0x%x\0A\00"
@str = [25 x i8] c"fast read sector failed.\00"
define i32 @check_sector(i32 %0, i32 %1, i64* %2, i32 %3, i32 %4) {
%6 = tail call i64* @malloc(i32 %3)
%7 = icmp eq i64* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %5
%11 = tail call i32 @memset(i64* %6, i32 0, i32 %3)
%12 = tail call i32 @read_sector(i32 %0, i32 %1, i64* %6, i32 %3, i32 %4)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @free(i64* %6)
br label %26
17: ; preds = %10
%18 = tail call i64 @getCRC8(i64* %2, i32 %3)
%19 = tail call i64 @getCRC8(i64* %6, i32 %3)
%20 = icmp eq i64 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %18, i64 %19)
%23 = tail call i32 @free(i64* %6)
br label %26
24: ; preds = %17
%25 = tail call i32 @free(i64* %6)
br label %26
26: ; preds = %24, %21, %14, %8
%27 = phi i32 [ -1, %8 ], [ -1, %14 ], [ -1, %21 ], [ 0, %24 ]
ret i32 %27
}