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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int slice_sectors(int /*<<< orphan*/ *, size_t, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: img_slice input\n");
    return EXIT_FAILURE;
  }
  size_t size;
  uint8_t *buffer;
  FILE *in;
  in = fopen(argv[1], "rb");
  if (in == NULL) {
    fprintf(stderr, "Failed to open %s : %s \n", argv[1], strerror(errno));
    return EXIT_FAILURE;
  }
  fseek(in, 0, SEEK_END);
  size = ftell(in);
  fseek(in, 0, SEEK_SET);
  size -= ftell(in);
  int ret = EXIT_SUCCESS;
  buffer = (uint8_t *)malloc(size);
  if (fread(buffer, 1, size, in) != size) {
    fprintf(stderr, "Failed to read %zd bytes from %s : %s \n", size, argv[1],
            strerror(errno));
    ret = EXIT_FAILURE;
  }
  fclose(in);
  if (ret == EXIT_SUCCESS) {
    ret = slice_sectors(buffer, size, argv[1]);
  }
  free(buffer);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e8 <main+0x48>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1377 <main+0x1d7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe21(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    125a <main+0xba>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1377 <main+0x1d7>
mov    -0x28(%rbp),%rdi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x18(%rbp),%rax
je     1335 <main+0x195>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    %rax,%r8
lea    0xd12(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    0x2c70(%rip),%rcx
cmp    (%rcx),%eax
jne    1368 <main+0x1c8>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
call   1060 <slice_sectors@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    12aa <main+0x10a>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0xe56(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12c3 <main+0x123>
mov    %rax,%rbx
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
sub    %rax,%r15
mov    0x2da2(%rip),%r13
mov    0x0(%r13),%ebp
mov    %r15,%rdi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
je     1281 <main+0xe1>
mov    0x2d94(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%r14),%r13
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdd5(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
mov    %r13,%rcx
mov    0x2d4a(%rip),%r13
mov    %rax,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    0x0(%r13),%ebp
jne    12a0 <main+0x100>
mov    0x8(%r14),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <slice_sectors@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <free@plt>
jmp    12fd <main+0x15d>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xd46(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12f4 <main+0x154>
mov    0x2d0e(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%r14),%rbp
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd36(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Usage: img_slice input\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [25 x i8] c"Failed to open %s : %s \0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [40 x i8] c"Failed to read %zd bytes from %s : %s \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %3
br label %82
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %32
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %27, i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
store i32 %31, i32* %3
br label %82
32: ; preds = %16
%33 = load i32*, i32** %8
%34 = load i32, i32* @SEEK_END
%35 = call i32 @fseek(i32* %33, i32 0, i32 %34)
%36 = load i32*, i32** %8
%37 = call i64 @ftell(i32* %36)
store i64 %37, i64* %6
%38 = load i32*, i32** %8
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @fseek(i32* %38, i32 0, i32 %39)
%41 = load i32*, i32** %8
%42 = call i64 @ftell(i32* %41)
%43 = load i64, i64* %6
%44 = sub i64 %43, %42
store i64 %44, i64* %6
%45 = load i32, i32* @EXIT_SUCCESS
store i32 %45, i32* %9
%46 = load i64, i64* %6
%47 = call i64 @malloc(i64 %46)
%48 = inttoptr i64 %47 to i32*
store i32* %48, i32** %7
%49 = load i32*, i32** %7
%50 = load i64, i64* %6
%51 = load i32*, i32** %8
%52 = call i64 @fread(i32* %49, i32 1, i64 %50, i32* %51)
%53 = load i64, i64* %6
%54 = icmp ne i64 %52, %53
br i1 %54, label %55, label %65
55: ; preds = %32
%56 = load i32, i32* @stderr
%57 = load i64, i64* %6
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i64 %57, i8* %60, i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
store i32 %64, i32* %9
br label %65
65: ; preds = %55, %32
%66 = load i32*, i32** %8
%67 = call i32 @fclose(i32* %66)
%68 = load i32, i32* %9
%69 = load i32, i32* @EXIT_SUCCESS
%70 = icmp eq i32 %68, %69
br i1 %70, label %71, label %78
71: ; preds = %65
%72 = load i32*, i32** %7
%73 = load i64, i64* %6
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = call i32 @slice_sectors(i32* %72, i64 %73, i8* %76)
store i32 %77, i32* %9
br label %78
78: ; preds = %71, %65
%79 = load i32*, i32** %7
%80 = call i32 @free(i32* %79)
%81 = load i32, i32* %9
store i32 %81, i32* %3
br label %82
82: ; preds = %78, %23, %12
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Usage: img_slice input\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [25 x i8] c"Failed to open %s : %s \0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.3 = [40 x i8] c"Failed to read %zd bytes from %s : %s \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
br label %51
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %9
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
br label %51
20: ; preds = %8
%21 = load i32, i32* @SEEK_END
%22 = tail call i32 @fseek(i32* %11, i32 0, i32 %21)
%23 = tail call i64 @ftell(i32* %11)
%24 = load i32, i32* @SEEK_SET
%25 = tail call i32 @fseek(i32* %11, i32 0, i32 %24)
%26 = tail call i64 @ftell(i32* %11)
%27 = sub i64 %23, %26
%28 = load i32, i32* @EXIT_SUCCESS
%29 = tail call i64 @malloc(i64 %27)
%30 = inttoptr i64 %29 to i32*
%31 = tail call i64 @fread(i32* %30, i32 1, i64 %27, i32* %11)
%32 = icmp eq i64 %31, %27
br i1 %32, label %40, label %33
33: ; preds = %20
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %9
%36 = load i32, i32* @errno
%37 = tail call i8* @strerror(i32 %36)
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i64 %27, i8* %35, i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
br label %40
40: ; preds = %33, %20
%41 = phi i32 [ %39, %33 ], [ %28, %20 ]
%42 = tail call i32 @fclose(i32* %11)
%43 = load i32, i32* @EXIT_SUCCESS
%44 = icmp eq i32 %41, %43
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i8*, i8** %9
%47 = tail call i32 @slice_sectors(i32* %30, i64 %27, i8* %46)
br label %48
48: ; preds = %45, %40
%49 = phi i32 [ %47, %45 ], [ %41, %40 ]
%50 = tail call i32 @free(i32* %30)
br label %51
51: ; preds = %13, %48, %4
%52 = phi i32 [ %7, %4 ], [ %19, %13 ], [ %49, %48 ]
ret i32 %52
}