#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 lwwire_fetch_sector(unsigned char, int, unsigned char *);
scalar_t__ lwwire_read(unsigned char *, int);
int /*<<< orphan*/ lwwire_write(unsigned char *, int);
void lwwire_proto_read(void) {
  unsigned char buf[259];
  int ec;
  int lsn;
  int i;
  if (lwwire_read(buf, 4) < 0)
    return;
  lsn = (buf[1] << 16) | (buf[2] << 8) | buf[3];
  ec = lwwire_fetch_sector(buf[0], lsn, buf + 1);
  buf[0] = ec;
  lwwire_write(buf, 1);
  if (ec)
    return;
  ec = 0;
  for (i = 1; i < 257; i++)
    ec += buf[i];
  buf[257] = (ec >> 8) & 0xff;
  buf[258] = ec & 0xff;
  lwwire_write(buf + 1, 258);
}


x86 assembly (clang, -O0):
<lwwire_proto_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    -0x110(%rbp),%rdi
mov    $0x4,%esi
call   1050 <lwwire_read@plt>
cmp    $0x0,%rax
jge    115b <lwwire_proto_read+0x2b>
jmp    125b <lwwire_proto_read+0x12b>
movzbl -0x10f(%rbp),%eax
shl    $0x10,%eax
movzbl -0x10e(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x10d(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x118(%rbp)
mov    -0x110(%rbp),%al
mov    -0x118(%rbp),%esi
lea    -0x110(%rbp),%rdx
add    $0x1,%rdx
movzbl %al,%edi
call   1030 <lwwire_fetch_sector@plt>
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%eax
mov    %al,-0x110(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0x1,%esi
call   1040 <lwwire_write@plt>
cmpl   $0x0,-0x114(%rbp)
je     11d4 <lwwire_proto_read+0xa4>
jmp    125b <lwwire_proto_read+0x12b>
movl   $0x0,-0x114(%rbp)
movl   $0x1,-0x11c(%rbp)
cmpl   $0x101,-0x11c(%rbp)
jge    1227 <lwwire_proto_read+0xf7>
movslq -0x11c(%rbp),%rax
movzbl -0x110(%rbp,%rax,1),%eax
ff
add    -0x114(%rbp),%eax
mov    %eax,-0x114(%rbp)
mov    -0x11c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x11c(%rbp)
jmp    11e8 <lwwire_proto_read+0xb8>
mov    -0x114(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0xf(%rbp)
mov    -0x114(%rbp),%eax
and    $0xff,%eax
mov    %al,-0xe(%rbp)
lea    -0x110(%rbp),%rdi
add    $0x1,%rdi
mov    $0x102,%esi
call   1040 <lwwire_write@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lwwire_proto_read>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
mov    %rsp,%rdi
mov    $0x4,%esi
call   1050 <lwwire_read@plt>
test   %rax,%rax
js     1237 <lwwire_proto_read+0x107>
lea    0x1(%rsp),%rbx
movzbl 0x1(%rsp),%eax
shl    $0x10,%eax
movzbl 0x2(%rsp),%ecx
shl    $0x8,%ecx
or     %eax,%ecx
movzbl 0x3(%rsp),%esi
or     %ecx,%esi
movzbl (%rsp),%edi
mov    %rbx,%rdx
call   1030 <lwwire_fetch_sector@plt>
mov    %eax,%ebp
mov    %bpl,(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
call   1040 <lwwire_write@plt>
test   %ebp,%ebp
jne    1237 <lwwire_proto_read+0x107>
pxor   %xmm0,%xmm0
xor    %eax,%eax
pxor   %xmm2,%xmm2
pxor   %xmm1,%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movd   0x1(%rsp,%rax,1),%xmm3
movd   0x5(%rsp,%rax,1),%xmm4
punpcklbw %xmm0,%xmm3
punpcklwd %xmm0,%xmm3
paddd  %xmm2,%xmm3
punpcklbw %xmm0,%xmm4
punpcklwd %xmm0,%xmm4
paddd  %xmm1,%xmm4
movd   0x9(%rsp,%rax,1),%xmm2
movd   0xd(%rsp,%rax,1),%xmm1
punpcklbw %xmm0,%xmm2
punpcklwd %xmm0,%xmm2
paddd  %xmm3,%xmm2
punpcklbw %xmm0,%xmm1
punpcklwd %xmm0,%xmm1
paddd  %xmm4,%xmm1
add    $0x10,%rax
cmp    $0x100,%rax
jne    11b0 <lwwire_proto_read+0x80>
paddd  %xmm2,%xmm1
pshufd $0xee,%xmm1,%xmm0
paddd  %xmm1,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%eax
rol    $0x8,%ax
mov    %ax,0x101(%rsp)
mov    %rbx,%rdi
mov    $0x102,%esi
call   1040 <lwwire_write@plt>
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define void @lwwire_proto_read() {
%1 = alloca [259 x i8]
%2 = getelementptr inbounds [259 x i8], [259 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 %61, label %5
5: ; preds = %0
%6 = getelementptr inbounds [259 x i8], [259 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 [259 x i8], [259 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 [259 x i8], [259 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* %6)
%21 = trunc i32 %20 to i8
store i8 %21, i8* %2
%22 = call i32 @lwwire_write(i8* %2, i32 1)
%23 = icmp eq i32 %20, 0
br i1 %23, label %24, label %61
24: ; preds = %5, %24
%25 = phi i64 [ %50, %24 ], [ 0, %5 ]
%26 = phi <4 x i32> [ %48, %24 ], [ zeroinitializer, %5 ]
%27 = phi <4 x i32> [ %49, %24 ], [ zeroinitializer, %5 ]
%28 = or i64 %25, 1
%29 = getelementptr inbounds [259 x i8], [259 x i8]* %1, i64 0, i64 %28
%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> %26, %35
%38 = add <4 x i32> %27, %36
%39 = or i64 %25, 9
%40 = getelementptr inbounds [259 x i8], [259 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 %25, 16
%51 = icmp eq i64 %50, 256
br i1 %51, label %52, label %24
52: ; preds = %24
%53 = add <4 x i32> %49, %48
%54 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %53)
%55 = lshr i32 %54, 8
%56 = trunc i32 %55 to i8
%57 = getelementptr inbounds [259 x i8], [259 x i8]* %1, i64 0, i64 257
store i8 %56, i8* %57
%58 = trunc i32 %54 to i8
%59 = getelementptr inbounds [259 x i8], [259 x i8]* %1, i64 0, i64 258
store i8 %58, i8* %59
%60 = call i32 @lwwire_write(i8* %6, i32 258)
br label %61
61: ; preds = %5, %0, %52
ret void
}