#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 /*<<< orphan*/ ata_read(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ ata_write(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int disk_write(char *source, int start_block_index, int offset, int num_bytes) {
  int blocks_needed = ((offset + num_bytes) / 512);
  if (((offset + num_bytes) % 512) != 0) {
    blocks_needed++;
  }
  char block_buffer[512];
  int blocks_read_in;
  int memcpy_offset;
  int bytes_written = 0;
  int bytes_to_write;
  for (blocks_read_in = 0; blocks_read_in < blocks_needed; blocks_read_in++) {
    ata_read(0, block_buffer, 1, start_block_index);
    memcpy_offset = blocks_read_in == 0 ? offset : 0;
    bytes_to_write = ((num_bytes - bytes_written) + memcpy_offset) > 512
                         ? 512 - memcpy_offset
                         : num_bytes - bytes_written;
    memcpy(block_buffer + memcpy_offset, source + bytes_written,
           bytes_to_write);
    bytes_written += bytes_to_write;
    ata_write(0, block_buffer, 1, start_block_index + blocks_read_in);
  }
  return bytes_written;
}


x86 assembly (clang, -O0):
<disk_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1179 <disk_write+0x49>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x22c(%rbp)
movl   $0x0,-0x224(%rbp)
mov    -0x224(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    129d <disk_write+0x16d>
lea    -0x220(%rbp),%rsi
mov    -0xc(%rbp),%ecx
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <ata_read@plt>
cmpl   $0x0,-0x224(%rbp)
jne    11cd <disk_write+0x9d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x234(%rbp)
jmp    11da <disk_write+0xaa>
xor    %eax,%eax
mov    %eax,-0x234(%rbp)
jmp    11da <disk_write+0xaa>
mov    -0x234(%rbp),%eax
mov    %eax,-0x228(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x22c(%rbp),%eax
add    -0x228(%rbp),%eax
cmp    $0x200,%eax
jle    1216 <disk_write+0xe6>
mov    $0x200,%eax
sub    -0x228(%rbp),%eax
mov    %eax,-0x238(%rbp)
jmp    1225 <disk_write+0xf5>
mov    -0x14(%rbp),%eax
sub    -0x22c(%rbp),%eax
mov    %eax,-0x238(%rbp)
mov    -0x238(%rbp),%eax
mov    %eax,-0x230(%rbp)
lea    -0x220(%rbp),%rdi
movslq -0x228(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x22c(%rbp),%rax
add    %rax,%rsi
mov    -0x230(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x230(%rbp),%eax
add    -0x22c(%rbp),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x220(%rbp),%rsi
mov    -0xc(%rbp),%ecx
add    -0x224(%rbp),%ecx
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <ata_write@plt>
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
jmp    118d <disk_write+0x5d>
mov    -0x22c(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disk_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %ecx,%ebp
mov    %edx,%r12d
mov    %ecx,%eax
add    %edx,%eax
lea    0x1ff(%rbp,%r12,1),%ecx
cmovns %eax,%ecx
sar    $0x9,%ecx
and    $0x1ff,%eax
cmp    $0x1,%eax
sbb    $0xffffffff,%ecx
test   %ecx,%ecx
jle    1209 <disk_write+0xd9>
mov    %esi,%r13d
xor    %ebx,%ebx
xor    %r15d,%r15d
mov    %rdi,0x8(%rsp)
mov    %ecx,0x4(%rsp)
nopl   0x0(%rax)
xor    %edi,%edi
lea    0x10(%rsp),%rsi
mov    $0x1,%edx
mov    %r13d,%ecx
call   1050 <ata_read@plt>
test   %r15d,%r15d
mov    $0x0,%eax
cmove  %r12d,%eax
mov    %ebp,%ecx
sub    %ebx,%ecx
lea    (%rcx,%rax,1),%edx
mov    %r12,%r14
mov    $0x200,%r12d
sub    %eax,%r12d
cmp    $0x201,%edx
cmovl  %ecx,%r12d
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
movslq %ebx,%rbx
mov    0x8(%rsp),%rax
lea    (%rax,%rbx,1),%rsi
mov    %r12d,%edx
call   1030 <memcpy@plt>
add    %r12d,%ebx
mov    %r14,%r12
lea    (%r15,%r13,1),%ecx
xor    %edi,%edi
lea    0x10(%rsp),%rsi
mov    $0x1,%edx
call   1040 <ata_write@plt>
mov    0x4(%rsp),%ecx
add    $0x1,%r15d
cmp    %r15d,%ecx
jne    1180 <disk_write+0x50>
jmp    120b <disk_write+0xdb>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @disk_write(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [512 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = add nsw i32 %15, %16
%18 = sdiv i32 %17, 512
store i32 %18, i32* %9
%19 = load i32, i32* %7
%20 = load i32, i32* %8
%21 = add nsw i32 %19, %20
%22 = srem i32 %21, 512
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %4
%25 = load i32, i32* %9
%26 = add nsw i32 %25, 1
store i32 %26, i32* %9
br label %27
27: ; preds = %24, %4
store i32 0, i32* %13
store i32 0, i32* %11
br label %28
28: ; preds = %76, %27
%29 = load i32, i32* %11
%30 = load i32, i32* %9
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %79
32: ; preds = %28
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%34 = load i32, i32* %6
%35 = call i32 @ata_read(i32 0, i8* %33, i32 1, i32 %34)
%36 = load i32, i32* %11
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = load i32, i32* %7
br label %41
40: ; preds = %32
br label %41
41: ; preds = %40, %38
%42 = phi i32 [ %39, %38 ], [ 0, %40 ]
store i32 %42, i32* %12
%43 = load i32, i32* %8
%44 = load i32, i32* %13
%45 = sub nsw i32 %43, %44
%46 = load i32, i32* %12
%47 = add nsw i32 %45, %46
%48 = icmp sgt i32 %47, 512
br i1 %48, label %49, label %52
49: ; preds = %41
%50 = load i32, i32* %12
%51 = sub nsw i32 512, %50
br label %56
52: ; preds = %41
%53 = load i32, i32* %8
%54 = load i32, i32* %13
%55 = sub nsw i32 %53, %54
br label %56
56: ; preds = %52, %49
%57 = phi i32 [ %51, %49 ], [ %55, %52 ]
store i32 %57, i32* %14
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%59 = load i32, i32* %12
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i8*, i8** %5
%63 = load i32, i32* %13
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = load i32, i32* %14
%67 = call i32 @memcpy(i8* %61, i8* %65, i32 %66)
%68 = load i32, i32* %14
%69 = load i32, i32* %13
%70 = add nsw i32 %69, %68
store i32 %70, i32* %13
%71 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%72 = load i32, i32* %6
%73 = load i32, i32* %11
%74 = add nsw i32 %72, %73
%75 = call i32 @ata_write(i32 0, i8* %71, i32 1, i32 %74)
br label %76
76: ; preds = %56
%77 = load i32, i32* %11
%78 = add nsw i32 %77, 1
store i32 %78, i32* %11
br label %28
79: ; preds = %28
%80 = load i32, i32* %13
ret i32 %80
}

LLVM IR (-O2):
define i32 @disk_write(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [512 x i8]
%6 = add nsw i32 %3, %2
%7 = sdiv i32 %6, 512
%8 = and i32 %6, 511
%9 = icmp ne i32 %8, 0
%10 = zext i1 %9 to i32
%11 = add nsw i32 %7, %10
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%13 = icmp sgt i32 %11, 0
br i1 %13, label %14, label %35
14: ; preds = %4, %14
%15 = phi i32 [ %30, %14 ], [ 0, %4 ]
%16 = phi i32 [ %33, %14 ], [ 0, %4 ]
%17 = call i32 @ata_read(i32 0, i8* %12, i32 1, i32 %1)
%18 = icmp eq i32 %16, 0
%19 = select i1 %18, i32 %2, i32 0
%20 = sub nsw i32 %3, %15
%21 = add nsw i32 %20, %19
%22 = icmp sgt i32 %21, 512
%23 = sub nsw i32 512, %19
%24 = select i1 %22, i32 %23, i32 %20
%25 = sext i32 %19 to i64
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %25
%27 = sext i32 %15 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = call i32 @memcpy(i8* %26, i8* %28, i32 %24)
%30 = add nsw i32 %24, %15
%31 = add nsw i32 %16, %1
%32 = call i32 @ata_write(i32 0, i8* %12, i32 1, i32 %31)
%33 = add nuw nsw i32 %16, 1
%34 = icmp eq i32 %33, %11
br i1 %34, label %35, label %14
35: ; preds = %14, %4
%36 = phi i32 [ 0, %4 ], [ %30, %14 ]
ret i32 %36
}