#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_write_256(struct flashrom_flashctx *,
                          int /*<<< orphan*/ const *, unsigned int,
                          unsigned int);
int get_fd_from_context(struct flashrom_flashctx *);
int lspcon_i2c_clt2_spi_reset(int);
int lspcon_i2c_spi_disable_all_protection(int);
int lspcon_i2c_spi_disable_hw_write(int);
int lspcon_i2c_spi_enable_hw_write(int);
int lspcon_i2c_spi_enable_write_protection(int);
int lspcon_i2c_spi_map_page(int, unsigned int);
int lspcon_i2c_spi_write_page(int, int /*<<< orphan*/ const *,
                              int /*<<< orphan*/);
int /*<<< orphan*/ min(unsigned int, int);
int lspcon_i2c_spi_write_256(struct flashrom_flashctx *flash,
                             const uint8_t *buf, unsigned int start,
                             unsigned int len) {
  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 |= lspcon_i2c_spi_disable_all_protection(fd);
  ret |= lspcon_i2c_spi_enable_hw_write(fd);
  ret |= lspcon_i2c_clt2_spi_reset(fd);
  for (unsigned int i = 0; i < len; i += 256) {
    ret |= lspcon_i2c_spi_map_page(fd, start + i);
    ret |= lspcon_i2c_spi_write_page(fd, buf + i, min(len - i, 256));
  }
  ret |= lspcon_i2c_spi_enable_write_protection(fd);
  ret |= lspcon_i2c_spi_disable_hw_write(fd);
  return ret;
}


x86 assembly (clang, -O0):
<lspcon_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,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
cmp    $0x0,%eax
je     11e9 <lspcon_i2c_spi_write_256+0x49>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1040 <default_spi_write_256@plt>
mov    %eax,-0x4(%rbp)
jmp    12ca <lspcon_i2c_spi_write_256+0x12a>
mov    -0x10(%rbp),%rdi
call   1030 <get_fd_from_context@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    120b <lspcon_i2c_spi_write_256+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <lspcon_i2c_spi_write_256+0x12a>
mov    -0x28(%rbp),%edi
call   1080 <lspcon_i2c_spi_disable_all_protection@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <lspcon_i2c_spi_enable_hw_write@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   10a0 <lspcon_i2c_clt2_spi_reset@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    12a8 <lspcon_i2c_spi_write_256+0x108>
mov    -0x28(%rbp),%edi
mov    -0x1c(%rbp),%esi
add    -0x2c(%rbp),%esi
call   1070 <lspcon_i2c_spi_map_page@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
sub    -0x2c(%rbp),%edi
mov    $0x100,%esi
call   1060 <min@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   10b0 <lspcon_i2c_spi_write_page@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x2c(%rbp)
jmp    123c <lspcon_i2c_spi_write_256+0x9c>
mov    -0x28(%rbp),%edi
call   10c0 <lspcon_i2c_spi_enable_write_protection@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <lspcon_i2c_spi_disable_hw_write@plt>
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lspcon_i2c_spi_write_256>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r12d
mov    %rsi,%rbp
test   %r12b,%r12b
je     11d8 <lspcon_i2c_spi_write_256+0x38>
mov    %rbp,%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_write_256@plt>
call   1030 <get_fd_from_context@plt>
test   %eax,%eax
js     12a5 <lspcon_i2c_spi_write_256+0x105>
mov    %eax,%ebx
mov    %eax,%edi
call   1080 <lspcon_i2c_spi_disable_all_protection@plt>
mov    %eax,%r13d
mov    %ebx,%edi
call   1090 <lspcon_i2c_spi_enable_hw_write@plt>
mov    %eax,%r15d
or     %r13d,%r15d
mov    %ebx,%r13d
mov    %ebx,%edi
call   10a0 <lspcon_i2c_clt2_spi_reset@plt>
mov    %eax,%ebx
or     %r15d,%ebx
test   %r14d,%r14d
mov    %r13d,0x4(%rsp)
je     1289 <lspcon_i2c_spi_write_256+0xe9>
mov    %rbp,0x8(%rsp)
xor    %r15d,%r15d
mov    %r14d,%ebp
mov    %r12,0x10(%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     %ebx,%r14d
mov    %r15d,%eax
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,4),%rbx
mov    %ebp,%edi
mov    $0x100,%esi
call   1060 <min@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   10b0 <lspcon_i2c_spi_write_page@plt>
mov    %eax,%ebx
or     %r14d,%ebx
mov    %r12d,%r14d
mov    0x10(%rsp),%r12
add    $0x100,%r15d
add    $0xffffff00,%ebp
cmp    %r14d,%r15d
jb     1230 <lspcon_i2c_spi_write_256+0x90>
mov    0x4(%rsp),%ebp
mov    %ebp,%edi
call   10c0 <lspcon_i2c_spi_enable_write_protection@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1050 <lspcon_i2c_spi_disable_hw_write@plt>
or     %ebx,%eax
or     %r14d,%eax
jmp    12aa <lspcon_i2c_spi_write_256+0x10a>
mov    $0xffffffff,%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_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
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* %10
%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_write_256(%struct.flashrom_flashctx* %17, i32* %18, i32 %19, i32 %20)
store i32 %21, i32* %5
br label %78
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* %11
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 -1, i32* %5
br label %78
28: ; preds = %22
%29 = load i32, i32* %11
%30 = call i32 @lspcon_i2c_spi_disable_all_protection(i32 %29)
%31 = load i32, i32* %10
%32 = or i32 %31, %30
store i32 %32, i32* %10
%33 = load i32, i32* %11
%34 = call i32 @lspcon_i2c_spi_enable_hw_write(i32 %33)
%35 = load i32, i32* %10
%36 = or i32 %35, %34
store i32 %36, i32* %10
%37 = load i32, i32* %11
%38 = call i32 @lspcon_i2c_clt2_spi_reset(i32 %37)
%39 = load i32, i32* %10
%40 = or i32 %39, %38
store i32 %40, i32* %10
store i32 0, i32* %12
br label %41
41: ; preds = %65, %28
%42 = load i32, i32* %12
%43 = load i32, i32* %9
%44 = icmp ult i32 %42, %43
br i1 %44, label %45, label %68
45: ; preds = %41
%46 = load i32, i32* %11
%47 = load i32, i32* %8
%48 = load i32, i32* %12
%49 = add i32 %47, %48
%50 = call i32 @lspcon_i2c_spi_map_page(i32 %46, i32 %49)
%51 = load i32, i32* %10
%52 = or i32 %51, %50
store i32 %52, i32* %10
%53 = load i32, i32* %11
%54 = load i32*, i32** %7
%55 = load i32, i32* %12
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
%58 = load i32, i32* %9
%59 = load i32, i32* %12
%60 = sub i32 %58, %59
%61 = call i32 @min(i32 %60, i32 256)
%62 = call i32 @lspcon_i2c_spi_write_page(i32 %53, i32* %57, i32 %61)
%63 = load i32, i32* %10
%64 = or i32 %63, %62
store i32 %64, i32* %10
br label %65
65: ; preds = %45
%66 = load i32, i32* %12
%67 = add i32 %66, 256
store i32 %67, i32* %12
br label %41
68: ; preds = %41
%69 = load i32, i32* %11
%70 = call i32 @lspcon_i2c_spi_enable_write_protection(i32 %69)
%71 = load i32, i32* %10
%72 = or i32 %71, %70
store i32 %72, i32* %10
%73 = load i32, i32* %11
%74 = call i32 @lspcon_i2c_spi_disable_hw_write(i32 %73)
%75 = load i32, i32* %10
%76 = or i32 %75, %74
store i32 %76, i32* %10
%77 = load i32, i32* %10
store i32 %77, i32* %5
br label %78
78: ; preds = %68, %27, %16
%79 = load i32, i32* %5
ret i32 %79
}

LLVM IR (-O2):
%struct.flashrom_flashctx = type { i32 }
define i32 @lspcon_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 %39
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 %39, label %12
12: ; preds = %9
%13 = tail call i32 @lspcon_i2c_spi_disable_all_protection(i32 %10)
%14 = tail call i32 @lspcon_i2c_spi_enable_hw_write(i32 %10)
%15 = or i32 %14, %13
%16 = tail call i32 @lspcon_i2c_clt2_spi_reset(i32 %10)
%17 = or i32 %15, %16
%18 = icmp eq i32 %3, 0
br i1 %18, label %19, label %25
19: ; preds = %25, %12
%20 = phi i32 [ %17, %12 ], [ %36, %25 ]
%21 = tail call i32 @lspcon_i2c_spi_enable_write_protection(i32 %10)
%22 = or i32 %21, %20
%23 = tail call i32 @lspcon_i2c_spi_disable_hw_write(i32 %10)
%24 = or i32 %22, %23
br label %39
25: ; preds = %12, %25
%26 = phi i32 [ %37, %25 ], [ 0, %12 ]
%27 = phi i32 [ %36, %25 ], [ %17, %12 ]
%28 = add i32 %26, %2
%29 = tail call i32 @lspcon_i2c_spi_map_page(i32 %10, i32 %28)
%30 = or i32 %29, %27
%31 = zext i32 %26 to i64
%32 = getelementptr inbounds i32, i32* %1, i64 %31
%33 = sub i32 %3, %26
%34 = tail call i32 @min(i32 %33, i32 256)
%35 = tail call i32 @lspcon_i2c_spi_write_page(i32 %10, i32* %32, i32 %34)
%36 = or i32 %30, %35
%37 = add i32 %26, 256
%38 = icmp ult i32 %37, %3
br i1 %38, label %25, label %19
39: ; preds = %19, %9, %7
%40 = phi i32 [ %8, %7 ], [ %24, %19 ], [ -1, %9 ]
ret i32 %40
}