#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__ off_t;
typedef int /*<<< orphan*/ cmp;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ lseek(int, unsigned long, int /*<<< orphan*/);
scalar_t__ memcmp(unsigned char const *, unsigned char *, unsigned long);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ printf(char *, ...);
unsigned long read(int, unsigned char *, unsigned long);
void flash_verify(int fd, unsigned long off, unsigned long len,
                  const unsigned char *buf) {
  unsigned long blen;
  unsigned char cmp[2048];
  if (lseek(fd, off, SEEK_SET) == (off_t)-1)
    panic("error setting offset\n");
  blen = sizeof(cmp);
  while (len != 0) {
    if (blen > len)
      blen = len;
    printf("verifying: %08lX .. %08lX\r", off, off + blen - 1);
    off += blen;
    if (read(fd, cmp, blen) != blen)
      panic("read error while verifying flash\n");
    if (memcmp(buf, cmp, blen) != 0)
      panic("verify error\n");
    buf += blen;
    len -= blen;
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<flash_verify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    1195 <flash_verify+0x45>
lea    0xe70(%rip),%rdi
call   1050 <panic@plt>
movq   $0x800,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     125b <flash_verify+0x10b>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11be <flash_verify+0x6e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
add    -0x28(%rbp),%rdx
sub    $0x1,%rdx
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
cmp    -0x28(%rbp),%rax
je     1211 <flash_verify+0xc1>
lea    0xe25(%rip),%rdi
call   1050 <panic@plt>
mov    -0x20(%rbp),%rdi
lea    -0x830(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     123b <flash_verify+0xeb>
lea    0xe1d(%rip),%rdi
call   1050 <panic@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    119d <flash_verify+0x4d>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_verify>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r12d
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     119b <flash_verify+0x3b>
test   %r15,%r15
jne    11b0 <flash_verify+0x50>
jmp    122b <flash_verify+0xcb>
lea    0xe5e(%rip),%rdi
call   1060 <panic@plt>
test   %r15,%r15
je     122b <flash_verify+0xcb>
mov    $0x800,%ebp
mov    %rsp,%r13
jmp    11cb <flash_verify+0x6b>
nopw   0x0(%rax,%rax,1)
add    %rbp,%rbx
add    %rbp,%r14
sub    %rbp,%r15
je     122b <flash_verify+0xcb>
cmp    %r15,%rbp
cmova  %r15,%rbp
lea    (%rbx,%rbp,1),%rdx
add    $0xffffffffffffffff,%rdx
lea    0xe35(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1070 <read@plt>
cmp    %rbp,%rax
je     120a <flash_verify+0xaa>
lea    0xe2c(%rip),%rdi
call   1060 <panic@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1080 <memcmp@plt>
test   %rax,%rax
je     11c0 <flash_verify+0x60>
lea    0xe2f(%rip),%rdi
call   1060 <panic@plt>
jmp    11c0 <flash_verify+0x60>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [22 x i8] c"error setting offset\0A\00"
@.str.1 = [27 x i8] c"verifying: %08lX .. %08lX\0D\00"
@.str.2 = [34 x i8] c"read error while verifying flash\0A\00"
@.str.3 = [14 x i8] c"verify error\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @flash_verify(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [2048 x i8]
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i8* %3, i8** %8
%11 = load i32, i32* %5
%12 = load i64, i64* %6
%13 = load i32, i32* @SEEK_SET
%14 = call i64 @lseek(i32 %11, i64 %12, i32 %13)
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %4
store i64 2048, i64* %9
br label %19
19: ; preds = %54, %18
%20 = load i64, i64* %7
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %61
22: ; preds = %19
%23 = load i64, i64* %9
%24 = load i64, i64* %7
%25 = icmp ugt i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = load i64, i64* %7
store i64 %27, i64* %9
br label %28
28: ; preds = %26, %22
%29 = load i64, i64* %6
%30 = load i64, i64* %6
%31 = load i64, i64* %9
%32 = add i64 %30, %31
%33 = sub i64 %32, 1
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %29, i64 %33)
%35 = load i64, i64* %9
%36 = load i64, i64* %6
%37 = add i64 %36, %35
store i64 %37, i64* %6
%38 = load i32, i32* %5
%39 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%40 = load i64, i64* %9
%41 = call i64 @read(i32 %38, i8* %39, i64 %40)
%42 = load i64, i64* %9
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %28
%45 = call i32 @panic(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %28
%47 = load i8*, i8** %8
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%49 = load i64, i64* %9
%50 = call i64 @memcmp(i8* %47, i8* %48, i64 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @panic(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %52, %46
%55 = load i64, i64* %9
%56 = load i8*, i8** %8
%57 = getelementptr inbounds i8, i8* %56, i64 %55
store i8* %57, i8** %8
%58 = load i64, i64* %9
%59 = load i64, i64* %7
%60 = sub i64 %59, %58
store i64 %60, i64* %7
br label %19
61: ; preds = %19
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [22 x i8] c"error setting offset\0A\00"
@.str.1 = [27 x i8] c"verifying: %08lX .. %08lX\0D\00"
@.str.2 = [34 x i8] c"read error while verifying flash\0A\00"
@.str.3 = [14 x i8] c"verify error\0A\00"
define void @flash_verify(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca [2048 x i8]
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @lseek(i32 %0, i64 %1, i32 %7)
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %4
%13 = icmp eq i64 %2, 0
br i1 %13, label %37, label %14
14: ; preds = %12, %33
%15 = phi i64 [ %20, %33 ], [ 2048, %12 ]
%16 = phi i8* [ %34, %33 ], [ %3, %12 ]
%17 = phi i64 [ %35, %33 ], [ %2, %12 ]
%18 = phi i64 [ %21, %33 ], [ %1, %12 ]
%19 = icmp ugt i64 %15, %17
%20 = select i1 %19, i64 %17, i64 %15
%21 = add i64 %20, %18
%22 = add i64 %21, -1
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %18, i64 %22)
%24 = call i64 @read(i32 %0, i8* %6, i64 %20)
%25 = icmp eq i64 %24, %20
br i1 %25, label %28, label %26
26: ; preds = %14
%27 = call i32 @panic(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %14
%29 = call i64 @memcmp(i8* %16, i8* %6, i64 %20)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @panic(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = getelementptr inbounds i8, i8* %16, i64 %20
%35 = sub i64 %17, %20
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %14
37: ; preds = %33, %12
%38 = call i32 @putchar(i32 10)
ret void
}