#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*/ uint8_t;
struct flashrom_flashctx {
  int dummy;
};

/* Variables and functions */
int default_spi_read(struct flashrom_flashctx *, int /*<<< orphan*/ *,
                     unsigned int, unsigned int);
int get_fd_from_context(struct flashrom_flashctx *);
int lspcon_i2c_spi_map_page(int, unsigned int);
int lspcon_i2c_spi_read_data(int, int, int /*<<< orphan*/ *,
                             int /*<<< orphan*/);
int /*<<< orphan*/ min(unsigned int, int);
int lspcon_i2c_spi_read(struct flashrom_flashctx *flash, uint8_t *buf,
                        unsigned int start, unsigned int len) {
  unsigned int i;
  int ret = 0;
  if (start & 0xff)
    return default_spi_read(flash, buf, start, len);
  int fd = get_fd_from_context(flash);
  if (fd < 0)
    return -1;
  for (i = 0; i < len; i += 256) {
    ret |= lspcon_i2c_spi_map_page(fd, start + i);
    ret |=
        lspcon_i2c_spi_read_data(fd, (0x9e >> 1), buf + i, min(len - i, 256));
  }
  return ret;
}


x86 assembly (clang, -O0):
<lspcon_i2c_spi_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
cmp    $0x0,%eax
je     1199 <lspcon_i2c_spi_read+0x49>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1040 <default_spi_read@plt>
mov    %eax,-0x4(%rbp)
jmp    1239 <lspcon_i2c_spi_read+0xe9>
mov    -0x10(%rbp),%rdi
call   1030 <get_fd_from_context@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11bb <lspcon_i2c_spi_read+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <lspcon_i2c_spi_read+0xe9>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    1233 <lspcon_i2c_spi_read+0xe3>
mov    -0x2c(%rbp),%edi
mov    -0x1c(%rbp),%esi
add    -0x24(%rbp),%esi
call   1070 <lspcon_i2c_spi_map_page@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
sub    -0x24(%rbp),%edi
mov    $0x100,%esi
call   1060 <min@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
mov    $0x4f,%esi
call   1050 <lspcon_i2c_spi_read_data@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x24(%rbp)
jmp    11c2 <lspcon_i2c_spi_read+0x72>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lspcon_i2c_spi_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r12d
mov    %rsi,%rbx
test   %r12b,%r12b
je     1188 <lspcon_i2c_spi_read+0x38>
mov    %rbx,%rsi
mov    %r12d,%edx
mov    %r14d,%ecx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <default_spi_read@plt>
call   1030 <get_fd_from_context@plt>
test   %eax,%eax
js     1220 <lspcon_i2c_spi_read+0xd0>
xor    %ebp,%ebp
test   %r14d,%r14d
je     1225 <lspcon_i2c_spi_read+0xd5>
mov    %rbx,0x10(%rsp)
mov    %r14d,%ebx
xor    %r15d,%r15d
mov    %eax,0x4(%rsp)
mov    %r12,0x8(%rsp)
mov    0x4(%rsp),%r13d
nopl   0x0(%rax)
lea    (%r12,%r15,1),%esi
mov    %r13d,%edi
call   1070 <lspcon_i2c_spi_map_page@plt>
mov    %r14d,%r12d
mov    %eax,%r14d
or     %ebp,%r14d
mov    %r15d,%eax
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,4),%rbp
mov    %ebx,%edi
mov    $0x100,%esi
call   1060 <min@plt>
mov    %r13d,%edi
mov    $0x4f,%esi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1050 <lspcon_i2c_spi_read_data@plt>
mov    %eax,%ebp
or     %r14d,%ebp
mov    %r12d,%r14d
mov    0x8(%rsp),%r12
add    $0x100,%r15d
add    $0xffffff00,%ebx
cmp    %r14d,%r15d
jb     11c0 <lspcon_i2c_spi_read+0x70>
jmp    1225 <lspcon_i2c_spi_read+0xd5>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flashrom_flashctx = type { i32 }
define i32 @lspcon_i2c_spi_read(%struct.flashrom_flashctx* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.flashrom_flashctx*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.flashrom_flashctx* %0, %struct.flashrom_flashctx** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %11
%13 = load i32, i32* %8
%14 = and i32 %13, 255
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %4
%17 = load %struct.flashrom_flashctx*, %struct.flashrom_flashctx** %6
%18 = load i32*, i32** %7
%19 = load i32, i32* %8
%20 = load i32, i32* %9
%21 = call i32 @default_spi_read(%struct.flashrom_flashctx* %17, i32* %18, i32 %19, i32 %20)
store i32 %21, i32* %5
br label %58
22: ; preds = %4
%23 = load %struct.flashrom_flashctx*, %struct.flashrom_flashctx** %6
%24 = call i32 @get_fd_from_context(%struct.flashrom_flashctx* %23)
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 -1, i32* %5
br label %58
28: ; preds = %22
store i32 0, i32* %10
br label %29
29: ; preds = %53, %28
%30 = load i32, i32* %10
%31 = load i32, i32* %9
%32 = icmp ult i32 %30, %31
br i1 %32, label %33, label %56
33: ; preds = %29
%34 = load i32, i32* %12
%35 = load i32, i32* %8
%36 = load i32, i32* %10
%37 = add i32 %35, %36
%38 = call i32 @lspcon_i2c_spi_map_page(i32 %34, i32 %37)
%39 = load i32, i32* %11
%40 = or i32 %39, %38
store i32 %40, i32* %11
%41 = load i32, i32* %12
%42 = load i32*, i32** %7
%43 = load i32, i32* %10
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
%46 = load i32, i32* %9
%47 = load i32, i32* %10
%48 = sub i32 %46, %47
%49 = call i32 @min(i32 %48, i32 256)
%50 = call i32 @lspcon_i2c_spi_read_data(i32 %41, i32 79, i32* %45, i32 %49)
%51 = load i32, i32* %11
%52 = or i32 %51, %50
store i32 %52, i32* %11
br label %53
53: ; preds = %33
%54 = load i32, i32* %10
%55 = add i32 %54, 256
store i32 %55, i32* %10
br label %29
56: ; preds = %29
%57 = load i32, i32* %11
store i32 %57, i32* %5
br label %58
58: ; preds = %56, %27, %16
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
%struct.flashrom_flashctx = type { i32 }
define i32 @lspcon_i2c_spi_read(%struct.flashrom_flashctx* %0, i32* %1, i32 %2, i32 %3) {
%5 = and i32 %2, 255
%6 = icmp eq i32 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call i32 @default_spi_read(%struct.flashrom_flashctx* %0, i32* %1, i32 %2, i32 %3)
br label %28
9: ; preds = %4
%10 = tail call i32 @get_fd_from_context(%struct.flashrom_flashctx* %0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %28, label %12
12: ; preds = %9
%13 = icmp eq i32 %3, 0
br i1 %13, label %28, label %14
14: ; preds = %12, %14
%15 = phi i32 [ %25, %14 ], [ 0, %12 ]
%16 = phi i32 [ %26, %14 ], [ 0, %12 ]
%17 = add i32 %16, %2
%18 = tail call i32 @lspcon_i2c_spi_map_page(i32 %10, i32 %17)
%19 = or i32 %18, %15
%20 = zext i32 %16 to i64
%21 = getelementptr inbounds i32, i32* %1, i64 %20
%22 = sub i32 %3, %16
%23 = tail call i32 @min(i32 %22, i32 256)
%24 = tail call i32 @lspcon_i2c_spi_read_data(i32 %10, i32 79, i32* %21, i32 %23)
%25 = or i32 %19, %24
%26 = add i32 %16, 256
%27 = icmp ult i32 %26, %3
br i1 %27, label %14, label %28
28: ; preds = %14, %12, %9, %7
%29 = phi i32 [ %8, %7 ], [ -1, %9 ], [ 0, %12 ], [ %25, %14 ]
ret i32 %29
}