#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ cal(int, int *, int *, int *);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(void *);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int readFileToMem(char *, void **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ writeToFloopy(int, int, int, void *, int);
void readToFloopy(char *fileName) {
  printf("start readToFloopy: ");
  int fd;
  if ((fd = open(fileName, O_RDWR | O_CREAT)) == -1) {
    fprintf(stderr, "open %s error \n ", fileName);
    error("open file error");
  }
  void *buf;
  int rsize = readFileToMem(fileName, &buf);
  int sector_num = rsize / 512 + (rsize % 512 ? 1 : 0);
  printf("rsize=%d sector_num = %d\n", rsize, sector_num);
  if (!sector_num)
    return;
  int h = 0, c = 0, s = 0;
  int nsize = 512;
  for (int i = 1; i <= sector_num; i++, rsize = rsize - 512) {
    cal(i, &h, &c, &s);
    if (rsize < 512)
      nsize = rsize;
    writeToFloopy(h, c, s, buf, nsize);
  }
  free(buf);
}


x86 assembly (clang, -O0):
<readToFloopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11e6 <readToFloopy+0x66>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe45(%rip),%rdi
call   1070 <error@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <readFileToMem@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%eax
mov    %edx,%esi
xor    %ecx,%ecx
mov    $0x1,%edx
cmp    $0x0,%esi
cmovne %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1249 <readToFloopy+0xc9>
jmp    12d7 <readToFloopy+0x157>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x200,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     12ce <readToFloopy+0x14e>
mov    -0x34(%rbp),%edi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1090 <cal@plt>
cmpl   $0x200,-0x1c(%rbp)
jge    129f <readToFloopy+0x11f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%r8d
call   10a0 <writeToFloopy@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x1c(%rbp)
jmp    126c <readToFloopy+0xec>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readToFloopy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0xe68(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e3a(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    11e2 <readToFloopy+0x62>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
lea    0xe49(%rip),%rdi
call   1070 <error@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <readFileToMem@plt>
mov    %eax,%r15d
lea    0x1ff(%r15),%r14d
test   %eax,%eax
cmovns %eax,%r14d
sar    $0x9,%r14d
xor    %ebx,%ebx
test   $0x1ff,%eax
setne  %bl
lea    0xe22(%rip),%rdi
lea    (%r14,%rbx,1),%edx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    %ebx,%r14d
je     12ba <readToFloopy+0x13a>
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
test   %r14d,%r14d
jle    12b0 <readToFloopy+0x130>
neg    %r14d
mov    $0x200,%ebp
mov    $0x1,%ebx
lea    0x8(%rsp),%r12
lea    0x4(%rsp),%r13
xchg   %ax,%ax
mov    %ebx,%edi
lea    0xc(%rsp),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1090 <cal@plt>
cmp    $0x200,%r15d
cmovl  %r15d,%ebp
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
mov    0x4(%rsp),%edx
mov    0x10(%rsp),%rcx
mov    %ebp,%r8d
call   10a0 <writeToFloopy@plt>
add    $0xfffffe00,%r15d
lea    (%r14,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    1260 <readToFloopy+0xe0>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"start readToFloopy: \00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"open %s error \0A \00"
@.str.2 = [16 x i8] c"open file error\00"
@.str.3 = [26 x i8] c"rsize=%d sector_num = %d\0A\00"
define void @readToFloopy(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %2
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %13, i32 %16)
store i32 %17, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %2
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %19, %1
%25 = load i8*, i8** %2
%26 = call i32 @readFileToMem(i8* %25, i8** %4)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = sdiv i32 %27, 512
%29 = load i32, i32* %5
%30 = srem i32 %29, 512
%31 = icmp ne i32 %30, 0
%32 = zext i1 %31 to i64
%33 = select i1 %31, i32 1, i32 0
%34 = add nsw i32 %28, %33
store i32 %34, i32* %6
%35 = load i32, i32* %5
%36 = load i32, i32* %6
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 %36)
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %24
br label %68
41: ; preds = %24
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 512, i32* %10
store i32 1, i32* %11
br label %42
42: ; preds = %60, %41
%43 = load i32, i32* %11
%44 = load i32, i32* %6
%45 = icmp sle i32 %43, %44
br i1 %45, label %46, label %65
46: ; preds = %42
%47 = load i32, i32* %11
%48 = call i32 @cal(i32 %47, i32* %7, i32* %8, i32* %9)
%49 = load i32, i32* %5
%50 = icmp slt i32 %49, 512
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = load i32, i32* %5
store i32 %52, i32* %10
br label %53
53: ; preds = %51, %46
%54 = load i32, i32* %7
%55 = load i32, i32* %8
%56 = load i32, i32* %9
%57 = load i8*, i8** %4
%58 = load i32, i32* %10
%59 = call i32 @writeToFloopy(i32 %54, i32 %55, i32 %56, i8* %57, i32 %58)
br label %60
60: ; preds = %53
%61 = load i32, i32* %11
%62 = add nsw i32 %61, 1
store i32 %62, i32* %11
%63 = load i32, i32* %5
%64 = sub nsw i32 %63, 512
store i32 %64, i32* %5
br label %42
65: ; preds = %42
%66 = load i8*, i8** %4
%67 = call i32 @free(i8* %66)
br label %68
68: ; preds = %65, %40
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"start readToFloopy: \00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"open %s error \0A \00"
@.str.2 = [16 x i8] c"open file error\00"
@.str.3 = [26 x i8] c"rsize=%d sector_num = %d\0A\00"
define void @readToFloopy(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%15 = tail call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %12, %1
%17 = bitcast i8** %2 to i8*
%18 = call i32 @readFileToMem(i8* %0, i8** %2)
%19 = sdiv i32 %18, 512
%20 = and i32 %18, 511
%21 = icmp ne i32 %20, 0
%22 = zext i1 %21 to i32
%23 = add nsw i32 %19, %22
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %18, i32 %23)
%25 = icmp eq i32 %23, 0
br i1 %25, label %49, label %26
26: ; preds = %16
%27 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%28 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%29 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%30 = icmp slt i32 %23, 1
br i1 %30, label %31, label %34
31: ; preds = %34, %26
%32 = load i8*, i8** %2
%33 = call i32 @free(i8* %32)
br label %49
34: ; preds = %26, %34
%35 = phi i32 [ %46, %34 ], [ 1, %26 ]
%36 = phi i32 [ %40, %34 ], [ 512, %26 ]
%37 = phi i32 [ %47, %34 ], [ %18, %26 ]
%38 = call i32 @cal(i32 %35, i32* %3, i32* %4, i32* %5)
%39 = icmp slt i32 %37, 512
%40 = select i1 %39, i32 %37, i32 %36
%41 = load i32, i32* %3
%42 = load i32, i32* %4
%43 = load i32, i32* %5
%44 = load i8*, i8** %2
%45 = call i32 @writeToFloopy(i32 %41, i32 %42, i32 %43, i8* %44, i32 %40)
%46 = add nuw nsw i32 %35, 1
%47 = add nsw i32 %37, -512
%48 = icmp eq i32 %35, %23
br i1 %48, label %31, label %34
49: ; preds = %16, %31
ret void
}