#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int lwwire_fetch_sector(unsigned char, int, unsigned char *);
scalar_t__ lwwire_read(unsigned char *, int);
int lwwire_read2(unsigned char *, int, int);
int /*<<< orphan*/ lwwire_write(unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void lwwire_proto_readex(void) {
  unsigned char buf[256];
  int lsn;
  int ec;
  int i;
  int csum;
  if (lwwire_read(buf, 4) < 0)
    return;
  lsn = (buf[1] << 16) | (buf[2] << 8) | buf[3];
  ec = lwwire_fetch_sector(buf[0], lsn, buf);
  if (ec)
    memset(buf, 0, 256);
  for (i = 0, csum = 0; i < 256; i++)
    csum += buf[i];
  lwwire_write(buf, 256);
  if ((i = lwwire_read2(buf, 2, 5)) < 0) {
    fprintf(stderr, "Error reading protocol bytes: %d, %s\n", i,
            strerror(errno));
    return;
  }
  i = (buf[0] << 8) | buf[1];
  if (i != csum)
    ec = 0xF3;
  buf[0] = ec;
  lwwire_write(buf, 1);
}


x86 assembly (clang, -O0):
<lwwire_proto_readex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    -0x100(%rbp),%rdi
mov    $0x4,%esi
call   1090 <lwwire_read@plt>
cmp    $0x0,%rax
jge    119b <lwwire_proto_readex+0x2b>
jmp    1325 <lwwire_proto_readex+0x1b5>
movzbl -0xff(%rbp),%eax
shl    $0x10,%eax
movzbl -0xfe(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0xfd(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x104(%rbp)
mov    -0x100(%rbp),%al
mov    -0x104(%rbp),%esi
lea    -0x100(%rbp),%rdx
movzbl %al,%edi
call   1040 <lwwire_fetch_sector@plt>
mov    %eax,-0x108(%rbp)
cmpl   $0x0,-0x108(%rbp)
je     1201 <lwwire_proto_readex+0x91>
lea    -0x100(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
movl   $0x0,-0x10c(%rbp)
movl   $0x0,-0x110(%rbp)
cmpl   $0x100,-0x10c(%rbp)
jge    1254 <lwwire_proto_readex+0xe4>
movslq -0x10c(%rbp),%rax
movzbl -0x100(%rbp,%rax,1),%eax
ff
add    -0x110(%rbp),%eax
mov    %eax,-0x110(%rbp)
mov    -0x10c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10c(%rbp)
jmp    1215 <lwwire_proto_readex+0xa5>
lea    -0x100(%rbp),%rdi
mov    $0x100,%esi
call   1070 <lwwire_write@plt>
lea    -0x100(%rbp),%rdi
mov    $0x2,%esi
mov    $0x5,%edx
call   1050 <lwwire_read2@plt>
mov    %eax,-0x10c(%rbp)
cmp    $0x0,%eax
jge    12d3 <lwwire_proto_readex+0x163>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x118(%rbp)
mov    -0x10c(%rbp),%eax
mov    %eax,-0x114(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x118(%rbp),%edi
mov    -0x114(%rbp),%edx
mov    %rax,%rcx
lea    0xd37(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1325 <lwwire_proto_readex+0x1b5>
movzbl -0x100(%rbp),%eax
shl    $0x8,%eax
movzbl -0xff(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x10c(%rbp)
mov    -0x10c(%rbp),%eax
cmp    -0x110(%rbp),%eax
je     1308 <lwwire_proto_readex+0x198>
movl   $0xf3,-0x108(%rbp)
mov    -0x108(%rbp),%eax
mov    %al,-0x100(%rbp)
lea    -0x100(%rbp),%rdi
mov    $0x1,%esi
call   1070 <lwwire_write@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lwwire_proto_readex>:
push   %rbp
push   %rbx
sub    $0x128,%rsp
lea    0x20(%rsp),%rdi
mov    $0x4,%esi
call   1090 <lwwire_read@plt>
test   %rax,%rax
js     12dd <lwwire_proto_readex+0x16d>
movzbl 0x21(%rsp),%eax
shl    $0x10,%eax
movzbl 0x22(%rsp),%ecx
shl    $0x8,%ecx
or     %eax,%ecx
movzbl 0x23(%rsp),%esi
or     %ecx,%esi
movzbl 0x20(%rsp),%edi
lea    0x20(%rsp),%rdx
call   1040 <lwwire_fetch_sector@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11d0 <lwwire_proto_readex+0x60>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
pxor   %xmm0,%xmm0
xor    %eax,%eax
pxor   %xmm4,%xmm4
pxor   %xmm3,%xmm3
xchg   %ax,%ax
movdqa 0x20(%rsp,%rax,1),%xmm1
movd   0x24(%rsp,%rax,1),%xmm2
punpcklbw %xmm0,%xmm1
punpcklwd %xmm0,%xmm1
paddd  %xmm4,%xmm1
punpcklbw %xmm0,%xmm2
punpcklwd %xmm0,%xmm2
paddd  %xmm3,%xmm2
movq   0x28(%rsp,%rax,1),%xmm4
movd   0x2c(%rsp,%rax,1),%xmm3
punpcklbw %xmm0,%xmm4
punpcklwd %xmm0,%xmm4
paddd  %xmm1,%xmm4
punpcklbw %xmm0,%xmm3
punpcklwd %xmm0,%xmm3
paddd  %xmm2,%xmm3
add    $0x10,%rax
cmp    $0x100,%rax
jne    11e0 <lwwire_proto_readex+0x70>
movdqa %xmm4,(%rsp)
movdqa %xmm3,0x10(%rsp)
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1070 <lwwire_write@plt>
mov    %rbp,%rdi
mov    $0x2,%esi
mov    $0x5,%edx
call   1050 <lwwire_read2@plt>
test   %eax,%eax
js     12b1 <lwwire_proto_readex+0x141>
movdqa 0x10(%rsp),%xmm1
paddd  (%rsp),%xmm1
pshufd $0xee,%xmm1,%xmm0
paddd  %xmm1,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%eax
movzwl 0x20(%rsp),%ecx
shl    $0x10,%ecx
bswap  %ecx
cmp    %eax,%ecx
mov    $0xf3,%eax
cmove  %ebx,%eax
mov    %al,0x20(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x1,%esi
call   1070 <lwwire_write@plt>
jmp    12dd <lwwire_proto_readex+0x16d>
mov    %eax,%ebp
mov    0x2d26(%rip),%rax
mov    (%rax),%ebx
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd2f(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
add    $0x128,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Error reading protocol bytes: %d, %s\0A\00"
@errno = global i32 0
define void @lwwire_proto_readex() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%7 = call i64 @lwwire_read(i8* %6, i32 4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
br label %80
10: ; preds = %0
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 1
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = shl i32 %13, 16
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 2
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = shl i32 %17, 8
%19 = or i32 %14, %18
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 3
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = or i32 %19, %22
store i32 %23, i32* %2
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%25 = load i8, i8* %24
%26 = load i32, i32* %2
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%28 = call i32 @lwwire_fetch_sector(i8 zeroext %25, i32 %26, i8* %27)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %10
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 256)
br label %34
34: ; preds = %31, %10
store i32 0, i32* %4
store i32 0, i32* %5
br label %35
35: ; preds = %46, %34
%36 = load i32, i32* %4
%37 = icmp slt i32 %36, 256
br i1 %37, label %38, label %49
38: ; preds = %35
%39 = load i32, i32* %4
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = load i32, i32* %5
%45 = add nsw i32 %44, %43
store i32 %45, i32* %5
br label %46
46: ; preds = %38
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %35
49: ; preds = %35
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%51 = call i32 @lwwire_write(i8* %50, i32 256)
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%53 = call i32 @lwwire_read2(i8* %52, i32 2, i32 5)
store i32 %53, i32* %4
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = load i32, i32* %4
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %57, i8* %59)
br label %80
61: ; preds = %49
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%63 = load i8, i8* %62
%64 = zext i8 %63 to i32
%65 = shl i32 %64, 8
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 1
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = or i32 %65, %68
store i32 %69, i32* %4
%70 = load i32, i32* %4
%71 = load i32, i32* %5
%72 = icmp ne i32 %70, %71
br i1 %72, label %73, label %74
73: ; preds = %61
store i32 243, i32* %3
br label %74
74: ; preds = %73, %61
%75 = load i32, i32* %3
%76 = trunc i32 %75 to i8
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
store i8 %76, i8* %77
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%79 = call i32 @lwwire_write(i8* %78, i32 1)
br label %80
80: ; preds = %74, %55, %9
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Error reading protocol bytes: %d, %s\0A\00"
@errno = global i32 0
define void @lwwire_proto_readex() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = call i64 @lwwire_read(i8* %2, i32 4)
%4 = icmp slt i64 %3, 0
br i1 %4, label %74, label %5
5: ; preds = %0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 1
%7 = load i8, i8* %6
%8 = zext i8 %7 to i32
%9 = shl nuw nsw i32 %8, 16
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 2
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = shl nuw nsw i32 %12, 8
%14 = or i32 %13, %9
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 3
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = or i32 %14, %17
%19 = load i8, i8* %2
%20 = call i32 @lwwire_fetch_sector(i8 zeroext %19, i32 %18, i8* %2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %5
%23 = call i32 @memset(i8* %2, i32 0, i32 256)
br label %24
24: ; preds = %5, %22
br label %25
25: ; preds = %25, %24
%26 = phi i64 [ 0, %24 ], [ %50, %25 ]
%27 = phi <4 x i32> [ zeroinitializer, %24 ], [ %48, %25 ]
%28 = phi <4 x i32> [ zeroinitializer, %24 ], [ %49, %25 ]
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %26
%30 = bitcast i8* %29 to <4 x i8>*
%31 = load <4 x i8>, <4 x i8>* %30
%32 = getelementptr inbounds i8, i8* %29, i64 4
%33 = bitcast i8* %32 to <4 x i8>*
%34 = load <4 x i8>, <4 x i8>* %33
%35 = zext <4 x i8> %31 to <4 x i32>
%36 = zext <4 x i8> %34 to <4 x i32>
%37 = add <4 x i32> %27, %35
%38 = add <4 x i32> %28, %36
%39 = or i64 %26, 8
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %39
%41 = bitcast i8* %40 to <4 x i8>*
%42 = load <4 x i8>, <4 x i8>* %41
%43 = getelementptr inbounds i8, i8* %40, i64 4
%44 = bitcast i8* %43 to <4 x i8>*
%45 = load <4 x i8>, <4 x i8>* %44
%46 = zext <4 x i8> %42 to <4 x i32>
%47 = zext <4 x i8> %45 to <4 x i32>
%48 = add <4 x i32> %37, %46
%49 = add <4 x i32> %38, %47
%50 = add nuw nsw i64 %26, 16
%51 = icmp eq i64 %50, 256
br i1 %51, label %52, label %25
52: ; preds = %25
%53 = call i32 @lwwire_write(i8* %2, i32 256)
%54 = call i32 @lwwire_read2(i8* %2, i32 2, i32 5)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %54, i8* %59)
br label %74
61: ; preds = %52
%62 = add <4 x i32> %49, %48
%63 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %62)
%64 = load i8, i8* %2
%65 = zext i8 %64 to i32
%66 = shl nuw nsw i32 %65, 8
%67 = load i8, i8* %6
%68 = zext i8 %67 to i32
%69 = or i32 %66, %68
%70 = icmp eq i32 %69, %63
%71 = trunc i32 %20 to i8
%72 = select i1 %70, i8 %71, i8 -13
store i8 %72, i8* %2
%73 = call i32 @lwwire_write(i8* %2, i32 1)
br label %74
74: ; preds = %0, %61, %56
ret void
}