#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 /*<<< orphan*/ min(unsigned int, int);
int realtek_mst_i2c_spi_map_page(int, unsigned int);
int realtek_mst_i2c_spi_read_data(int, int, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/);
int /*<<< orphan*/ realtek_mst_i2c_spi_read_register(int, int,
                                                     int /*<<< orphan*/ *);
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);
  ret |= realtek_mst_i2c_spi_map_page(fd, start);
  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 += 128) {
    ret |= realtek_mst_i2c_spi_read_data(fd, (0x94 >> 1), buf + i,
                                         min(len - i, 128));
    if (ret)
      return ret;
  }
  return ret;
}


x86 assembly (clang, -O0):
<realtek_mst_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     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    133f <realtek_mst_i2c_spi_read+0x1bf>
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    133f <realtek_mst_i2c_spi_read+0x1bf>
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    -0x2c(%rbp),%edi
mov    -0x1c(%rbp),%esi
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     127a <realtek_mst_i2c_spi_read+0xfa>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133f <realtek_mst_i2c_spi_read+0x1bf>
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     12aa <realtek_mst_i2c_spi_read+0x12a>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133f <realtek_mst_i2c_spi_read+0x1bf>
mov    -0x2c(%rbp),%edi
mov    $0x70,%esi
lea    -0x30(%rbp),%rdx
call   1060 <realtek_mst_i2c_spi_read_register@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    1339 <realtek_mst_i2c_spi_read+0x1b9>
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    $0x80,%esi
call   1080 <min@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%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     1324 <realtek_mst_i2c_spi_read+0x1a4>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133f <realtek_mst_i2c_spi_read+0x1bf>
jmp    1329 <realtek_mst_i2c_spi_read+0x1a9>
mov    -0x24(%rbp),%eax
add    $0x80,%eax
mov    %eax,-0x24(%rbp)
jmp    12c2 <realtek_mst_i2c_spi_read+0x142>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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     12a8 <realtek_mst_i2c_spi_read+0x128>
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    %r12d,%edi
mov    %ebx,%esi
call   10a0 <realtek_mst_i2c_spi_map_page@plt>
mov    %eax,%r13d
mov    %r12d,%edi
mov    $0x6a,%esi
mov    $0x3,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
mov    %eax,%ebx
or     %r13d,%ebx
or     %ebp,%ebx
mov    %r12d,%edi
mov    $0x60,%esi
mov    $0x47,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
or     %ebx,%eax
jne    12ad <realtek_mst_i2c_spi_read+0x12d>
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    12ad <realtek_mst_i2c_spi_read+0x12d>
lea    0x4(%rsp),%rdx
mov    %r12d,%edi
mov    $0x70,%esi
call   1060 <realtek_mst_i2c_spi_read_register@plt>
test   %r14d,%r14d
je     12bf <realtek_mst_i2c_spi_read+0x13f>
xor    %ebx,%ebx
mov    %r14d,%ebp
nopl   (%rax)
mov    %ebx,%eax
lea    (%r15,%rax,4),%r13
mov    %ebp,%edi
mov    $0x80,%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    12bc <realtek_mst_i2c_spi_read+0x13c>
sub    $0xffffff80,%ebx
add    $0xffffff80,%ebp
cmp    %r14d,%ebx
jb     1270 <realtek_mst_i2c_spi_read+0xf0>
xor    %r13d,%r13d
jmp    12bf <realtek_mst_i2c_spi_read+0x13f>
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    12ad <realtek_mst_i2c_spi_read+0x12d>

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

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 %45
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 %45, 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 = tail call i32 @realtek_mst_i2c_spi_map_page(i32 %11, i32 %14)
%19 = or i32 %17, %18
%20 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %11, i32 106, i32 3)
%21 = or i32 %19, %20
%22 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %11, i32 96, i32 71)
%23 = or i32 %21, %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %45
25: ; preds = %13
%26 = tail call i32 @realtek_mst_i2c_spi_wait_command_done(i32 %11, i32 96, i32 1, i32 0, i32 1)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %45
28: ; preds = %25
%29 = bitcast i32* %5 to i8*
%30 = call i32 @realtek_mst_i2c_spi_read_register(i32 %11, i32 112, i32* %5)
%31 = icmp eq i32 %3, 0
br i1 %31, label %43, label %34
32: ; preds = %34
%33 = icmp ult i32 %42, %3
br i1 %33, label %34, label %43
34: ; preds = %28, %32
%35 = phi i32 [ %42, %32 ], [ 0, %28 ]
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %1, i64 %36
%38 = sub i32 %3, %35
%39 = call i32 @min(i32 %38, i32 128)
%40 = call i32 @realtek_mst_i2c_spi_read_data(i32 %11, i32 74, i32* %37, i32 %39)
%41 = icmp eq i32 %40, 0
%42 = add i32 %35, 128
br i1 %41, label %32, label %43
43: ; preds = %34, %32, %28
%44 = phi i32 [ 0, %28 ], [ 0, %32 ], [ %40, %34 ]
br label %45
45: ; preds = %43, %10, %13, %25, %8
%46 = phi i32 [ %9, %8 ], [ %44, %43 ], [ -1, %10 ], [ %23, %13 ], [ %26, %25 ]
ret i32 %46
}