#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 /*<<< orphan*/ st_size;
};

/* Variables and functions */
unsigned int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int checksum(unsigned int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
unsigned int *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
                   int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int fd, bs = 0;
  unsigned *data;
  struct stat stat;
  if (argc != 2 && argc != 3) {
    fprintf(stderr, "Usage: %s <filename> byteswap\n", argv[0]);
    return 1;
  }
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    perror(argv[1]);
    return 1;
  }
  if (fstat(fd, &stat) < 0) {
    perror(argv[1]);
    return 1;
  }
  data = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (data == MAP_FAILED) {
    perror(argv[1]);
    return 1;
  }
  fprintf(stdout, "%08x", checksum(data, stat.st_size, (argc == 3)));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bb <main+0x5b>
cmpl   $0x3,-0x8(%rbp)
je     11bb <main+0x5b>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <main+0x159>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f6 <main+0x96>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <main+0x159>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    1225 <main+0xc5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <main+0x159>
mov    -0x28(%rbp),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    0x2db0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d5f(%rip),%rcx
cmp    (%rcx),%rax
jne    127b <main+0x11b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <main+0x159>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
cmpl   $0x3,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edx
call   1040 <checksum@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    1216 <main+0xb6>
mov    %edi,%ebp
mov    0x8(%r14),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1246 <main+0xe6>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     1246 <main+0xe6>
mov    0x8(%rsp),%r15d
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
je     1246 <main+0xe6>
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%ebx
xor    %edx,%edx
cmp    $0x3,%ebp
sete   %dl
mov    %rax,%rdi
mov    %r15d,%esi
call   1040 <checksum@plt>
lea    0xe16(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1236 <main+0xd6>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdd7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r14),%rdi
call   1070 <perror@plt>
jmp    1230 <main+0xd0>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"Usage: %s <filename> byteswap\0A\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@stdout = global i32 0
@.str.1 = [5 x i8] c"%08x\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 %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %21
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %67
21: ; preds = %12, %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %21
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @perror(i8* %31)
store i32 1, i32* %3
br label %67
33: ; preds = %21
%34 = load i32, i32* %6
%35 = call i64 @fstat(i32 %34, %struct.stat* %9)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @perror(i8* %40)
store i32 1, i32* %3
br label %67
42: ; preds = %33
%43 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @PROT_READ
%46 = load i32, i32* @MAP_SHARED
%47 = load i32, i32* %6
%48 = call i32* @mmap(i32* null, i32 %44, i32 %45, i32 %46, i32 %47, i32 0)
store i32* %48, i32** %8
%49 = load i32*, i32** %8
%50 = load i32*, i32** @MAP_FAILED
%51 = icmp eq i32* %49, %50
br i1 %51, label %52, label %57
52: ; preds = %42
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @perror(i8* %55)
store i32 1, i32* %3
br label %67
57: ; preds = %42
%58 = load i32, i32* @stdout
%59 = load i32*, i32** %8
%60 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%61 = load i32, i32* %60
%62 = load i32, i32* %4
%63 = icmp eq i32 %62, 3
%64 = zext i1 %63 to i32
%65 = call i32 @checksum(i32* %59, i32 %61, i32 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %65)
store i32 0, i32* %3
br label %67
67: ; preds = %57, %52, %37, %28, %15
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"Usage: %s <filename> byteswap\0A\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@stdout = global i32 0
@.str.1 = [5 x i8] c"%08x\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 2
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %43
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i8*, i8** %12
%19 = tail call i32 @perror(i8* %18)
br label %43
20: ; preds = %11
%21 = call i64 @fstat(i32 %15, %struct.stat* %3)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i8*, i8** %12
%25 = tail call i32 @perror(i8* %24)
br label %43
26: ; preds = %20
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i32* @mmap(i32* null, i32 %28, i32 %29, i32 %30, i32 %15, i32 0)
%32 = load i32*, i32** @MAP_FAILED
%33 = icmp eq i32* %31, %32
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i8*, i8** %12
%36 = tail call i32 @perror(i8* %35)
br label %43
37: ; preds = %26
%38 = load i32, i32* @stdout
%39 = icmp eq i32 %0, 3
%40 = zext i1 %39 to i32
%41 = tail call i32 @checksum(i32* %31, i32 %28, i32 %40)
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %37, %34, %23, %17, %7
%44 = phi i32 [ 1, %7 ], [ 1, %17 ], [ 1, %23 ], [ 1, %34 ], [ 0, %37 ]
ret i32 %44
}