#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*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int creat(char const *, int);
int /*<<< orphan*/ lseek(int const, int const, int /*<<< orphan*/);
int /*<<< orphan*/ read(int const, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void outputfile(const char *outpath, const int ihfs_img, const int offset,
                const int length) {
  int outfd;
  int i, rem;
  char buf[4096];
  lseek(ihfs_img, offset, SEEK_SET);
  outfd = creat(outpath, 0644);
  for (i = 0; i < length / 4096; ++i) {
    read(ihfs_img, buf, 4096);
    write(outfd, buf, 4096);
  }
  rem = length - i * 4096;
  if (rem > 0) {
    read(ihfs_img, buf, rem);
    write(outfd, buf, rem);
  }
  close(outfd);
}


x86 assembly (clang, -O0):
<outputfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x8(%rbp),%rdi
mov    $0x1a4,%esi
call   1070 <creat@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x1024(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1024(%rbp),%eax
cmp    %ecx,%eax
jge    11ee <outputfile+0x9e>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1194 <outputfile+0x44>
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shl    $0xc,%ecx
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    122a <outputfile+0xda>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1060 <read@plt>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<outputfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %ecx,%ebp
mov    %esi,%r12d
mov    %rdi,%rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %esi,%edi
mov    %edx,%esi
mov    %eax,%edx
call   1040 <lseek@plt>
mov    %rbx,%rdi
mov    $0x1a4,%esi
call   1070 <creat@plt>
mov    %eax,%r14d
lea    0xfff(%rbp),%r13d
test   %ebp,%ebp
cmovns %ebp,%r13d
xor    %r15d,%r15d
cmp    $0x1000,%ebp
jl     11f4 <outputfile+0xa4>
mov    %rbp,0x8(%rsp)
sar    $0xc,%r13d
lea    0x10(%rsp),%rbx
mov    %r13d,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebp
jne    11c0 <outputfile+0x70>
shl    $0xc,%r13d
neg    %r13d
mov    %r13d,%r15d
mov    0x8(%rsp),%rbp
add    %ebp,%r15d
test   %r15d,%r15d
jle    121d <outputfile+0xcd>
lea    0x10(%rsp),%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %r14d,%edi
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

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

LLVM IR (-O2):
@SEEK_SET = global i32 0
define void @outputfile(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @lseek(i32 %1, i32 %2, i32 %7)
%9 = tail call i32 @creat(i8* %0, i32 420)
%10 = sdiv i32 %3, 4096
%11 = icmp sgt i32 %3, 4095
br i1 %11, label %12, label %20
12: ; preds = %4, %12
%13 = phi i32 [ %16, %12 ], [ 0, %4 ]
%14 = call i32 @read(i32 %1, i8* %6, i32 4096)
%15 = call i32 @write(i32 %9, i8* %6, i32 4096)
%16 = add nuw nsw i32 %13, 1
%17 = icmp eq i32 %16, %10
br i1 %17, label %18, label %12
18: ; preds = %12
%19 = mul i32 %10, -4096
br label %20
20: ; preds = %18, %4
%21 = phi i32 [ 0, %4 ], [ %19, %18 ]
%22 = add i32 %21, %3
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @read(i32 %1, i8* %6, i32 %22)
%26 = call i32 @write(i32 %9, i8* %6, i32 %22)
br label %27
27: ; preds = %24, %20
%28 = tail call i32 @close(i32 %9)
ret void
}