#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 atoi(char *);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ makeFloopy(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int readFileToMem(char *, void **);
int /*<<< orphan*/ readToFloopy(char *);
int /*<<< orphan*/ writeToFloopy(int, int, int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 6) {
    die("Usage:./imgTool inputFile outputFile cylenderNo headNo sectorNo");
  }
  readToFloopy(argv[2]);
  int m_header = atoi(argv[3]);
  int cylender = atoi(argv[4]);
  int sector_pos = atoi(argv[5]);
  void *pbuf;
  int rsize = readFileToMem(argv[1], &pbuf);
  printf("rsize = %d\n", rsize);
  int n = rsize / 512 + (rsize % 512 == 0 ? 0 : 1);
  int wsize = 512;
  for (int i = 0; i < n; i++) {
    if (i == (n - 1)) {
      wsize = rsize;
    }
    printf("count=%d\twrite to header=%d cylender=%d sector=%d wsize=%d\n ", i,
           m_header, cylender, sector_pos, wsize);
    writeToFloopy(m_header, cylender, sector_pos, ((char *)pbuf + (i * 512)),
                  wsize);
    sector_pos++;
    if (sector_pos > 18) {
      sector_pos = 1;
      cylender++;
    }
    rsize -= 512;
  }
  free(pbuf);
  makeFloopy(argv[2]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x6,-0x8(%rbp)
je     11ac <main+0x2c>
lea    0xe59(%rip),%rdi
call   1080 <die@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <readToFloopy@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <readFileToMem@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    -0x3c(%rbp),%eax
mov    %edx,%esi
mov    $0x1,%ecx
xor    %edx,%edx
cmp    $0x0,%esi
cmove  %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x30(%rbp)
movl   $0x200,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    12eb <main+0x16b>
mov    -0x38(%rbp),%eax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    126f <main+0xef>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x34(%rbp),%r9d
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%eax
shl    $0x9,%eax
cltq
add    %rax,%rcx
mov    -0x34(%rbp),%r8d
call   10a0 <writeToFloopy@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x12,-0x1c(%rbp)
jle    12d2 <main+0x152>
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    124c <main+0xcc>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <makeFloopy@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
cmp    $0x6,%edi
je     11a2 <main+0x22>
lea    0xe63(%rip),%rdi
call   1080 <die@plt>
mov    0x10(%rbp),%rdi
call   1060 <readToFloopy@plt>
mov    0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,0x4(%rsp)
mov    0x20(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
mov    0x28(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%r13d
mov    %rbp,0x10(%rsp)
mov    0x8(%rbp),%rdi
lea    0x8(%rsp),%rsi
call   1070 <readFileToMem@plt>
mov    %eax,%ebx
lea    0xe54(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x1ff(%rbx),%edx
test   %ebx,%ebx
cmovns %ebx,%edx
sar    $0x9,%edx
xor    %eax,%eax
test   $0x1ff,%ebx
setne  %al
add    %edx,%eax
jle    12b3 <main+0x133>
mov    %r14d,%ecx
lea    -0x1(%rax),%edx
mov    %eax,%eax
shl    $0x9,%rdx
mov    %rdx,0x20(%rsp)
shl    $0x9,%rax
mov    %rax,0x18(%rsp)
mov    $0x200,%ebp
xor    %r15d,%r15d
xor    %r14d,%r14d
mov    0x4(%rsp),%r12d
mov    %ecx,(%rsp)
cmp    %r15,0x20(%rsp)
cmove  %ebx,%ebp
lea    0xdfa(%rip),%rdi
mov    %r14d,%esi
mov    %r12d,%edx
mov    %r13d,%r8d
mov    %ebp,%r9d
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rcx
add    %r15,%rcx
mov    %r12d,%edi
mov    (%rsp),%esi
mov    %r13d,%edx
mov    %ebp,%r8d
call   10a0 <writeToFloopy@plt>
mov    (%rsp),%ecx
xor    %eax,%eax
cmp    $0x12,%r13d
lea    0x1(%r13),%edx
setge  %al
mov    $0x1,%esi
cmovge %esi,%edx
add    %eax,%ecx
add    $0xfffffe00,%ebx
add    $0x1,%r14d
add    $0x200,%r15
mov    %edx,%r13d
cmp    %r15,0x18(%rsp)
jne    1240 <main+0xc0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rdi
call   1040 <makeFloopy@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [64 x i8] c"Usage:./imgTool inputFile outputFile cylenderNo headNo sectorNo\00"
@.str.1 = [12 x i8] c"rsize = %d\0A\00"
@.str.2 = [61 x i8] c"count=%d\09write to header=%d cylender=%d sector=%d wsize=%d\0A \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 6
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @die(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @readToFloopy(i8* %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 3
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %6
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 4
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %7
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 5
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %8
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @readFileToMem(i8* %37, i8** %9)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %10
%42 = sdiv i32 %41, 512
%43 = load i32, i32* %10
%44 = srem i32 %43, 512
%45 = icmp eq i32 %44, 0
%46 = zext i1 %45 to i64
%47 = select i1 %45, i32 0, i32 1
%48 = add nsw i32 %42, %47
store i32 %48, i32* %11
store i32 512, i32* %12
store i32 0, i32* %13
br label %49
49: ; preds = %87, %18
%50 = load i32, i32* %13
%51 = load i32, i32* %11
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %90
53: ; preds = %49
%54 = load i32, i32* %13
%55 = load i32, i32* %11
%56 = sub nsw i32 %55, 1
%57 = icmp eq i32 %54, %56
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = load i32, i32* %10
store i32 %59, i32* %12
br label %60
60: ; preds = %58, %53
%61 = load i32, i32* %13
%62 = load i32, i32* %6
%63 = load i32, i32* %7
%64 = load i32, i32* %8
%65 = load i32, i32* %12
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %61, i32 %62, i32 %63, i32 %64, i32 %65)
%67 = load i32, i32* %6
%68 = load i32, i32* %7
%69 = load i32, i32* %8
%70 = load i8*, i8** %9
%71 = load i32, i32* %13
%72 = mul nsw i32 %71, 512
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %70, i64 %73
%75 = load i32, i32* %12
%76 = call i32 @writeToFloopy(i32 %67, i32 %68, i32 %69, i8* %74, i32 %75)
%77 = load i32, i32* %8
%78 = add nsw i32 %77, 1
store i32 %78, i32* %8
%79 = load i32, i32* %8
%80 = icmp sgt i32 %79, 18
br i1 %80, label %81, label %84
81: ; preds = %60
store i32 1, i32* %8
%82 = load i32, i32* %7
%83 = add nsw i32 %82, 1
store i32 %83, i32* %7
br label %84
84: ; preds = %81, %60
%85 = load i32, i32* %10
%86 = sub nsw i32 %85, 512
store i32 %86, i32* %10
br label %87
87: ; preds = %84
%88 = load i32, i32* %13
%89 = add nsw i32 %88, 1
store i32 %89, i32* %13
br label %49
90: ; preds = %49
%91 = load i8*, i8** %9
%92 = call i32 @free(i8* %91)
%93 = load i8**, i8*** %5
%94 = getelementptr inbounds i8*, i8** %93, i64 2
%95 = load i8*, i8** %94
%96 = call i32 @makeFloopy(i8* %95)
ret i32 0
}

LLVM IR (-O2):
@.str = [64 x i8] c"Usage:./imgTool inputFile outputFile cylenderNo headNo sectorNo\00"
@.str.1 = [12 x i8] c"rsize = %d\0A\00"
@.str.2 = [61 x i8] c"count=%d\09write to header=%d cylender=%d sector=%d wsize=%d\0A \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp eq i32 %0, 6
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @die(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call i32 @readToFloopy(i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 3
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 4
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 5
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = bitcast i8** %3 to i8*
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @readFileToMem(i8* %22, i8** %3)
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = sdiv i32 %23, 512
%26 = and i32 %23, 511
%27 = icmp ne i32 %26, 0
%28 = zext i1 %27 to i32
%29 = add nsw i32 %25, %28
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %7
%32 = add nsw i32 %29, -1
%33 = zext i32 %32 to i64
%34 = zext i32 %29 to i64
br label %40
35: ; preds = %40, %7
%36 = load i8*, i8** %3
%37 = call i32 @free(i8* %36)
%38 = load i8*, i8** %8
%39 = call i32 @makeFloopy(i8* %38)
ret i32 0
40: ; preds = %31, %40
%41 = phi i64 [ 0, %31 ], [ %60, %40 ]
%42 = phi i32 [ 512, %31 ], [ %47, %40 ]
%43 = phi i32 [ %16, %31 ], [ %58, %40 ]
%44 = phi i32 [ %23, %31 ], [ %59, %40 ]
%45 = phi i32 [ %19, %31 ], [ %56, %40 ]
%46 = icmp eq i64 %41, %33
%47 = select i1 %46, i32 %44, i32 %42
%48 = trunc i64 %41 to i32
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %13, i32 %43, i32 %45, i32 %47)
%50 = load i8*, i8** %3
%51 = shl nsw i64 %41, 9
%52 = getelementptr inbounds i8, i8* %50, i64 %51
%53 = call i32 @writeToFloopy(i32 %13, i32 %43, i32 %45, i8* %52, i32 %47)
%54 = add nsw i32 %45, 1
%55 = icmp sgt i32 %45, 17
%56 = select i1 %55, i32 1, i32 %54
%57 = zext i1 %55 to i32
%58 = add nsw i32 %43, %57
%59 = add nsw i32 %44, -512
%60 = add nuw nsw i64 %41, 1
%61 = icmp eq i64 %60, %34
br i1 %61, label %35, label %40
}