#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int find_unused_bytes(unsigned char *, int);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
int main(int argc, char *argv[]) {
  int i = 0;
  FILE *infile;
  struct stat infile_stats;
  unsigned char *buff;
  int unused_count = 0;
  int total_unused_count = 0;
  size_t sz = 0;
  int debug = 1;
  if (argc < 2)
    return 1;
  for (i = 1; i < argc; i++) {
    unused_count = 0;
    if ((infile = fopen(argv[i], "rb")) == NULL) {
      printf("[-] unable to open file %s\n", argv[i]);
      return 1;
    }
    if (!(stat(argv[i], &infile_stats) == 0)) {
      printf("[-] unable to stat file %s\n", argv[i]);
      return 1;
    }
    if (debug)
      printf("[+] trying to allocate %d bytes for %s\n",
             (int)infile_stats.st_size, argv[i]);
    if ((buff = malloc(sizeof(unsigned char) * infile_stats.st_size)) == NULL) {
      printf("[-] malloc failed\n");
      return 1;
    }
    sz = fread(buff, 1, infile_stats.st_size, infile);
    unused_count = find_unused_bytes(buff, infile_stats.st_size);
    if (unused_count >= 0) {
      printf("[+] found %d unused bytes in file %s\n", unused_count, argv[i]);
      total_unused_count += unused_count;
    } else {
      printf("[-] check_unused failed for file %s\n", argv[i]);
    }
    fclose(infile);
    free(buff);
  }
  printf("\n");
  printf("[+] total number of unused bytes: %d\n", total_unused_count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x1,-0x44(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d0 <main+0x50>
movl   $0x1,-0x4(%rbp)
jmp    1396 <main+0x216>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1370 <main+0x1f0>
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe03(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1236 <main+0xb6>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1396 <main+0x216>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     127b <main+0xfb>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1396 <main+0x216>
cmpl   $0x0,-0x44(%rbp)
je     12a2 <main+0x122>
mov    -0x28(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movslq -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12d9 <main+0x159>
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1396 <main+0x216>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1090 <find_unused_bytes@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     1336 <main+0x1b6>
mov    -0x34(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1350 <main+0x1d0>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d7 <main+0x57>
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%esi
lea    0xd39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x1,%ebp
cmp    $0x2,%edi
jl     1304 <main+0x164>
mov    %rsi,%rbx
mov    %edi,%eax
mov    %rax,0x10(%rsp)
mov    $0x1,%r14d
xor    %r15d,%r15d
jmp    1210 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0xea2(%rip),%rdi
mov    %rdx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r13,%rdi
call   1070 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x1,%r14
cmp    %r14,0x10(%rsp)
je     12b7 <main+0x117>
mov    (%rbx,%r14,8),%rdi
lea    0xde5(%rip),%rsi
call   10c0 <fopen@plt>
mov    (%rbx,%r14,8),%rsi
test   %rax,%rax
je     12d6 <main+0x136>
mov    %rax,%r13
mov    %rsi,%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
test   %rax,%rax
jne    12df <main+0x13f>
mov    0x8(%rsp),%esi
mov    (%rbx,%r14,8),%rdx
lea    0xde6(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12f3 <main+0x153>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r13,%rcx
call   1060 <fread@plt>
mov    0x8(%rsp),%esi
mov    %r12,%rdi
call   10b0 <find_unused_bytes@plt>
mov    (%rbx,%r14,8),%rdx
test   %eax,%eax
js     11e0 <main+0x40>
mov    %eax,%ebp
lea    0xdbd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
add    %ebp,%r15d
jmp    11f1 <main+0x51>
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0xde6(%rip),%rdi
xor    %ebp,%ebp
mov    %r15d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1304 <main+0x164>
lea    0xd26(%rip),%rdi
jmp    12ea <main+0x14a>
mov    (%rbx,%r14,8),%rsi
lea    0xd35(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    12ff <main+0x15f>
lea    0xdda(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"[-] unable to open file %s\0A\00"
@.str.2 = [28 x i8] c"[-] unable to stat file %s\0A\00"
@.str.3 = [40 x i8] c"[+] trying to allocate %d bytes for %s\0A\00"
@.str.4 = [19 x i8] c"[-] malloc failed\0A\00"
@.str.5 = [38 x i8] c"[+] found %d unused bytes in file %s\0A\00"
@.str.6 = [37 x i8] c"[-] check_unused failed for file %s\0A\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [38 x i8] c"[+] total number of unused bytes: %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 %struct.stat
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %10
store i32 0, i32* %11
store i64 0, i64* %12
store i32 1, i32* %13
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 1, i32* %3
br label %116
17: ; preds = %2
store i32 1, i32* %6
br label %18
18: ; preds = %109, %17
%19 = load i32, i32* %6
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %112
22: ; preds = %18
store i32 0, i32* %10
%23 = load i8**, i8*** %5
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
%27 = load i8*, i8** %26
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %28, i32** %7
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %37
30: ; preds = %22
%31 = load i8**, i8*** %5
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %35)
store i32 1, i32* %3
br label %116
37: ; preds = %22
%38 = load i8**, i8*** %5
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = call i64 @stat(i8* %42, %struct.stat* %8)
%44 = icmp eq i64 %43, 0
br i1 %44, label %52, label %45
45: ; preds = %37
%46 = load i8**, i8*** %5
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %50)
store i32 1, i32* %3
br label %116
52: ; preds = %37
%53 = load i32, i32* %13
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %64
55: ; preds = %52
%56 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%57 = load i32, i32* %56
%58 = load i8**, i8*** %5
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
%62 = load i8*, i8** %61
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %57, i8* %62)
br label %64
64: ; preds = %55, %52
%65 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%66 = load i32, i32* %65
%67 = sext i32 %66 to i64
%68 = mul i64 1, %67
%69 = trunc i64 %68 to i32
%70 = call i8* @malloc(i32 %69)
store i8* %70, i8** %9
%71 = icmp eq i8* %70, null
br i1 %71, label %72, label %74
72: ; preds = %64
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %116
74: ; preds = %64
%75 = load i8*, i8** %9
%76 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%77 = load i32, i32* %76
%78 = load i32*, i32** %7
%79 = call i64 @fread(i8* %75, i32 1, i32 %77, i32* %78)
store i64 %79, i64* %12
%80 = load i8*, i8** %9
%81 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%82 = load i32, i32* %81
%83 = call i32 @find_unused_bytes(i8* %80, i32 %82)
store i32 %83, i32* %10
%84 = load i32, i32* %10
%85 = icmp sge i32 %84, 0
br i1 %85, label %86, label %97
86: ; preds = %74
%87 = load i32, i32* %10
%88 = load i8**, i8*** %5
%89 = load i32, i32* %6
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i8*, i8** %88, i64 %90
%92 = load i8*, i8** %91
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %87, i8* %92)
%94 = load i32, i32* %10
%95 = load i32, i32* %11
%96 = add nsw i32 %95, %94
store i32 %96, i32* %11
br label %104
97: ; preds = %74
%98 = load i8**, i8*** %5
%99 = load i32, i32* %6
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8*, i8** %98, i64 %100
%102 = load i8*, i8** %101
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %102)
br label %104
104: ; preds = %97, %86
%105 = load i32*, i32** %7
%106 = call i32 @fclose(i32* %105)
%107 = load i8*, i8** %9
%108 = call i32 @free(i8* %107)
br label %109
109: ; preds = %104
%110 = load i32, i32* %6
%111 = add nsw i32 %110, 1
store i32 %111, i32* %6
br label %18
112: ; preds = %18
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%114 = load i32, i32* %11
%115 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0), i32 %114)
store i32 0, i32* %3
br label %116
116: ; preds = %112, %72, %45, %30, %16
%117 = load i32, i32* %3
ret i32 %117
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"[-] unable to open file %s\0A\00"
@.str.2 = [28 x i8] c"[-] unable to stat file %s\0A\00"
@.str.3 = [40 x i8] c"[+] trying to allocate %d bytes for %s\0A\00"
@.str.5 = [38 x i8] c"[+] found %d unused bytes in file %s\0A\00"
@.str.6 = [37 x i8] c"[-] check_unused failed for file %s\0A\00"
@.str.8 = [38 x i8] c"[+] total number of unused bytes: %d\0A\00"
@str = [18 x i8] c"[-] malloc failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %54, label %6
6: ; preds = %2
%7 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %6, %45
%10 = phi i64 [ 1, %6 ], [ %49, %45 ]
%11 = phi i32 [ 0, %6 ], [ %46, %45 ]
%12 = getelementptr inbounds i8*, i8** %1, i64 %10
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32* %14, null
%16 = load i8*, i8** %12
br i1 %15, label %17, label %19
17: ; preds = %9
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %54
19: ; preds = %9
%20 = call i64 @stat(i8* %16, %struct.stat* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i8*, i8** %12
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %54
25: ; preds = %19
%26 = load i32, i32* %7
%27 = load i8*, i8** %12
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %26, i8* %27)
%29 = load i32, i32* %7
%30 = call i8* @malloc(i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %54
34: ; preds = %25
%35 = call i64 @fread(i8* %30, i32 1, i32 %29, i32* %14)
%36 = load i32, i32* %7
%37 = call i32 @find_unused_bytes(i8* %30, i32 %36)
%38 = icmp sgt i32 %37, -1
%39 = load i8*, i8** %12
br i1 %38, label %40, label %43
40: ; preds = %34
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %37, i8* %39)
%42 = add nsw i32 %37, %11
br label %45
43: ; preds = %34
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %39)
br label %45
45: ; preds = %43, %40
%46 = phi i32 [ %42, %40 ], [ %11, %43 ]
%47 = call i32 @fclose(i32* %14)
%48 = call i32 @free(i8* %30)
%49 = add nuw nsw i64 %10, 1
%50 = icmp eq i64 %49, %8
br i1 %50, label %51, label %9
51: ; preds = %45
%52 = call i32 @putchar(i32 10)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.8, i64 0, i64 0), i32 %46)
br label %54
54: ; preds = %2, %51, %32, %22, %17
%55 = phi i32 [ 1, %17 ], [ 1, %32 ], [ 1, %22 ], [ 0, %51 ], [ 1, %2 ]
ret i32 %55
}