#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*/ memset(char *, int, int);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void hxf_to_ihfs(int hxf_img, int ihfs_img, int filesize) {
  char buf[512];
  char ff[512];
  int i, n, rem;
  n = filesize / 512;
  rem = filesize % 512;
  for (i = 0; i < n; ++i) {
    read(hxf_img, buf, 512);
    write(ihfs_img, buf, 512);
  }
  if (rem > 0) {
    read(hxf_img, buf, rem);
    write(ihfs_img, buf, rem);
    memset(ff, 0xff, 512 - rem);
    write(ihfs_img, ff, 512 - rem);
  }
}


x86 assembly (clang, -O0):
<hxf_to_ihfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    %eax,-0x418(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    %edx,-0x41c(%rbp)
movl   $0x0,-0x414(%rbp)
mov    -0x414(%rbp),%eax
cmp    -0x418(%rbp),%eax
jge    11be <hxf_to_ihfs+0x8e>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1030 <write@plt>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    1170 <hxf_to_ihfs+0x40>
cmpl   $0x0,-0x41c(%rbp)
jle    122b <hxf_to_ihfs+0xfb>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <write@plt>
lea    -0x410(%rbp),%rdi
mov    $0x200,%edx
sub    -0x41c(%rbp),%edx
mov    $0xff,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x200,%edx
sub    -0x41c(%rbp),%edx
call   1030 <write@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hxf_to_ihfs>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
lea    0x1ff(%rdx),%ebp
test   %edx,%edx
cmovns %edx,%ebp
mov    %esi,%r15d
mov    %edi,%r12d
mov    %ebp,%eax
and    $0xfffffe00,%eax
mov    %edx,%r14d
sub    %eax,%r14d
cmp    $0x200,%edx
jl     1195 <hxf_to_ihfs+0x65>
sar    $0x9,%ebp
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebp
jne    1170 <hxf_to_ihfs+0x40>
test   %r14d,%r14d
jle    11e5 <hxf_to_ihfs+0xb5>
mov    %rsp,%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    $0x200,%ebx
sub    %r14d,%ebx
lea    0x200(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %ebx,%edx
call   1040 <memset@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @hxf_to_ihfs(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* %6
%13 = sdiv i32 %12, 512
store i32 %13, i32* %10
%14 = load i32, i32* %6
%15 = srem i32 %14, 512
store i32 %15, i32* %11
store i32 0, i32* %9
br label %16
16: ; preds = %27, %3
%17 = load i32, i32* %9
%18 = load i32, i32* %10
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i32, i32* %4
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 512)
%24 = load i32, i32* %5
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%26 = call i32 @write(i32 %24, i8* %25, i32 512)
br label %27
27: ; preds = %20
%28 = load i32, i32* %9
%29 = add nsw i32 %28, 1
store i32 %29, i32* %9
br label %16
30: ; preds = %16
%31 = load i32, i32* %11
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %51
33: ; preds = %30
%34 = load i32, i32* %4
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%36 = load i32, i32* %11
%37 = call i32 @read(i32 %34, i8* %35, i32 %36)
%38 = load i32, i32* %5
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%40 = load i32, i32* %11
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%43 = load i32, i32* %11
%44 = sub nsw i32 512, %43
%45 = call i32 @memset(i8* %42, i32 255, i32 %44)
%46 = load i32, i32* %5
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%48 = load i32, i32* %11
%49 = sub nsw i32 512, %48
%50 = call i32 @write(i32 %46, i8* %47, i32 %49)
br label %51
51: ; preds = %33, %30
ret void
}

LLVM IR (-O2):
define void @hxf_to_ihfs(i32 %0, i32 %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = sdiv i32 %2, 512
%9 = srem i32 %2, 512
%10 = icmp sgt i32 %2, 511
br i1 %10, label %11, label %17
11: ; preds = %3, %11
%12 = phi i32 [ %15, %11 ], [ 0, %3 ]
%13 = call i32 @read(i32 %0, i8* %6, i32 512)
%14 = call i32 @write(i32 %1, i8* %6, i32 512)
%15 = add nuw nsw i32 %12, 1
%16 = icmp eq i32 %15, %8
br i1 %16, label %17, label %11
17: ; preds = %11, %3
%18 = icmp sgt i32 %9, 0
br i1 %18, label %19, label %25
19: ; preds = %17
%20 = call i32 @read(i32 %0, i8* %6, i32 %9)
%21 = call i32 @write(i32 %1, i8* %6, i32 %9)
%22 = sub nsw i32 512, %9
%23 = call i32 @memset(i8* %7, i32 255, i32 %22)
%24 = call i32 @write(i32 %1, i8* %7, i32 %22)
br label %25
25: ; preds = %19, %17
ret void
}