#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 */
int ee24c02_write_page(int, unsigned char, unsigned char *, int);
int /*<<< orphan*/ usleep(int);
int ee24c02_write(int file, unsigned char address, unsigned char *data,
                  int len) {
  int ret = 0, nbytes;
  if (len + address > 256)
    len = 256 - address;
  while (len > 0) {
    nbytes = 8 - (address % 8);
    nbytes = (len < nbytes ? len : nbytes);
    if (ee24c02_write_page(file, address, data, nbytes) != nbytes)
      return -1;
    usleep(10000);
    len -= nbytes;
    address += nbytes;
    data += nbytes;
  }
  return len;
}


x86 assembly (clang, -O0):
<ee24c02_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
movzbl -0x9(%rbp),%ecx
add    %ecx,%eax
cmp    $0x100,%eax
jle    1161 <ee24c02_write+0x41>
movzbl -0x9(%rbp),%ecx
mov    $0x100,%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <ee24c02_write+0x46>
cmpl   $0x0,-0x1c(%rbp)
jle    120a <ee24c02_write+0xea>
movzbl -0x9(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    $0x8,%eax
sub    %edx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    119d <ee24c02_write+0x7d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11a3 <ee24c02_write+0x83>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x9(%rbp),%al
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
movzbl %al,%esi
call   1030 <ee24c02_write_page@plt>
cmp    -0x24(%rbp),%eax
je     11d3 <ee24c02_write+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <ee24c02_write+0xf0>
mov    $0x2710,%edi
call   1040 <usleep@plt>
mov    -0x24(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%ecx
movzbl -0x9(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x9(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1166 <ee24c02_write+0x46>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ee24c02_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
lea    (%r15,%rcx,1),%eax
mov    $0x100,%ebx
sub    %esi,%ebx
cmp    $0x101,%eax
cmovl  %ecx,%ebx
test   %ebx,%ebx
jle    1193 <ee24c02_write+0x73>
mov    %rdx,%r14
mov    %edi,%r12d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%eax
and    $0x7,%al
mov    $0x8,%cl
sub    %al,%cl
movzbl %cl,%ebp
cmp    %ebp,%ebx
cmovb  %ebx,%ebp
movzbl %r15b,%esi
mov    %r12d,%edi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1030 <ee24c02_write_page@plt>
cmp    %ebp,%eax
jne    118e <ee24c02_write+0x6e>
mov    $0x2710,%edi
call   1040 <usleep@plt>
add    %bpl,%r15b
mov    %ebp,%eax
add    %rax,%r14
sub    %ebp,%ebx
jg     1150 <ee24c02_write+0x30>
jmp    1193 <ee24c02_write+0x73>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ee24c02_write(i32 %0, i8 zeroext %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8 %1, i8* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* %9
%13 = load i8, i8* %7
%14 = zext i8 %13 to i32
%15 = add nsw i32 %12, %14
%16 = icmp sgt i32 %15, 256
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i8, i8* %7
%19 = zext i8 %18 to i32
%20 = sub nsw i32 256, %19
store i32 %20, i32* %9
br label %21
21: ; preds = %17, %4
br label %22
22: ; preds = %47, %21
%23 = load i32, i32* %9
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %61
25: ; preds = %22
%26 = load i8, i8* %7
%27 = zext i8 %26 to i32
%28 = srem i32 %27, 8
%29 = sub nsw i32 8, %28
store i32 %29, i32* %11
%30 = load i32, i32* %9
%31 = load i32, i32* %11
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = load i32, i32* %9
br label %37
35: ; preds = %25
%36 = load i32, i32* %11
br label %37
37: ; preds = %35, %33
%38 = phi i32 [ %34, %33 ], [ %36, %35 ]
store i32 %38, i32* %11
%39 = load i32, i32* %6
%40 = load i8, i8* %7
%41 = load i8*, i8** %8
%42 = load i32, i32* %11
%43 = call i32 @ee24c02_write_page(i32 %39, i8 zeroext %40, i8* %41, i32 %42)
%44 = load i32, i32* %11
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %37
store i32 -1, i32* %5
br label %63
47: ; preds = %37
%48 = call i32 @usleep(i32 10000)
%49 = load i32, i32* %11
%50 = load i32, i32* %9
%51 = sub nsw i32 %50, %49
store i32 %51, i32* %9
%52 = load i32, i32* %11
%53 = load i8, i8* %7
%54 = zext i8 %53 to i32
%55 = add nsw i32 %54, %52
%56 = trunc i32 %55 to i8
store i8 %56, i8* %7
%57 = load i32, i32* %11
%58 = load i8*, i8** %8
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i8, i8* %58, i64 %59
store i8* %60, i8** %8
br label %22
61: ; preds = %22
%62 = load i32, i32* %9
store i32 %62, i32* %5
br label %63
63: ; preds = %61, %46
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
define i32 @ee24c02_write(i32 %0, i8 zeroext %1, i8* %2, i32 %3) {
%5 = zext i8 %1 to i32
%6 = add nsw i32 %5, %3
%7 = icmp sgt i32 %6, 256
%8 = sub nuw nsw i32 256, %5
%9 = select i1 %7, i32 %8, i32 %3
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %4, %22
%12 = phi i32 [ %24, %22 ], [ %9, %4 ]
%13 = phi i8* [ %28, %22 ], [ %2, %4 ]
%14 = phi i8 [ %26, %22 ], [ %1, %4 ]
%15 = and i8 %14, 7
%16 = sub nuw nsw i8 8, %15
%17 = zext i8 %16 to i32
%18 = icmp ult i32 %12, %17
%19 = select i1 %18, i32 %12, i32 %17
%20 = tail call i32 @ee24c02_write_page(i32 %0, i8 zeroext %14, i8* %13, i32 %19)
%21 = icmp eq i32 %20, %19
br i1 %21, label %22, label %30
22: ; preds = %11
%23 = tail call i32 @usleep(i32 10000)
%24 = sub nsw i32 %12, %19
%25 = trunc i32 %19 to i8
%26 = add i8 %14, %25
%27 = zext i32 %19 to i64
%28 = getelementptr inbounds i8, i8* %13, i64 %27
%29 = icmp sgt i32 %24, 0
br i1 %29, label %11, label %30
30: ; preds = %11, %22, %4
%31 = phi i32 [ %9, %4 ], [ %24, %22 ], [ -1, %11 ]
ret i32 %31
}