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

/* Variables and functions */
int default_spi_write_256(struct flashrom_flashctx *,
                          int /*<<< orphan*/ 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_map_page(int, unsigned int);
int realtek_mst_i2c_spi_wait_command_done(int, int, int, int, int);
int realtek_mst_i2c_spi_write_page(int, int, int /*<<< orphan*/ 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_write_register(fd, 0x6D, 0x02);
  ret |= realtek_mst_i2c_spi_write_register(fd, 0x71, (128 - 1));
  for (i = 0; i < len; i += 128) {
    uint16_t page_len = min(len - i, 128);
    if (len - i < 128)
      ret |= realtek_mst_i2c_spi_write_register(fd, 0x71, page_len - 1);
    ret |= realtek_mst_i2c_spi_map_page(fd, start + i);
    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    $0x30,%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_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    1328 <realtek_mst_i2c_spi_write_256+0x1a8>
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_write_256+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1328 <realtek_mst_i2c_spi_write_256+0x1a8>
mov    -0x2c(%rbp),%edi
mov    $0x6d,%esi
mov    $0x2,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x71,%esi
mov    $0x7f,%edx
call   1070 <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    1322 <realtek_mst_i2c_spi_write_256+0x1a2>
mov    -0x20(%rbp),%edi
sub    -0x24(%rbp),%edi
mov    $0x80,%esi
call   1080 <min@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x24(%rbp),%eax
cmp    $0x80,%eax
jae    126b <realtek_mst_i2c_spi_write_256+0xeb>
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%edx
sub    $0x1,%edx
mov    $0x71,%esi
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
add    -0x24(%rbp),%esi
call   1090 <realtek_mst_i2c_spi_map_page@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     128e <realtek_mst_i2c_spi_write_256+0x10e>
jmp    1322 <realtek_mst_i2c_spi_write_256+0x1a2>
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     12bd <realtek_mst_i2c_spi_write_256+0x13d>
jmp    1322 <realtek_mst_i2c_spi_write_256+0x1a2>
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   10a0 <realtek_mst_i2c_spi_write_page@plt>
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12f0 <realtek_mst_i2c_spi_write_256+0x170>
jmp    1322 <realtek_mst_i2c_spi_write_256+0x1a2>
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     130d <realtek_mst_i2c_spi_write_256+0x18d>
jmp    1322 <realtek_mst_i2c_spi_write_256+0x1a2>
jmp    1312 <realtek_mst_i2c_spi_write_256+0x192>
mov    -0x24(%rbp),%eax
add    $0x80,%eax
mov    %eax,-0x24(%rbp)
jmp    1222 <realtek_mst_i2c_spi_write_256+0xa2>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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,%r13d
mov    %edx,%r12d
mov    %rsi,%r15
test   %r12b,%r12b
je     11b8 <realtek_mst_i2c_spi_write_256+0x38>
mov    %r15,%rsi
mov    %r12d,%edx
mov    %r13d,%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     12ac <realtek_mst_i2c_spi_write_256+0x12c>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x6d,%esi
mov    $0x2,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
mov    %eax,%r14d
mov    %ebp,%edi
mov    $0x71,%esi
mov    $0x7f,%edx
call   1070 <realtek_mst_i2c_spi_write_register@plt>
mov    %eax,%ebx
or     %r14d,%ebx
test   %r13d,%r13d
je     12b3 <realtek_mst_i2c_spi_write_256+0x133>
mov    %r15,0x10(%rsp)
xor    %r15d,%r15d
mov    %r13d,0xc(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    $0x80,%esi
call   1080 <min@plt>
mov    %eax,%r14d
sub    $0x80,%r13d
jae    123b <realtek_mst_i2c_spi_write_256+0xbb>
lea    -0x1(%r14),%edx
mov    %ebp,%edi
mov    $0x71,%esi
call   1070 <realtek_mst_i2c_spi_write_register@plt>
or     %eax,%ebx
lea    (%r12,%r15,1),%esi
mov    %ebp,%edi
call   1090 <realtek_mst_i2c_spi_map_page@plt>
or     %ebx,%eax
jne    12b5 <realtek_mst_i2c_spi_write_256+0x135>
mov    %ebp,%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    12b5 <realtek_mst_i2c_spi_write_256+0x135>
mov    %r15d,%eax
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,4),%rdx
mov    %ebp,%edi
mov    $0x70,%esi
mov    %r14d,%ecx
call   10a0 <realtek_mst_i2c_spi_write_page@plt>
test   %eax,%eax
jne    12b5 <realtek_mst_i2c_spi_write_256+0x135>
mov    %ebp,%edi
call   1040 <realtek_mst_i2c_execute_write@plt>
test   %eax,%eax
jne    12b5 <realtek_mst_i2c_spi_write_256+0x135>
sub    $0xffffff80,%r15d
xor    %ebx,%ebx
mov    $0x0,%eax
cmp    0xc(%rsp),%r15d
jb     1210 <realtek_mst_i2c_spi_write_256+0x90>
jmp    12b5 <realtek_mst_i2c_spi_write_256+0x135>
mov    $0xffffffff,%eax
jmp    12b5 <realtek_mst_i2c_spi_write_256+0x135>
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
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_write_256(%struct.flashrom_flashctx* %18, i32* %19, i32 %20, i32 %21)
store i32 %22, i32* %5
br label %104
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 %104
29: ; preds = %23
%30 = load i32, i32* %12
%31 = call i32 @realtek_mst_i2c_spi_write_register(i32 %30, i32 109, i32 2)
%32 = load i32, i32* %11
%33 = or i32 %32, %31
store i32 %33, i32* %11
%34 = load i32, i32* %12
%35 = call i32 @realtek_mst_i2c_spi_write_register(i32 %34, i32 113, i32 127)
%36 = load i32, i32* %11
%37 = or i32 %36, %35
store i32 %37, i32* %11
store i32 0, i32* %10
br label %38
38: ; preds = %99, %29
%39 = load i32, i32* %10
%40 = load i32, i32* %9
%41 = icmp ult i32 %39, %40
br i1 %41, label %42, label %102
42: ; preds = %38
%43 = load i32, i32* %9
%44 = load i32, i32* %10
%45 = sub i32 %43, %44
%46 = call i32 @min(i32 %45, i32 128)
store i32 %46, i32* %13
%47 = load i32, i32* %9
%48 = load i32, i32* %10
%49 = sub i32 %47, %48
%50 = icmp ult i32 %49, 128
br i1 %50, label %51, label %58
51: ; preds = %42
%52 = load i32, i32* %12
%53 = load i32, i32* %13
%54 = sub nsw i32 %53, 1
%55 = call i32 @realtek_mst_i2c_spi_write_register(i32 %52, i32 113, i32 %54)
%56 = load i32, i32* %11
%57 = or i32 %56, %55
store i32 %57, i32* %11
br label %58
58: ; preds = %51, %42
%59 = load i32, i32* %12
%60 = load i32, i32* %8
%61 = load i32, i32* %10
%62 = add i32 %60, %61
%63 = call i32 @realtek_mst_i2c_spi_map_page(i32 %59, i32 %62)
%64 = load i32, i32* %11
%65 = or i32 %64, %63
store i32 %65, i32* %11
%66 = load i32, i32* %11
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %58
br label %102
69: ; preds = %58
%70 = load i32, i32* %12
%71 = call i32 @realtek_mst_i2c_spi_wait_command_done(i32 %70, i32 111, i32 16, i32 16, i32 1)
%72 = load i32, i32* %11
%73 = or i32 %72, %71
store i32 %73, i32* %11
%74 = load i32, i32* %11
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %77
76: ; preds = %69
br label %102
77: ; preds = %69
%78 = load i32, i32* %12
%79 = load i32*, i32** %7
%80 = load i32, i32* %10
%81 = zext i32 %80 to i64
%82 = getelementptr inbounds i32, i32* %79, i64 %81
%83 = load i32, i32* %13
%84 = call i32 @realtek_mst_i2c_spi_write_page(i32 %78, i32 112, i32* %82, i32 %83)
%85 = load i32, i32* %11
%86 = or i32 %85, %84
store i32 %86, i32* %11
%87 = load i32, i32* %11
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %90
89: ; preds = %77
br label %102
90: ; preds = %77
%91 = load i32, i32* %12
%92 = call i32 @realtek_mst_i2c_execute_write(i32 %91)
%93 = load i32, i32* %11
%94 = or i32 %93, %92
store i32 %94, i32* %11
%95 = load i32, i32* %11
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %98
97: ; preds = %90
br label %102
98: ; preds = %90
br label %99
99: ; preds = %98
%100 = load i32, i32* %10
%101 = add i32 %100, 128
store i32 %101, i32* %10
br label %38
102: ; preds = %97, %89, %76, %68, %38
%103 = load i32, i32* %11
store i32 %103, i32* %5
br label %104
104: ; preds = %102, %28, %17
%105 = load i32, i32* %5
ret i32 %105
}

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