#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 */

/* Variables and functions */
unsigned char nvmem_write(unsigned long, unsigned long, unsigned short,
                          unsigned char *);
unsigned char nvmem_write_patch(unsigned long ulFileId, unsigned long spLength,
                                const unsigned char *spData) {
  unsigned char status = 0;
  unsigned short offset = 0;
  unsigned char *spDataPtr = (unsigned char *)spData;
  while ((status == 0) && (spLength >= 512)) {
    status = nvmem_write(ulFileId, 512, offset, spDataPtr);
    offset += 512;
    spLength -= 512;
    spDataPtr += 512;
  }
  if (status != 0) {
    return status;
  }
  if (spLength != 0) {
    status = nvmem_write(ulFileId, spLength, offset, spDataPtr);
  }
  return status;
}


x86 assembly (clang, -O0):
<nvmem_write_patch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
movw   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movzbl -0x21(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
jne    1156 <nvmem_write_patch+0x46>
cmpq   $0x200,-0x18(%rbp)
setae  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1166 <nvmem_write_patch+0x56>
jmp    11b0 <nvmem_write_patch+0xa0>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%ax
mov    -0x30(%rbp),%rcx
mov    $0x200,%esi
movzwl %ax,%edx
call   1030 <nvmem_write@plt>
mov    %al,-0x21(%rbp)
movzwl -0x24(%rbp),%eax
add    $0x200,%eax
mov    %ax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x200,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x30(%rbp)
jmp    1136 <nvmem_write_patch+0x26>
movzbl -0x21(%rbp),%eax
cmp    $0x0,%eax
je     11c8 <nvmem_write_patch+0xb8>
mov    -0x21(%rbp),%al
mov    %al,-0x1(%rbp)
jmp    11f4 <nvmem_write_patch+0xe4>
cmpq   $0x0,-0x18(%rbp)
je     11ee <nvmem_write_patch+0xde>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%ax
mov    -0x30(%rbp),%rcx
movzwl %ax,%edx
call   1030 <nvmem_write@plt>
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
mov    %al,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nvmem_write_patch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
cmp    $0x200,%rsi
jb     1168 <nvmem_write_patch+0x58>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzwl %bp,%ebp
mov    $0x200,%esi
mov    %r15,%rdi
mov    %ebp,%edx
mov    %r14,%rcx
call   1030 <nvmem_write@plt>
add    $0x200,%ebp
add    $0xfffffffffffffe00,%rbx
add    $0x200,%r14
test   %al,%al
jne    116c <nvmem_write_patch+0x5c>
cmp    $0x1ff,%rbx
ja     1130 <nvmem_write_patch+0x20>
jmp    116c <nvmem_write_patch+0x5c>
xor    %eax,%eax
xor    %ebp,%ebp
test   %al,%al
jne    1190 <nvmem_write_patch+0x80>
test   %rbx,%rbx
je     1190 <nvmem_write_patch+0x80>
movzwl %bp,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <nvmem_write@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @nvmem_write_patch(i64 %0, i64 %1, i8* %2) {
%4 = alloca i8
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i16
%10 = alloca i8*
store i64 %0, i64* %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i8 0, i8* %8
store i16 0, i16* %9
%11 = load i8*, i8** %7
store i8* %11, i8** %10
br label %12
12: ; preds = %21, %3
%13 = load i8, i8* %8
%14 = zext i8 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i64, i64* %6
%18 = icmp uge i64 %17, 512
br label %19
19: ; preds = %16, %12
%20 = phi i1 [ false, %12 ], [ %18, %16 ]
br i1 %20, label %21, label %34
21: ; preds = %19
%22 = load i64, i64* %5
%23 = load i16, i16* %9
%24 = load i8*, i8** %10
%25 = call zeroext i8 @nvmem_write(i64 %22, i64 512, i16 zeroext %23, i8* %24)
store i8 %25, i8* %8
%26 = load i16, i16* %9
%27 = zext i16 %26 to i32
%28 = add nsw i32 %27, 512
%29 = trunc i32 %28 to i16
store i16 %29, i16* %9
%30 = load i64, i64* %6
%31 = sub i64 %30, 512
store i64 %31, i64* %6
%32 = load i8*, i8** %10
%33 = getelementptr inbounds i8, i8* %32, i64 512
store i8* %33, i8** %10
br label %12
34: ; preds = %19
%35 = load i8, i8* %8
%36 = zext i8 %35 to i32
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i8, i8* %8
store i8 %39, i8* %4
br label %51
40: ; preds = %34
%41 = load i64, i64* %6
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load i64, i64* %5
%45 = load i64, i64* %6
%46 = load i16, i16* %9
%47 = load i8*, i8** %10
%48 = call zeroext i8 @nvmem_write(i64 %44, i64 %45, i16 zeroext %46, i8* %47)
store i8 %48, i8* %8
br label %49
49: ; preds = %43, %40
%50 = load i8, i8* %8
store i8 %50, i8* %4
br label %51
51: ; preds = %49, %38
%52 = load i8, i8* %4
ret i8 %52
}

LLVM IR (-O2):
define zeroext i8 @nvmem_write_patch(i64 %0, i64 %1, i8* %2) {
%4 = icmp ugt i64 %1, 511
br i1 %4, label %5, label %16
5: ; preds = %3, %5
%6 = phi i8* [ %12, %5 ], [ %2, %3 ]
%7 = phi i16 [ %10, %5 ], [ 0, %3 ]
%8 = phi i64 [ %11, %5 ], [ %1, %3 ]
%9 = tail call zeroext i8 @nvmem_write(i64 %0, i64 512, i16 zeroext %7, i8* %6)
%10 = add i16 %7, 512
%11 = add i64 %8, -512
%12 = getelementptr inbounds i8, i8* %6, i64 512
%13 = icmp eq i8 %9, 0
%14 = icmp ugt i64 %11, 511
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %5, label %16
16: ; preds = %5, %3
%17 = phi i64 [ %1, %3 ], [ %11, %5 ]
%18 = phi i8 [ 0, %3 ], [ %9, %5 ]
%19 = phi i16 [ 0, %3 ], [ %10, %5 ]
%20 = phi i8* [ %2, %3 ], [ %12, %5 ]
%21 = icmp ne i8 %18, 0
%22 = icmp eq i64 %17, 0
%23 = select i1 %21, i1 true, i1 %22
br i1 %23, label %26, label %24
24: ; preds = %16
%25 = tail call zeroext i8 @nvmem_write(i64 %0, i64 %17, i16 zeroext %19, i8* %20)
br label %26
26: ; preds = %24, %16
%27 = phi i8 [ %18, %16 ], [ %25, %24 ]
ret i8 %27
}