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

/* Variables and functions */
int default_spi_read(struct flashrom_flashctx *, unsigned int *, unsigned int,
                     unsigned int);
int get_fd_from_context(struct flashrom_flashctx *);
int /*<<< orphan*/ min(unsigned int, int);
int realtek_mst_i2c_spi_map_page(int, unsigned int, unsigned int, unsigned int);
int realtek_mst_i2c_spi_read_data(int, int, unsigned int *, int /*<<< orphan*/);
int /*<<< orphan*/ realtek_mst_i2c_spi_read_register(int, int, unsigned int *);
int realtek_mst_i2c_spi_wait_command_done(int, int, int, int /*<<< orphan*/,
                                          int);
int realtek_mst_i2c_spi_write_register(int, int, int);
int realtek_mst_i2c_spi_read(struct flashrom_flashctx *flash, uint8_t *buf,
                             unsigned int start, unsigned int len) {
  unsigned 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;
  start--;
  ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x46);
  ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, 3);
  uint8_t block_idx = start >> 16;
  uint8_t page_idx = start >> 8;
  uint8_t byte_idx = start;
  ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, byte_idx);
  ret |= realtek_mst_i2c_spi_write_register(fd, 0x6a, 0x03);
  ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x47);
  if (ret)
    return ret;
  ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01, 0, 1);
  if (ret)
    return ret;
  uint8_t dummy;
  realtek_mst_i2c_spi_read_register(fd, 0x70, &dummy);
  for (i = 0; i < len; i += 256) {
    ret |= realtek_mst_i2c_spi_read_data(fd, (0x94 >> 1), buf + i,
                                         min(len - i, 256));
    if (ret)
      return ret;
  }
  return ret;
}


x86 assembly (clang, -O0):
<realtek_mst_i2c_spi_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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     11c9 <realtek_mst_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    135d <realtek_mst_i2c_spi_read+0x1dd>
mov    -0x10(%rbp),%rdi
call   1030 <get_fd_from_context@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11eb <realtek_mst_i2c_spi_read+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    135d <realtek_mst_i2c_spi_read+0x1dd>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x60,%esi
mov    $0x46,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x61,%esi
mov    $0x3,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
shr    $0x10,%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x38(%rbp),%ecx
call   10a0 <realtek_mst_i2c_spi_map_page@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x6a,%esi
mov    $0x3,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x60,%esi
mov    $0x47,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1298 <realtek_mst_i2c_spi_read+0x118>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    135d <realtek_mst_i2c_spi_read+0x1dd>
mov    -0x2c(%rbp),%edi
mov    $0x60,%esi
mov    $0x1,%r8d
xor    %ecx,%ecx
mov    %r8d,%edx
call   1050 <realtek_mst_i2c_spi_wait_command_done@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12c8 <realtek_mst_i2c_spi_read+0x148>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    135d <realtek_mst_i2c_spi_read+0x1dd>
mov    -0x2c(%rbp),%edi
mov    $0x70,%esi
lea    -0x3c(%rbp),%rdx
call   1060 <realtek_mst_i2c_spi_read_register@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    1357 <realtek_mst_i2c_spi_read+0x1d7>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%edi
sub    -0x24(%rbp),%edi
mov    $0x100,%esi
call   1080 <min@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
mov    $0x4a,%esi
call   1090 <realtek_mst_i2c_spi_read_data@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1342 <realtek_mst_i2c_spi_read+0x1c2>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    135d <realtek_mst_i2c_spi_read+0x1dd>
jmp    1347 <realtek_mst_i2c_spi_read+0x1c7>
mov    -0x24(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x24(%rbp)
jmp    12e0 <realtek_mst_i2c_spi_read+0x160>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realtek_mst_i2c_spi_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %rsi,%r15
test   %bl,%bl
je     11b2 <realtek_mst_i2c_spi_read+0x32>
mov    %r15,%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
add    $0x8,%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     12be <realtek_mst_i2c_spi_read+0x13e>
mov    %eax,%r12d
add    $0xffffffff,%ebx
mov    %eax,%edi
mov    $0x60,%esi
mov    $0x46,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
mov    %eax,%r13d
mov    %r12d,%edi
mov    $0x61,%esi
mov    $0x3,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
mov    %eax,%ebp
or     %r13d,%ebp
mov    %ebx,%esi
shr    $0x10,%esi
mov    %ebx,%edx
shr    $0x8,%edx
mov    %r12d,%edi
mov    %ebx,%ecx
call   10a0 <realtek_mst_i2c_spi_map_page@plt>
mov    %eax,%ebx
or     %ebp,%ebx
mov    %r12d,%edi
mov    $0x6a,%esi
mov    $0x3,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
mov    %eax,%ebp
or     %ebx,%ebp
mov    %r12d,%edi
mov    $0x60,%esi
mov    $0x47,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
or     %ebp,%eax
jne    12c3 <realtek_mst_i2c_spi_read+0x143>
xor    %r13d,%r13d
mov    %r12d,%edi
mov    $0x60,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
mov    $0x1,%r8d
call   1050 <realtek_mst_i2c_spi_wait_command_done@plt>
test   %eax,%eax
jne    12c3 <realtek_mst_i2c_spi_read+0x143>
lea    0x4(%rsp),%rdx
mov    %r12d,%edi
mov    $0x70,%esi
call   1060 <realtek_mst_i2c_spi_read_register@plt>
test   %r14d,%r14d
je     12d5 <realtek_mst_i2c_spi_read+0x155>
xor    %ebx,%ebx
mov    %r14d,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%eax
lea    (%r15,%rax,4),%r13
mov    %ebp,%edi
mov    $0x100,%esi
call   1080 <min@plt>
mov    %r12d,%edi
mov    $0x4a,%esi
mov    %r13,%rdx
mov    %eax,%ecx
call   1090 <realtek_mst_i2c_spi_read_data@plt>
test   %eax,%eax
jne    12d2 <realtek_mst_i2c_spi_read+0x152>
add    $0x100,%ebx
add    $0xffffff00,%ebp
cmp    %r14d,%ebx
jb     1280 <realtek_mst_i2c_spi_read+0x100>
xor    %r13d,%r13d
jmp    12d5 <realtek_mst_i2c_spi_read+0x155>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r13d
mov    %r13d,%eax
jmp    12c3 <realtek_mst_i2c_spi_read+0x143>

LLVM IR (-O0):
%struct.flashrom_flashctx = type { i32 }
define i32 @realtek_mst_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
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = 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
%17 = load i32, i32* %8
%18 = and i32 %17, 255
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %4
%21 = load %struct.flashrom_flashctx*, %struct.flashrom_flashctx** %6
%22 = load i32*, i32** %7
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = call i32 @default_spi_read(%struct.flashrom_flashctx* %21, i32* %22, i32 %23, i32 %24)
store i32 %25, i32* %5
br label %104
26: ; preds = %4
%27 = load %struct.flashrom_flashctx*, %struct.flashrom_flashctx** %6
%28 = call i32 @get_fd_from_context(%struct.flashrom_flashctx* %27)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 -1, i32* %5
br label %104
32: ; preds = %26
%33 = load i32, i32* %8
%34 = add i32 %33, -1
store i32 %34, i32* %8
%35 = load i32, i32* %12
%36 = call i32 @realtek_mst_i2c_spi_write_register(i32 %35, i32 96, i32 70)
%37 = load i32, i32* %11
%38 = or i32 %37, %36
store i32 %38, i32* %11
%39 = load i32, i32* %12
%40 = call i32 @realtek_mst_i2c_spi_write_register(i32 %39, i32 97, i32 3)
%41 = load i32, i32* %11
%42 = or i32 %41, %40
store i32 %42, i32* %11
%43 = load i32, i32* %8
%44 = lshr i32 %43, 16
store i32 %44, i32* %13
%45 = load i32, i32* %8
%46 = lshr i32 %45, 8
store i32 %46, i32* %14
%47 = load i32, i32* %8
store i32 %47, i32* %15
%48 = load i32, i32* %12
%49 = load i32, i32* %13
%50 = load i32, i32* %14
%51 = load i32, i32* %15
%52 = call i32 @realtek_mst_i2c_spi_map_page(i32 %48, i32 %49, i32 %50, i32 %51)
%53 = load i32, i32* %11
%54 = or i32 %53, %52
store i32 %54, i32* %11
%55 = load i32, i32* %12
%56 = call i32 @realtek_mst_i2c_spi_write_register(i32 %55, i32 106, i32 3)
%57 = load i32, i32* %11
%58 = or i32 %57, %56
store i32 %58, i32* %11
%59 = load i32, i32* %12
%60 = call i32 @realtek_mst_i2c_spi_write_register(i32 %59, i32 96, i32 71)
%61 = load i32, i32* %11
%62 = or i32 %61, %60
store i32 %62, i32* %11
%63 = load i32, i32* %11
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %32
%66 = load i32, i32* %11
store i32 %66, i32* %5
br label %104
67: ; preds = %32
%68 = load i32, i32* %12
%69 = call i32 @realtek_mst_i2c_spi_wait_command_done(i32 %68, i32 96, i32 1, i32 0, i32 1)
store i32 %69, i32* %11
%70 = load i32, i32* %11
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = load i32, i32* %11
store i32 %73, i32* %5
br label %104
74: ; preds = %67
%75 = load i32, i32* %12
%76 = call i32 @realtek_mst_i2c_spi_read_register(i32 %75, i32 112, i32* %16)
store i32 0, i32* %10
br label %77
77: ; preds = %99, %74
%78 = load i32, i32* %10
%79 = load i32, i32* %9
%80 = icmp ult i32 %78, %79
br i1 %80, label %81, label %102
81: ; preds = %77
%82 = load i32, i32* %12
%83 = load i32*, i32** %7
%84 = load i32, i32* %10
%85 = zext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
%87 = load i32, i32* %9
%88 = load i32, i32* %10
%89 = sub i32 %87, %88
%90 = call i32 @min(i32 %89, i32 256)
%91 = call i32 @realtek_mst_i2c_spi_read_data(i32 %82, i32 74, i32* %86, i32 %90)
%92 = load i32, i32* %11
%93 = or i32 %92, %91
store i32 %93, i32* %11
%94 = load i32, i32* %11
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %81
%97 = load i32, i32* %11
store i32 %97, i32* %5
br label %104
98: ; preds = %81
br label %99
99: ; preds = %98
%100 = load i32, i32* %10
%101 = add i32 %100, 256
store i32 %101, i32* %10
br label %77
102: ; preds = %77
%103 = load i32, i32* %11
store i32 %103, i32* %5
br label %104
104: ; preds = %102, %96, %72, %65, %31, %20
%105 = load i32, i32* %5
ret i32 %105
}

LLVM IR (-O2):
%struct.flashrom_flashctx = type { i32 }
define i32 @realtek_mst_i2c_spi_read(%struct.flashrom_flashctx* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = and i32 %2, 255
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i32 @default_spi_read(%struct.flashrom_flashctx* %0, i32* %1, i32 %2, i32 %3)
br label %47
10: ; preds = %4
%11 = tail call i32 @get_fd_from_context(%struct.flashrom_flashctx* %0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %47, label %13
13: ; preds = %10
%14 = add i32 %2, -1
%15 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %11, i32 96, i32 70)
%16 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %11, i32 97, i32 3)
%17 = or i32 %16, %15
%18 = lshr i32 %14, 16
%19 = lshr i32 %14, 8
%20 = tail call i32 @realtek_mst_i2c_spi_map_page(i32 %11, i32 %18, i32 %19, i32 %14)
%21 = or i32 %17, %20
%22 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %11, i32 106, i32 3)
%23 = or i32 %21, %22
%24 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %11, i32 96, i32 71)
%25 = or i32 %23, %24
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %13
%28 = tail call i32 @realtek_mst_i2c_spi_wait_command_done(i32 %11, i32 96, i32 1, i32 0, i32 1)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %47
30: ; preds = %27
%31 = bitcast i32* %5 to i8*
%32 = call i32 @realtek_mst_i2c_spi_read_register(i32 %11, i32 112, i32* %5)
%33 = icmp eq i32 %3, 0
br i1 %33, label %45, label %36
34: ; preds = %36
%35 = icmp ult i32 %44, %3
br i1 %35, label %36, label %45
36: ; preds = %30, %34
%37 = phi i32 [ %44, %34 ], [ 0, %30 ]
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %1, i64 %38
%40 = sub i32 %3, %37
%41 = call i32 @min(i32 %40, i32 256)
%42 = call i32 @realtek_mst_i2c_spi_read_data(i32 %11, i32 74, i32* %39, i32 %41)
%43 = icmp eq i32 %42, 0
%44 = add i32 %37, 256
br i1 %43, label %34, label %45
45: ; preds = %36, %34, %30
%46 = phi i32 [ 0, %30 ], [ 0, %34 ], [ %42, %36 ]
br label %47
47: ; preds = %10, %27, %13, %45, %8
%48 = phi i32 [ %9, %8 ], [ -1, %10 ], [ %46, %45 ], [ %25, %13 ], [ %28, %27 ]
ret i32 %48
}