#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned short *, int, unsigned int,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned short,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned short *, int, unsigned int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, unsigned char, unsigned short,
                          unsigned short);
unsigned short rand();
int dump565Partial(char *srcName, char *dumpName, unsigned short yStart,
                   unsigned short yEnd, unsigned short w) {
  FILE *picFile, *dumpFile;
  unsigned short buff[800];
  unsigned int buffSize = 800, writeBuffSize = w;
  unsigned char row, height = yEnd - yStart;
  picFile = fopen(srcName, "r+b");
  dumpFile = fopen(dumpName, "a+b");
  fseek(picFile, yStart * 800 * 2, SEEK_SET);
  for (row = 0; row < height; row++) {
    printf("Writing line %d : 0x%04x 0x%04x \n", row, *(buff + rand() % w),
           *(buff + rand() % w));
    fread(buff, 2, buffSize, picFile);
    fwrite(buff, 2, writeBuffSize, dumpFile);
  }
  fclose(dumpFile);
  fclose(picFile);
  return 0;
}


x86 assembly (clang, -O0):
<dump565Partial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x680,%rsp
mov    %r8w,%ax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %dx,-0x12(%rbp)
mov    %cx,-0x14(%rbp)
mov    %ax,-0x16(%rbp)
movl   $0x320,-0x674(%rbp)
movzwl -0x16(%rbp),%eax
mov    %eax,-0x678(%rbp)
movzwl -0x14(%rbp),%eax
movzwl -0x12(%rbp),%ecx
sub    %ecx,%eax
mov    %al,-0x67a(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
movzwl -0x12(%rbp),%eax
imul   $0x320,%eax,%eax
shl    $0x1,%eax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edx
movzwl %ax,%esi
call   1060 <fseek@plt>
movb   $0x0,-0x679(%rbp)
movzbl -0x679(%rbp),%eax
movzbl -0x67a(%rbp),%ecx
cmp    %ecx,%eax
jge    12d2 <dump565Partial+0x162>
mov    -0x679(%rbp),%al
mov    %al,-0x67b(%rbp)
mov    $0x0,%al
call   1090 <rand@plt>
movzwl %ax,%eax
movzwl -0x16(%rbp),%ecx
cltd
idiv   %ecx
movslq %edx,%rax
mov    -0x670(%rbp,%rax,2),%ax
ff
mov    %ax,-0x67e(%rbp)
mov    $0x0,%al
call   1090 <rand@plt>
mov    -0x67e(%rbp),%cx
movzwl %ax,%eax
movzwl -0x16(%rbp),%esi
cltd
idiv   %esi
mov    %edx,%eax
mov    -0x67b(%rbp),%dl
cltq
lea    0xd92(%rip),%rdi
movzbl %dl,%esi
movzwl %cx,%edx
movzwl -0x670(%rbp,%rax,2),%ecx
ff
call   1050 <printf@plt>
lea    -0x670(%rbp),%rdi
mov    -0x674(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x2,%esi
call   1030 <fread@plt>
lea    -0x670(%rbp),%rdi
mov    -0x678(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x2,%esi
call   1080 <fwrite@plt>
mov    -0x679(%rbp),%al
add    $0x1,%al
mov    %al,-0x679(%rbp)
jmp    1208 <dump565Partial+0x98>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x680,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump565Partial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x648,%rsp
mov    %r8d,%r15d
mov    %ecx,%r13d
mov    %edx,%r14d
mov    %rsi,%r12
sub    %edx,%r13d
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbp
lea    0xe5e(%rip),%rsi
mov    %r12,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
imul   $0x640,%r14d,%eax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edx
movzwl %ax,%esi
mov    %rbp,%rdi
call   1060 <fseek@plt>
and    $0xff,%r13w
je     1251 <dump565Partial+0xe1>
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <rand@plt>
xor    %edx,%edx
div    %r15w
movzwl %dx,%eax
movzwl (%rsp,%rax,2),%r12d
xor    %eax,%eax
call   1090 <rand@plt>
xor    %edx,%edx
div    %r15w
movzwl %dx,%eax
movzwl (%rsp,%rax,2),%ecx
movzbl %r14b,%esi
lea    0xdf4(%rip),%rdi
mov    %r12d,%edx
call   1050 <printf@plt>
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x2,%esi
mov    $0x320,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
mov    %r12,%rdi
mov    $0x2,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
add    $0x1,%r14d
cmp    %r14w,%r13w
jne    11e0 <dump565Partial+0x70>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x648,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@.str.1 = [4 x i8] c"a+b\00"
@SEEK_SET = global i32 0
@.str.2 = [34 x i8] c"Writing line %d : 0x%04x 0x%04x \0A\00"
define i32 @dump565Partial(i8* %0, i8* %1, i16 zeroext %2, i16 zeroext %3, i16 zeroext %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i16
%9 = alloca i16
%10 = alloca i16
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca [800 x i16]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8
%17 = alloca i8
store i8* %0, i8** %6
store i8* %1, i8** %7
store i16 %2, i16* %8
store i16 %3, i16* %9
store i16 %4, i16* %10
store i32 800, i32* %14
%18 = load i16, i16* %10
%19 = zext i16 %18 to i32
store i32 %19, i32* %15
%20 = load i16, i16* %9
%21 = zext i16 %20 to i32
%22 = load i16, i16* %8
%23 = zext i16 %22 to i32
%24 = sub nsw i32 %21, %23
%25 = trunc i32 %24 to i8
store i8 %25, i8* %17
%26 = load i8*, i8** %6
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %11
%28 = load i8*, i8** %7
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %12
%30 = load i32*, i32** %11
%31 = load i16, i16* %8
%32 = zext i16 %31 to i32
%33 = mul nsw i32 %32, 800
%34 = mul nsw i32 %33, 2
%35 = trunc i32 %34 to i16
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %30, i16 zeroext %35, i32 %36)
store i8 0, i8* %16
br label %38
38: ; preds = %73, %5
%39 = load i8, i8* %16
%40 = zext i8 %39 to i32
%41 = load i8, i8* %17
%42 = zext i8 %41 to i32
%43 = icmp slt i32 %40, %42
br i1 %43, label %44, label %76
44: ; preds = %38
%45 = load i8, i8* %16
%46 = getelementptr inbounds [800 x i16], [800 x i16]* %13, i64 0, i64 0
%47 = call zeroext i16 (...) @rand()
%48 = zext i16 %47 to i32
%49 = load i16, i16* %10
%50 = zext i16 %49 to i32
%51 = srem i32 %48, %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i16, i16* %46, i64 %52
%54 = load i16, i16* %53
%55 = getelementptr inbounds [800 x i16], [800 x i16]* %13, i64 0, i64 0
%56 = call zeroext i16 (...) @rand()
%57 = zext i16 %56 to i32
%58 = load i16, i16* %10
%59 = zext i16 %58 to i32
%60 = srem i32 %57, %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i16, i16* %55, i64 %61
%63 = load i16, i16* %62
%64 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %45, i16 zeroext %54, i16 zeroext %63)
%65 = getelementptr inbounds [800 x i16], [800 x i16]* %13, i64 0, i64 0
%66 = load i32, i32* %14
%67 = load i32*, i32** %11
%68 = call i32 @fread(i16* %65, i32 2, i32 %66, i32* %67)
%69 = getelementptr inbounds [800 x i16], [800 x i16]* %13, i64 0, i64 0
%70 = load i32, i32* %15
%71 = load i32*, i32** %12
%72 = call i32 @fwrite(i16* %69, i32 2, i32 %70, i32* %71)
br label %73
73: ; preds = %44
%74 = load i8, i8* %16
%75 = add i8 %74, 1
store i8 %75, i8* %16
br label %38
76: ; preds = %38
%77 = load i32*, i32** %12
%78 = call i32 @fclose(i32* %77)
%79 = load i32*, i32** %11
%80 = call i32 @fclose(i32* %79)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@.str.1 = [4 x i8] c"a+b\00"
@SEEK_SET = global i32 0
@.str.2 = [34 x i8] c"Writing line %d : 0x%04x 0x%04x \0A\00"
define i32 @dump565Partial(i8* nocapture %0, i8* nocapture %1, i16 zeroext %2, i16 zeroext %3, i16 zeroext %4) {
%6 = alloca [800 x i16]
%7 = bitcast [800 x i16]* %6 to i8*
%8 = zext i16 %4 to i32
%9 = sub i16 %3, %2
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = mul i16 %2, 1600
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %10, i16 zeroext %12, i32 %13)
%15 = and i16 %9, 255
%16 = icmp eq i16 %15, 0
br i1 %16, label %37, label %17
17: ; preds = %5
%18 = getelementptr inbounds [800 x i16], [800 x i16]* %6, i64 0, i64 0
br label %19
19: ; preds = %17, %19
%20 = phi i16 [ 0, %17 ], [ %35, %19 ]
%21 = tail call zeroext i16 (...) @rand()
%22 = urem i16 %21, %4
%23 = zext i16 %22 to i64
%24 = getelementptr inbounds [800 x i16], [800 x i16]* %6, i64 0, i64 %23
%25 = load i16, i16* %24
%26 = tail call zeroext i16 (...) @rand()
%27 = urem i16 %26, %4
%28 = zext i16 %27 to i64
%29 = getelementptr inbounds [800 x i16], [800 x i16]* %6, i64 0, i64 %28
%30 = load i16, i16* %29
%31 = trunc i16 %20 to i8
%32 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %31, i16 zeroext %25, i16 zeroext %30)
%33 = call i32 @fread(i16* %18, i32 2, i32 800, i32* %10)
%34 = call i32 @fwrite(i16* %18, i32 2, i32 %8, i32* %11)
%35 = add nuw nsw i16 %20, 1
%36 = icmp eq i16 %35, %15
br i1 %36, label %37, label %19
37: ; preds = %19, %5
%38 = tail call i32 @fclose(i32* %11)
%39 = tail call i32 @fclose(i32* %10)
ret i32 0
}