#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ decrypt(unsigned char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, unsigned int, int,
                          int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
void filecut(unsigned char *disk, char *fname, unsigned int rofs,
             unsigned int rsize, unsigned int dofs) {
  int i;
  FILE *fp;
  int read_size = (((rsize + dofs) / 0x200) + 1) * 0x200;
  unsigned char *buf = (unsigned char *)malloc(read_size);
  memcpy(buf, disk + rofs, read_size);
  for (i = 0; i < read_size; i += 0x200) {
    decrypt(buf + i, 1);
  }
  fp = fopen(fname, "wb");
  if (fp) {
    fwrite(buf + dofs, rsize, 1, fp);
    fclose(fp);
  }
}


x86 assembly (clang, -O0):
<filecut>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
shr    $0x9,%eax
add    $0x1,%eax
shl    $0x9,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%eax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    11e6 <filecut+0x86>
mov    -0x38(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    $0x1,%esi
call   1030 <decrypt@plt>
mov    -0x20(%rbp),%eax
add    $0x200,%eax
mov    %eax,-0x20(%rbp)
jmp    11b5 <filecut+0x55>
mov    -0x10(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1229 <filecut+0xc9>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filecut>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r15d
mov    %edx,%r13d
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
mov    %rcx,0x8(%rsp)
lea    (%rcx,%r15,1),%ebp
add    $0x200,%ebp
and    $0xfffffe00,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %r13d,%esi
add    %r12,%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1050 <memcpy@plt>
test   %ebp,%ebp
jle    11da <filecut+0x7a>
mov    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%r14,%rbx,1),%rdi
mov    $0x1,%esi
call   1030 <decrypt@plt>
add    $0x200,%rbx
cmp    %rbp,%rbx
jb     11c0 <filecut+0x60>
lea    0xe1f(%rip),%rsi
mov    0x10(%rsp),%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1224 <filecut+0xc4>
mov    %rax,%rbp
mov    %r15d,%eax
add    %rax,%r14
mov    %r14,%rdi
mov    0x8(%rsp),%rsi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define void @filecut(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%15 = load i32, i32* %9
%16 = load i32, i32* %10
%17 = add i32 %15, %16
%18 = udiv i32 %17, 512
%19 = add i32 %18, 1
%20 = mul i32 %19, 512
store i32 %20, i32* %13
%21 = load i32, i32* %13
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %14
%24 = load i8*, i8** %14
%25 = load i8*, i8** %6
%26 = load i32, i32* %8
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32, i32* %13
%30 = call i32 @memcpy(i8* %24, i8* %28, i32 %29)
store i32 0, i32* %11
br label %31
31: ; preds = %41, %5
%32 = load i32, i32* %11
%33 = load i32, i32* %13
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %44
35: ; preds = %31
%36 = load i8*, i8** %14
%37 = load i32, i32* %11
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = call i32 @decrypt(i8* %39, i32 1)
br label %41
41: ; preds = %35
%42 = load i32, i32* %11
%43 = add nsw i32 %42, 512
store i32 %43, i32* %11
br label %31
44: ; preds = %31
%45 = load i8*, i8** %7
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %46, i32** %12
%47 = load i32*, i32** %12
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %59
49: ; preds = %44
%50 = load i8*, i8** %14
%51 = load i32, i32* %10
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i32, i32* %9
%55 = load i32*, i32** %12
%56 = call i32 @fwrite(i8* %53, i32 %54, i32 1, i32* %55)
%57 = load i32*, i32** %12
%58 = call i32 @fclose(i32* %57)
br label %59
59: ; preds = %49, %44
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
define void @filecut(i8* %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = add i32 %3, 512
%7 = add i32 %6, %4
%8 = and i32 %7, -512
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = zext i32 %2 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 @memcpy(i8* %10, i8* %12, i32 %8)
%14 = icmp sgt i32 %8, 0
br i1 %14, label %15, label %23
15: ; preds = %5
%16 = zext i32 %8 to i64
br label %17
17: ; preds = %15, %17
%18 = phi i64 [ 0, %15 ], [ %21, %17 ]
%19 = getelementptr inbounds i8, i8* %10, i64 %18
%20 = tail call i32 @decrypt(i8* %19, i32 1)
%21 = add nuw nsw i64 %18, 512
%22 = icmp ult i64 %21, %16
br i1 %22, label %17, label %23
23: ; preds = %17, %5
%24 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = zext i32 %4 to i64
%28 = getelementptr inbounds i8, i8* %10, i64 %27
%29 = tail call i32 @fwrite(i8* %28, i32 %3, i32 1, i32* %24)
%30 = tail call i32 @fclose(i32* %24)
br label %31
31: ; preds = %26, %23
ret void
}