#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 uint16_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, int *, int);
int main(int argc, char *argv[]) {
  int fd = -1;
  uint16_t shutter_count = 0;
  uint16_t int_buf = 0;
  printf("Canon 20D Shutter Count\n");
  printf("by: Daria Juniper\n\n");
  if (argc != 2) {
    printf("usage: %s <IMG_XXXX.CR2>\n\n", argv[0]);
    exit(-1);
  }
  if (fd = open(argv[1], O_RDONLY)) {
    if (lseek(fd, 0x95D, 0) >= 0) {
      if (read(fd, &int_buf, sizeof(uint16_t))) {
        shutter_count = (int_buf >> 8) | (int_buf << 8);
        printf("Shutter Count: %hu\n\n", shutter_count);
        close(fd);
      } else {
        printf("fatal: unable to read data from file.\n\n");
        close(fd);
        exit(-1);
      }
    } else {
      printf("fatal: unable to seek to 0x95D, invalid file?\n\n");
      close(fd);
      exit(-1);
    }
  } else {
    printf("fatal: failed to open %s.\n\n", argv[1]);
    exit(-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
je     11d0 <main+0x70>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     129f <main+0x13f>
mov    -0x14(%rbp),%edi
mov    $0x95d,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jl     127a <main+0x11a>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
je     1255 <main+0xf5>
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
mov    -0x1c(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1275 <main+0x115>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    129a <main+0x13a>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    12bf <main+0x15f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
movl   $0x0,0x4(%rsp)
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebx
jne    120c <main+0x9c>
mov    0x8(%r14),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
je     1218 <main+0xa8>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x95d,%esi
xor    %edx,%edx
call   1050 <lseek@plt>
test   %rax,%rax
js     1234 <main+0xc4>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <read@plt>
test   %rax,%rax
je     123d <main+0xcd>
mov    0x4(%rsp),%esi
mov    %esi,%eax
sar    $0x8,%eax
shl    $0x8,%esi
or     %eax,%esi
lea    0xe27(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%r14),%rsi
lea    0xdea(%rip),%rdi
jmp    1223 <main+0xb3>
mov    0x8(%r14),%rsi
lea    0xe0d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xe3c(%rip),%rdi
jmp    1244 <main+0xd4>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Canon 20D Shutter Count\0A\00"
@.str.1 = [20 x i8] c"by: Daria Juniper\0A\0A\00"
@.str.2 = [27 x i8] c"usage: %s <IMG_XXXX.CR2>\0A\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [21 x i8] c"Shutter Count: %hu\0A\0A\00"
@.str.4 = [40 x i8] c"fatal: unable to read data from file.\0A\0A\00"
@.str.5 = [48 x i8] c"fatal: unable to seek to 0x95D, invalid file?\0A\0A\00"
@.str.6 = [28 x i8] c"fatal: failed to open %s.\0A\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %56
26: ; preds = %19
%27 = load i32, i32* %6
%28 = call i64 @lseek(i32 %27, i32 2397, i32 0)
%29 = icmp sge i64 %28, 0
br i1 %29, label %30, label %50
30: ; preds = %26
%31 = load i32, i32* %6
%32 = call i64 @read(i32 %31, i32* %8, i32 4)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %44
34: ; preds = %30
%35 = load i32, i32* %8
%36 = ashr i32 %35, 8
%37 = load i32, i32* %8
%38 = shl i32 %37, 8
%39 = or i32 %36, %38
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
br label %49
44: ; preds = %30
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %34
br label %55
50: ; preds = %26
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %49
br label %62
56: ; preds = %19
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %59)
%61 = call i32 @exit(i32 -1)
unreachable
62: ; preds = %55
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [27 x i8] c"usage: %s <IMG_XXXX.CR2>\0A\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [21 x i8] c"Shutter Count: %hu\0A\0A\00"
@.str.6 = [28 x i8] c"fatal: failed to open %s.\0A\0A\00"
@str = [24 x i8] c"Canon 20D Shutter Count\00"
@str.7 = [19 x i8] c"by: Daria Juniper\0A\00"
@str.8 = [47 x i8] c"fatal: unable to seek to 0x95D, invalid file?\0A\00"
@str.9 = [39 x i8] c"fatal: unable to read data from file.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %39, label %18
18: ; preds = %12
%19 = tail call i64 @lseek(i32 %16, i32 2397, i32 0)
%20 = icmp sgt i64 %19, -1
br i1 %20, label %21, label %35
21: ; preds = %18
%22 = call i64 @read(i32 %16, i32* %3, i32 4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = load i32, i32* %3
%26 = ashr i32 %25, 8
%27 = shl i32 %25, 8
%28 = or i32 %26, %27
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = tail call i32 @close(i32 %16)
ret i32 0
31: ; preds = %21
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.9, i64 0, i64 0))
%33 = tail call i32 @close(i32 %16)
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %18
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.8, i64 0, i64 0))
%37 = tail call i32 @close(i32 %16)
%38 = tail call i32 @exit(i32 -1)
unreachable
39: ; preds = %12
%40 = load i8*, i8** %13
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %40)
%42 = tail call i32 @exit(i32 -1)
unreachable
}