#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned char,
                           unsigned int, unsigned int);
int /*<<< orphan*/ fwrite(unsigned char *, unsigned int, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ sendcmd(int, int);
int sgetc(int);
int /*<<< orphan*/ sputc(unsigned char, int);
int /*<<< orphan*/ stderr;
int readsector(unsigned char sector, FILE *f, int s) {
  unsigned char result;
  unsigned char buf[10000];
  unsigned int chksum, chksumsent, len;
  int i;
  sendcmd(5, s);
  sputc(sector, s);
  result = sgetc(s);
  chksum = 0;
  len = sgetc(s);
  len += sgetc(s) * 256;
  for (i = 0; i < len; i++) {
    buf[i] = sgetc(s);
    chksum += buf[i];
  }
  chksumsent = sgetc(s);
  chksumsent += sgetc(s) * 256;
  chksum &= 0xFFFF;
  chksumsent &= 0xFFFF;
  if (((result & 0x1C) == 0) && (chksum == chksumsent)) {
    fwrite(buf, len, 1, f);
    return 1;
  } else {
    fprintf(stderr,
            "readsector: read failed - result %02Xh, "
            "calculated checksum %04Xh, received checksum %04Xh.\n",
            result, chksum, chksumsent);
    return 0;
  }
}


x86 assembly (clang, -O0):
<readsector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2740,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    $0x5,%edi
call   1070 <sendcmd@plt>
mov    -0x5(%rbp),%al
mov    -0x14(%rbp),%esi
movzbl %al,%edi
call   1030 <sputc@plt>
mov    -0x14(%rbp),%edi
call   1050 <sgetc@plt>
mov    %al,-0x15(%rbp)
movl   $0x0,-0x2734(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <sgetc@plt>
mov    %eax,-0x273c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <sgetc@plt>
shl    $0x8,%eax
add    -0x273c(%rbp),%eax
mov    %eax,-0x273c(%rbp)
movl   $0x0,-0x2740(%rbp)
mov    -0x2740(%rbp),%eax
cmp    -0x273c(%rbp),%eax
jae    1220 <readsector+0xd0>
mov    -0x14(%rbp),%edi
call   1050 <sgetc@plt>
mov    %al,%cl
movslq -0x2740(%rbp),%rax
mov    %cl,-0x2730(%rbp,%rax,1)
movslq -0x2740(%rbp),%rax
movzbl -0x2730(%rbp,%rax,1),%eax
ff
add    -0x2734(%rbp),%eax
mov    %eax,-0x2734(%rbp)
mov    -0x2740(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2740(%rbp)
jmp    11c7 <readsector+0x77>
mov    -0x14(%rbp),%edi
call   1050 <sgetc@plt>
mov    %eax,-0x2738(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <sgetc@plt>
shl    $0x8,%eax
add    -0x2738(%rbp),%eax
mov    %eax,-0x2738(%rbp)
mov    -0x2734(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x2734(%rbp)
mov    -0x2738(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x2738(%rbp)
movzbl -0x15(%rbp),%eax
and    $0x1c,%eax
cmp    $0x0,%eax
jne    12b0 <readsector+0x160>
mov    -0x2734(%rbp),%eax
cmp    -0x2738(%rbp),%eax
jne    12b0 <readsector+0x160>
lea    -0x2730(%rbp),%rdi
mov    -0x273c(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
movl   $0x1,-0x4(%rbp)
jmp    12df <readsector+0x18f>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
mov    -0x15(%rbp),%al
mov    -0x2734(%rbp),%ecx
mov    -0x2738(%rbp),%r8d
lea    0xd30(%rip),%rsi
movzbl %al,%edx
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2740,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readsector>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2728,%rsp
mov    %edx,%r15d
mov    %rsi,0x8(%rsp)
mov    %edi,%ebp
mov    $0x5,%edi
mov    %edx,%esi
call   1070 <sendcmd@plt>
mov    %ebp,%edi
mov    %r15d,%esi
call   1030 <sputc@plt>
mov    %r15d,%edi
call   1050 <sgetc@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1050 <sgetc@plt>
mov    %eax,%r12d
mov    %r15d,%edi
call   1050 <sgetc@plt>
mov    %eax,%r14d
shl    $0x8,%r14d
add    %r12d,%r14d
je     11e3 <readsector+0x93>
mov    %ebx,%r13d
mov    %r14d,%r12d
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
call   1050 <sgetc@plt>
mov    %al,0x10(%rsp,%rbx,1)
movzbl %al,%eax
add    %eax,%ebp
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11c0 <readsector+0x70>
movzwl %bp,%r12d
mov    %r13d,%ebx
jmp    11e6 <readsector+0x96>
xor    %r12d,%r12d
mov    %r15d,%edi
call   1050 <sgetc@plt>
mov    %eax,%r13d
mov    %r15d,%edi
call   1050 <sgetc@plt>
shl    $0x8,%eax
add    %r13d,%eax
movzwl %ax,%r8d
test   $0x1c,%bl
jne    122b <readsector+0xdb>
cmp    %r8d,%r12d
jne    122b <readsector+0xdb>
lea    0x10(%rsp),%rdi
mov    $0x1,%ebx
mov    %r14d,%esi
mov    $0x1,%edx
mov    0x8(%rsp),%rcx
call   1060 <fwrite@plt>
jmp    1248 <readsector+0xf8>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
movzbl %bl,%edx
lea    0xdc2(%rip),%rsi
mov    %r12d,%ecx
call   1040 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x2728,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [93 x i8] c"readsector: read failed - result %02Xh, calculated checksum %04Xh, received checksum %04Xh.\0A\00"
define i32 @readsector(i8 zeroext %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8
%9 = alloca [10000 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8 %0, i8* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%14 = load i32, i32* %7
%15 = call i32 @sendcmd(i32 5, i32 %14)
%16 = load i8, i8* %5
%17 = load i32, i32* %7
%18 = call i32 @sputc(i8 zeroext %16, i32 %17)
%19 = load i32, i32* %7
%20 = call i32 @sgetc(i32 %19)
%21 = trunc i32 %20 to i8
store i8 %21, i8* %8
store i32 0, i32* %10
%22 = load i32, i32* %7
%23 = call i32 @sgetc(i32 %22)
store i32 %23, i32* %12
%24 = load i32, i32* %7
%25 = call i32 @sgetc(i32 %24)
%26 = mul nsw i32 %25, 256
%27 = load i32, i32* %12
%28 = add i32 %27, %26
store i32 %28, i32* %12
store i32 0, i32* %13
br label %29
29: ; preds = %47, %3
%30 = load i32, i32* %13
%31 = load i32, i32* %12
%32 = icmp ult i32 %30, %31
br i1 %32, label %33, label %50
33: ; preds = %29
%34 = load i32, i32* %7
%35 = call i32 @sgetc(i32 %34)
%36 = trunc i32 %35 to i8
%37 = load i32, i32* %13
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [10000 x i8], [10000 x i8]* %9, i64 0, i64 %38
store i8 %36, i8* %39
%40 = load i32, i32* %13
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [10000 x i8], [10000 x i8]* %9, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = load i32, i32* %10
%46 = add i32 %45, %44
store i32 %46, i32* %10
br label %47
47: ; preds = %33
%48 = load i32, i32* %13
%49 = add nsw i32 %48, 1
store i32 %49, i32* %13
br label %29
50: ; preds = %29
%51 = load i32, i32* %7
%52 = call i32 @sgetc(i32 %51)
store i32 %52, i32* %11
%53 = load i32, i32* %7
%54 = call i32 @sgetc(i32 %53)
%55 = mul nsw i32 %54, 256
%56 = load i32, i32* %11
%57 = add i32 %56, %55
store i32 %57, i32* %11
%58 = load i32, i32* %10
%59 = and i32 %58, 65535
store i32 %59, i32* %10
%60 = load i32, i32* %11
%61 = and i32 %60, 65535
store i32 %61, i32* %11
%62 = load i8, i8* %8
%63 = zext i8 %62 to i32
%64 = and i32 %63, 28
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %75
66: ; preds = %50
%67 = load i32, i32* %10
%68 = load i32, i32* %11
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %75
70: ; preds = %66
%71 = getelementptr inbounds [10000 x i8], [10000 x i8]* %9, i64 0, i64 0
%72 = load i32, i32* %12
%73 = load i32*, i32** %6
%74 = call i32 @fwrite(i8* %71, i32 %72, i32 1, i32* %73)
store i32 1, i32* %4
br label %81
75: ; preds = %66, %50
%76 = load i32, i32* @stderr
%77 = load i8, i8* %8
%78 = load i32, i32* %10
%79 = load i32, i32* %11
%80 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str, i64 0, i64 0), i8 zeroext %77, i32 %78, i32 %79)
store i32 0, i32* %4
br label %81
81: ; preds = %75, %70
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [93 x i8] c"readsector: read failed - result %02Xh, calculated checksum %04Xh, received checksum %04Xh.\0A\00"
define i32 @readsector(i8 zeroext %0, i32* nocapture %1, i32 %2) {
%4 = alloca [10000 x i8]
%5 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @sendcmd(i32 5, i32 %2)
%7 = tail call i32 @sputc(i8 zeroext %0, i32 %2)
%8 = tail call i32 @sgetc(i32 %2)
%9 = trunc i32 %8 to i8
%10 = tail call i32 @sgetc(i32 %2)
%11 = tail call i32 @sgetc(i32 %2)
%12 = shl nsw i32 %11, 8
%13 = add i32 %12, %10
%14 = icmp eq i32 %13, 0
br i1 %14, label %29, label %15
15: ; preds = %3
%16 = zext i32 %13 to i64
br label %17
17: ; preds = %15, %17
%18 = phi i64 [ 0, %15 ], [ %25, %17 ]
%19 = phi i32 [ 0, %15 ], [ %24, %17 ]
%20 = tail call i32 @sgetc(i32 %2)
%21 = trunc i32 %20 to i8
%22 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 %18
store i8 %21, i8* %22
%23 = and i32 %20, 255
%24 = add i32 %23, %19
%25 = add nuw nsw i64 %18, 1
%26 = icmp eq i64 %25, %16
br i1 %26, label %27, label %17
27: ; preds = %17
%28 = and i32 %24, 65535
br label %29
29: ; preds = %27, %3
%30 = phi i32 [ 0, %3 ], [ %28, %27 ]
%31 = tail call i32 @sgetc(i32 %2)
%32 = tail call i32 @sgetc(i32 %2)
%33 = shl nsw i32 %32, 8
%34 = add i32 %33, %31
%35 = and i32 %34, 65535
%36 = and i32 %8, 28
%37 = icmp eq i32 %36, 0
%38 = icmp eq i32 %30, %35
%39 = select i1 %37, i1 %38, i1 false
br i1 %39, label %40, label %42
40: ; preds = %29
%41 = call i32 @fwrite(i8* %5, i32 %13, i32 1, i32* %1)
br label %45
42: ; preds = %29
%43 = load i32, i32* @stderr
%44 = tail call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([93 x i8], [93 x i8]* @.str, i64 0, i64 0), i8 zeroext %9, i32 %30, i32 %35)
br label %45
45: ; preds = %42, %40
%46 = phi i32 [ 1, %40 ], [ 0, %42 ]
ret i32 %46
}