#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 scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ lseek(int, unsigned long, int /*<<< orphan*/);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
unsigned long write(int, unsigned char const *, unsigned long);
void flash_write(int fd, unsigned long off, unsigned long len,
                 const unsigned char *buf) {
  unsigned long blen = 2048;
  if (lseek(fd, off, SEEK_SET) == (off_t)-1)
    panic("error setting offset\n");
  while (len != 0) {
    if (blen > len)
      blen = len;
    printf("writing  : %08lX .. %08lX\r", off, off + blen - 1);
    off += blen;
    fflush(stdout);
    if (write(fd, buf, blen) != blen)
      panic("\nerror writing to flash\n");
    buf += blen;
    len -= blen;
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<flash_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x800,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    119a <flash_write+0x4a>
lea    0xe6b(%rip),%rdi
call   1060 <panic@plt>
jmp    119f <flash_write+0x4f>
cmpq   $0x0,-0x18(%rbp)
je     123e <flash_write+0xee>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11c0 <flash_write+0x70>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
add    -0x28(%rbp),%rdx
sub    $0x1,%rdx
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x28(%rbp),%rax
je     121e <flash_write+0xce>
lea    0xe18(%rip),%rdi
call   1060 <panic@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    119f <flash_write+0x4f>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r12d
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     1195 <flash_write+0x35>
test   %r15,%r15
jne    11a6 <flash_write+0x46>
jmp    1215 <flash_write+0xb5>
lea    0xe64(%rip),%rdi
call   1070 <panic@plt>
test   %r15,%r15
je     1215 <flash_write+0xb5>
mov    $0x800,%ebp
mov    0x2e26(%rip),%r13
jmp    11cb <flash_write+0x6b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %rbp,%rbx
add    %rbp,%r14
sub    %rbp,%r15
je     1215 <flash_write+0xb5>
cmp    %r15,%rbp
cmova  %r15,%rbp
lea    (%rbx,%rbp,1),%rdx
add    $0xffffffffffffffff,%rdx
lea    0xe35(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%r13),%edi
call   1080 <fflush@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1040 <write@plt>
cmp    %rbp,%rax
je     11c0 <flash_write+0x60>
lea    0xe23(%rip),%rdi
call   1070 <panic@plt>
jmp    11c0 <flash_write+0x60>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [22 x i8] c"error setting offset\0A\00"
@.str.1 = [27 x i8] c"writing : %08lX .. %08lX\0D\00"
@stdout = global i32 0
@.str.2 = [25 x i8] c"\0Aerror writing to flash\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @flash_write(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i8* %3, i8** %8
store i64 2048, i64* %9
%10 = load i32, i32* %5
%11 = load i64, i64* %6
%12 = load i32, i32* @SEEK_SET
%13 = call i64 @lseek(i32 %10, i64 %11, i32 %12)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %4
br label %18
18: ; preds = %47, %17
%19 = load i64, i64* %7
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %54
21: ; preds = %18
%22 = load i64, i64* %9
%23 = load i64, i64* %7
%24 = icmp ugt i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i64, i64* %7
store i64 %26, i64* %9
br label %27
27: ; preds = %25, %21
%28 = load i64, i64* %6
%29 = load i64, i64* %6
%30 = load i64, i64* %9
%31 = add i64 %29, %30
%32 = sub i64 %31, 1
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %28, i64 %32)
%34 = load i64, i64* %9
%35 = load i64, i64* %6
%36 = add i64 %35, %34
store i64 %36, i64* %6
%37 = load i32, i32* @stdout
%38 = call i32 @fflush(i32 %37)
%39 = load i32, i32* %5
%40 = load i8*, i8** %8
%41 = load i64, i64* %9
%42 = call i64 @write(i32 %39, i8* %40, i64 %41)
%43 = load i64, i64* %9
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %27
%46 = call i32 @panic(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %45, %27
%48 = load i64, i64* %9
%49 = load i8*, i8** %8
%50 = getelementptr inbounds i8, i8* %49, i64 %48
store i8* %50, i8** %8
%51 = load i64, i64* %9
%52 = load i64, i64* %7
%53 = sub i64 %52, %51
store i64 %53, i64* %7
br label %18
54: ; preds = %18
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [22 x i8] c"error setting offset\0A\00"
@.str.1 = [27 x i8] c"writing : %08lX .. %08lX\0D\00"
@stdout = global i32 0
@.str.2 = [25 x i8] c"\0Aerror writing to flash\0A\00"
define void @flash_write(i32 %0, i64 %1, i64 %2, i8* nocapture %3) {
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i64 %1, i32 %5)
%7 = icmp eq i64 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %4
%11 = icmp eq i64 %2, 0
br i1 %11, label %32, label %12
12: ; preds = %10, %28
%13 = phi i64 [ %18, %28 ], [ 2048, %10 ]
%14 = phi i8* [ %29, %28 ], [ %3, %10 ]
%15 = phi i64 [ %30, %28 ], [ %2, %10 ]
%16 = phi i64 [ %19, %28 ], [ %1, %10 ]
%17 = icmp ugt i64 %13, %15
%18 = select i1 %17, i64 %15, i64 %13
%19 = add i64 %18, %16
%20 = add i64 %19, -1
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %16, i64 %20)
%22 = load i32, i32* @stdout
%23 = tail call i32 @fflush(i32 %22)
%24 = tail call i64 @write(i32 %0, i8* %14, i64 %18)
%25 = icmp eq i64 %24, %18
br i1 %25, label %28, label %26
26: ; preds = %12
%27 = tail call i32 @panic(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %12
%29 = getelementptr inbounds i8, i8* %14, i64 %18
%30 = sub i64 %15, %18
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %12
32: ; preds = %28, %10
%33 = tail call i32 @putchar(i32 10)
ret void
}