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

/* Variables and functions */
int default_spi_write_256(struct flashrom_flashctx *, unsigned int const *,
                          unsigned int, unsigned int);
int get_fd_from_context(struct flashrom_flashctx *);
int min(unsigned int, int);
int realtek_mst_i2c_execute_write(int);
int realtek_mst_i2c_spi_disable_protection(int);
int realtek_mst_i2c_spi_map_page(int, unsigned int, unsigned int,
                                 int /*<<< orphan*/);
int realtek_mst_i2c_spi_wait_command_done(int, int, int, int, int);
int realtek_mst_i2c_spi_write_page(int, int, unsigned int const *, int);
int realtek_mst_i2c_spi_write_register(int, int, int);
int realtek_mst_i2c_spi_write_256(struct flashrom_flashctx *flash,
                                  const uint8_t *buf, unsigned int start,
                                  unsigned int len) {
  unsigned i;
  int ret = 0;
  if (start & 0xff)
    return default_spi_write_256(flash, buf, start, len);
  int fd = get_fd_from_context(flash);
  if (fd < 0)
    return -1;
  ret = realtek_mst_i2c_spi_disable_protection(fd);
  if (ret)
    return ret;
  ret |= realtek_mst_i2c_spi_write_register(fd, 0x6D, 0x02);
  ret |= realtek_mst_i2c_spi_write_register(fd, 0x71, (256 - 1));
  for (i = 0; i < len; i += 256) {
    uint16_t page_len = min(len - i, 256);
    if (len - i < 256)
      ret |= realtek_mst_i2c_spi_write_register(fd, 0x71, page_len - 1);
    uint8_t block_idx = (start + i) >> 16;
    uint8_t page_idx = (start + i) >> 8;
    ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, 0);
    if (ret)
      break;
    ret |= realtek_mst_i2c_spi_wait_command_done(fd, 0x6F, 0x10, 0x10, 1);
    if (ret)
      break;
    ret |= realtek_mst_i2c_spi_write_page(fd, 0x70, buf + i, page_len);
    if (ret)
      break;
    ret |= realtek_mst_i2c_execute_write(fd);
    if (ret)
      break;
  }
  return ret;
}


x86 assembly (clang, -O0):
<realtek_mst_i2c_spi_write_256>:
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     11d9 <realtek_mst_i2c_spi_write_256+0x49>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1050 <default_spi_write_256@plt>
mov    %eax,-0x4(%rbp)
jmp    1372 <realtek_mst_i2c_spi_write_256+0x1e2>
mov    -0x10(%rbp),%rdi
call   1030 <get_fd_from_context@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11fb <realtek_mst_i2c_spi_write_256+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1372 <realtek_mst_i2c_spi_write_256+0x1e2>
mov    -0x2c(%rbp),%edi
call   1070 <realtek_mst_i2c_spi_disable_protection@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     121b <realtek_mst_i2c_spi_write_256+0x8b>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1372 <realtek_mst_i2c_spi_write_256+0x1e2>
mov    -0x2c(%rbp),%edi
mov    $0x6d,%esi
mov    $0x2,%edx
call   1080 <realtek_mst_i2c_spi_write_register@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x71,%esi
mov    $0xff,%edx
call   1080 <realtek_mst_i2c_spi_write_register@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    136c <realtek_mst_i2c_spi_write_256+0x1dc>
mov    -0x20(%rbp),%edi
sub    -0x24(%rbp),%edi
mov    $0x100,%esi
call   1090 <min@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x24(%rbp),%eax
cmp    $0x100,%eax
jae    129b <realtek_mst_i2c_spi_write_256+0x10b>
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%edx
sub    $0x1,%edx
mov    $0x71,%esi
call   1080 <realtek_mst_i2c_spi_write_register@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
shr    $0x10,%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    -0x38(%rbp),%edx
xor    %ecx,%ecx
call   10a0 <realtek_mst_i2c_spi_map_page@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12d8 <realtek_mst_i2c_spi_write_256+0x148>
jmp    136c <realtek_mst_i2c_spi_write_256+0x1dc>
mov    -0x2c(%rbp),%edi
mov    $0x6f,%esi
mov    $0x10,%ecx
mov    $0x1,%r8d
mov    %ecx,%edx
call   1060 <realtek_mst_i2c_spi_wait_command_done@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1307 <realtek_mst_i2c_spi_write_256+0x177>
jmp    136c <realtek_mst_i2c_spi_write_256+0x1dc>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%eax
shl    $0x2,%rax
add    %rax,%rdx
mov    -0x30(%rbp),%ecx
mov    $0x70,%esi
call   10b0 <realtek_mst_i2c_spi_write_page@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     133a <realtek_mst_i2c_spi_write_256+0x1aa>
jmp    136c <realtek_mst_i2c_spi_write_256+0x1dc>
mov    -0x2c(%rbp),%edi
call   1040 <realtek_mst_i2c_execute_write@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1357 <realtek_mst_i2c_spi_write_256+0x1c7>
jmp    136c <realtek_mst_i2c_spi_write_256+0x1dc>
jmp    135c <realtek_mst_i2c_spi_write_256+0x1cc>
mov    -0x24(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x24(%rbp)
jmp    1252 <realtek_mst_i2c_spi_write_256+0xc2>
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_write_256>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r13d
mov    %rsi,%rbx
test   %r13b,%r13b
je     11c8 <realtek_mst_i2c_spi_write_256+0x38>
mov    %rbx,%rsi
mov    %r13d,%edx
mov    %r14d,%ecx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <default_spi_write_256@plt>
call   1030 <get_fd_from_context@plt>
test   %eax,%eax
js     12e5 <realtek_mst_i2c_spi_write_256+0x155>
mov    %eax,%r15d
mov    %rbx,0x8(%rsp)
mov    %eax,%edi
call   1070 <realtek_mst_i2c_spi_disable_protection@plt>
test   %eax,%eax
jne    12ee <realtek_mst_i2c_spi_write_256+0x15e>
mov    %r15d,%edi
mov    $0x6d,%esi
mov    $0x2,%edx
call   1080 <realtek_mst_i2c_spi_write_register@plt>
mov    %eax,%r12d
mov    %r15d,%edi
mov    $0x71,%esi
mov    $0xff,%edx
call   1080 <realtek_mst_i2c_spi_write_register@plt>
mov    %eax,%ebx
or     %r12d,%ebx
test   %r14d,%r14d
je     12ec <realtek_mst_i2c_spi_write_256+0x15c>
xor    %r12d,%r12d
mov    %r14d,0x4(%rsp)
nopl   0x0(%rax)
mov    0x4(%rsp),%ebp
mov    %ebp,%edi
mov    $0x100,%esi
call   1090 <min@plt>
sub    $0x100,%ebp
mov    %ebp,0x4(%rsp)
mov    %rax,0x10(%rsp)
jae    1263 <realtek_mst_i2c_spi_write_256+0xd3>
lea    -0x1(%rax),%edx
mov    %r15d,%edi
mov    $0x71,%esi
call   1080 <realtek_mst_i2c_spi_write_register@plt>
or     %eax,%ebx
lea    (%r12,%r13,1),%edx
mov    %edx,%esi
shr    $0x10,%esi
shr    $0x8,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   10a0 <realtek_mst_i2c_spi_map_page@plt>
or     %ebx,%eax
jne    12ee <realtek_mst_i2c_spi_write_256+0x15e>
mov    %r15d,%edi
mov    $0x6f,%esi
mov    $0x10,%edx
mov    $0x10,%ecx
mov    $0x1,%r8d
call   1060 <realtek_mst_i2c_spi_wait_command_done@plt>
test   %eax,%eax
jne    12ee <realtek_mst_i2c_spi_write_256+0x15e>
mov    %r12d,%eax
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,4),%rdx
mov    %r15d,%edi
mov    $0x70,%esi
mov    0x10(%rsp),%rcx
call   10b0 <realtek_mst_i2c_spi_write_page@plt>
test   %eax,%eax
jne    12ee <realtek_mst_i2c_spi_write_256+0x15e>
mov    %r15d,%edi
call   1040 <realtek_mst_i2c_execute_write@plt>
test   %eax,%eax
jne    12ee <realtek_mst_i2c_spi_write_256+0x15e>
add    $0x100,%r12d
xor    %ebx,%ebx
mov    $0x0,%eax
cmp    %r14d,%r12d
jb     1230 <realtek_mst_i2c_spi_write_256+0xa0>
jmp    12ee <realtek_mst_i2c_spi_write_256+0x15e>
mov    $0xffffffff,%eax
jmp    12ee <realtek_mst_i2c_spi_write_256+0x15e>
mov    %ebx,%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 @realtek_mst_i2c_spi_write_256(%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
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
%16 = load i32, i32* %8
%17 = and i32 %16, 255
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %4
%20 = load %struct.flashrom_flashctx*, %struct.flashrom_flashctx** %6
%21 = load i32*, i32** %7
%22 = load i32, i32* %8
%23 = load i32, i32* %9
%24 = call i32 @default_spi_write_256(%struct.flashrom_flashctx* %20, i32* %21, i32 %22, i32 %23)
store i32 %24, i32* %5
br label %120
25: ; preds = %4
%26 = load %struct.flashrom_flashctx*, %struct.flashrom_flashctx** %6
%27 = call i32 @get_fd_from_context(%struct.flashrom_flashctx* %26)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 -1, i32* %5
br label %120
31: ; preds = %25
%32 = load i32, i32* %12
%33 = call i32 @realtek_mst_i2c_spi_disable_protection(i32 %32)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = load i32, i32* %11
store i32 %37, i32* %5
br label %120
38: ; preds = %31
%39 = load i32, i32* %12
%40 = call i32 @realtek_mst_i2c_spi_write_register(i32 %39, i32 109, i32 2)
%41 = load i32, i32* %11
%42 = or i32 %41, %40
store i32 %42, i32* %11
%43 = load i32, i32* %12
%44 = call i32 @realtek_mst_i2c_spi_write_register(i32 %43, i32 113, i32 255)
%45 = load i32, i32* %11
%46 = or i32 %45, %44
store i32 %46, i32* %11
store i32 0, i32* %10
br label %47
47: ; preds = %115, %38
%48 = load i32, i32* %10
%49 = load i32, i32* %9
%50 = icmp ult i32 %48, %49
br i1 %50, label %51, label %118
51: ; preds = %47
%52 = load i32, i32* %9
%53 = load i32, i32* %10
%54 = sub i32 %52, %53
%55 = call i32 @min(i32 %54, i32 256)
store i32 %55, i32* %13
%56 = load i32, i32* %9
%57 = load i32, i32* %10
%58 = sub i32 %56, %57
%59 = icmp ult i32 %58, 256
br i1 %59, label %60, label %67
60: ; preds = %51
%61 = load i32, i32* %12
%62 = load i32, i32* %13
%63 = sub nsw i32 %62, 1
%64 = call i32 @realtek_mst_i2c_spi_write_register(i32 %61, i32 113, i32 %63)
%65 = load i32, i32* %11
%66 = or i32 %65, %64
store i32 %66, i32* %11
br label %67
67: ; preds = %60, %51
%68 = load i32, i32* %8
%69 = load i32, i32* %10
%70 = add i32 %68, %69
%71 = lshr i32 %70, 16
store i32 %71, i32* %14
%72 = load i32, i32* %8
%73 = load i32, i32* %10
%74 = add i32 %72, %73
%75 = lshr i32 %74, 8
store i32 %75, i32* %15
%76 = load i32, i32* %12
%77 = load i32, i32* %14
%78 = load i32, i32* %15
%79 = call i32 @realtek_mst_i2c_spi_map_page(i32 %76, i32 %77, i32 %78, i32 0)
%80 = load i32, i32* %11
%81 = or i32 %80, %79
store i32 %81, i32* %11
%82 = load i32, i32* %11
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %67
br label %118
85: ; preds = %67
%86 = load i32, i32* %12
%87 = call i32 @realtek_mst_i2c_spi_wait_command_done(i32 %86, i32 111, i32 16, i32 16, i32 1)
%88 = load i32, i32* %11
%89 = or i32 %88, %87
store i32 %89, i32* %11
%90 = load i32, i32* %11
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %93
92: ; preds = %85
br label %118
93: ; preds = %85
%94 = load i32, i32* %12
%95 = load i32*, i32** %7
%96 = load i32, i32* %10
%97 = zext i32 %96 to i64
%98 = getelementptr inbounds i32, i32* %95, i64 %97
%99 = load i32, i32* %13
%100 = call i32 @realtek_mst_i2c_spi_write_page(i32 %94, i32 112, i32* %98, i32 %99)
%101 = load i32, i32* %11
%102 = or i32 %101, %100
store i32 %102, i32* %11
%103 = load i32, i32* %11
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %106
105: ; preds = %93
br label %118
106: ; preds = %93
%107 = load i32, i32* %12
%108 = call i32 @realtek_mst_i2c_execute_write(i32 %107)
%109 = load i32, i32* %11
%110 = or i32 %109, %108
store i32 %110, i32* %11
%111 = load i32, i32* %11
%112 = icmp ne i32 %111, 0
br i1 %112, label %113, label %114
113: ; preds = %106
br label %118
114: ; preds = %106
br label %115
115: ; preds = %114
%116 = load i32, i32* %10
%117 = add i32 %116, 256
store i32 %117, i32* %10
br label %47
118: ; preds = %113, %105, %92, %84, %47
%119 = load i32, i32* %11
store i32 %119, i32* %5
br label %120
120: ; preds = %118, %36, %30, %19
%121 = load i32, i32* %5
ret i32 %121
}

LLVM IR (-O2):
%struct.flashrom_flashctx = type { i32 }
define i32 @realtek_mst_i2c_spi_write_256(%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_write_256(%struct.flashrom_flashctx* %0, i32* %1, i32 %2, i32 %3)
br label %52
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 %52, label %12
12: ; preds = %9
%13 = tail call i32 @realtek_mst_i2c_spi_disable_protection(i32 %10)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %52
15: ; preds = %12
%16 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %10, i32 109, i32 2)
%17 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %10, i32 113, i32 255)
%18 = or i32 %17, %16
%19 = icmp eq i32 %3, 0
br i1 %19, label %52, label %22
20: ; preds = %48
%21 = icmp ult i32 %51, %3
br i1 %21, label %22, label %52
22: ; preds = %15, %20
%23 = phi i32 [ %51, %20 ], [ 0, %15 ]
%24 = phi i32 [ 0, %20 ], [ %18, %15 ]
%25 = sub i32 %3, %23
%26 = tail call i32 @min(i32 %25, i32 256)
%27 = icmp ult i32 %25, 256
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = add nsw i32 %26, -1
%30 = tail call i32 @realtek_mst_i2c_spi_write_register(i32 %10, i32 113, i32 %29)
%31 = or i32 %30, %24
br label %32
32: ; preds = %28, %22
%33 = phi i32 [ %31, %28 ], [ %24, %22 ]
%34 = add i32 %23, %2
%35 = lshr i32 %34, 16
%36 = lshr i32 %34, 8
%37 = tail call i32 @realtek_mst_i2c_spi_map_page(i32 %10, i32 %35, i32 %36, i32 0)
%38 = or i32 %37, %33
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %52
40: ; preds = %32
%41 = tail call i32 @realtek_mst_i2c_spi_wait_command_done(i32 %10, i32 111, i32 16, i32 16, i32 1)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %52
43: ; preds = %40
%44 = zext i32 %23 to i64
%45 = getelementptr inbounds i32, i32* %1, i64 %44
%46 = tail call i32 @realtek_mst_i2c_spi_write_page(i32 %10, i32 112, i32* %45, i32 %26)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = tail call i32 @realtek_mst_i2c_execute_write(i32 %10)
%50 = icmp eq i32 %49, 0
%51 = add i32 %23, 256
br i1 %50, label %20, label %52
52: ; preds = %20, %32, %40, %43, %48, %15, %9, %12, %7
%53 = phi i32 [ %8, %7 ], [ -1, %9 ], [ %13, %12 ], [ %18, %15 ], [ 0, %20 ], [ %38, %32 ], [ %41, %40 ], [ %46, %43 ], [ %49, %48 ]
ret i32 %53
}