#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 u_int32_t;
typedef int /*<<< orphan*/ u_char;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int STDIN_FILENO;
int /*<<< orphan*/ *Z_NULL;
int adler32(int, int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ close(int);
long errno;
int /*<<< orphan*/ exit(long);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd = -1;
  u_char buf[(1 << 20)];
  ssize_t size = 0;
  u_int32_t adler = adler32(0L, Z_NULL, 0);
  if (argc == 2) {
    if ((fd = open(argv[1], O_RDONLY)) == -1) {
      long e = errno;
      perror(argv[1]);
      exit(e);
    }
  } else if (argc == 1) {
    fd = STDIN_FILENO;
  } else {
    fprintf(stderr, "Usage: %s FILE_NAME\n", argv[0]);
    exit(1);
  }
  while ((size = read(fd, buf, (1 << 20))) > 0) {
    adler = adler32(adler, buf, size);
  }
  if (size < 0) {
    long e = errno;
    perror(argv[1]);
    exit(e);
  } else {
    printf("%x\n", adler);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x400028(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%rsi
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <adler32@plt>
mov    %eax,-0x40002c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1221 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    121c <main+0x9c>
mov    0x2dd7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x400038(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
mov    -0x400038(%rbp),%rdi
call   10a0 <exit@plt>
jmp    1267 <main+0xe7>
cmpl   $0x1,-0x8(%rbp)
jne    123c <main+0xbc>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1262 <main+0xe2>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdad(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1267 <main+0xe7>
jmp    126c <main+0xec>
mov    -0x14(%rbp),%edi
lea    -0x400020(%rbp),%rsi
mov    $0x100000,%edx
call   1050 <read@plt>
mov    %rax,-0x400028(%rbp)
cmp    $0x0,%rax
jle    12b5 <main+0x135>
mov    -0x40002c(%rbp),%edi
lea    -0x400020(%rbp),%rsi
mov    -0x400028(%rbp),%rdx
call   1070 <adler32@plt>
mov    %eax,-0x40002c(%rbp)
jmp    126c <main+0xec>
cmpq   $0x0,-0x400028(%rbp)
jge    12ed <main+0x16d>
mov    0x2d06(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x400040(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
mov    -0x400040(%rbp),%rdi
call   10a0 <exit@plt>
mov    -0x40002c(%rbp),%esi
lea    0xd1b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x400040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x400008,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e2f(%rip),%rax
mov    (%rax),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1070 <adler32@plt>
mov    %eax,%ebp
cmp    $0x1,%ebx
je     11d1 <main+0x51>
cmp    $0x2,%ebx
jne    126a <main+0xea>
mov    0x8(%r14),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11db <main+0x5b>
jmp    124f <main+0xcf>
mov    0x2de8(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x100000,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    1224 <main+0xa4>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1070 <adler32@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100000,%edx
call   1050 <read@plt>
test   %rax,%rax
jg     1200 <main+0x80>
test   %rax,%rax
js     124f <main+0xcf>
lea    0xde5(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x400008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7a(%rip),%rax
mov    (%rax),%rbx
mov    0x8(%r14),%rdi
call   1090 <perror@plt>
mov    %rbx,%rdi
call   10a0 <exit@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd83(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@Z_NULL = global i32* null
@O_RDONLY = global i32 0
@errno = global i64 0
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s FILE_NAME\0A\00"
@.str.1 = [4 x i8] c"%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1048576 x i32]
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i64 0, i64* %8
%12 = load i32*, i32** @Z_NULL
%13 = call i32 @adler32(i32 0, i32* %12, i64 0)
store i32 %13, i32* %9
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 2
br i1 %15, label %16, label %32
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %31
23: ; preds = %16
%24 = load i64, i64* @errno
store i64 %24, i64* %10
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @perror(i8* %27)
%29 = load i64, i64* %10
%30 = call i32 @exit(i64 %29)
unreachable
31: ; preds = %16
br label %45
32: ; preds = %2
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i32, i32* @STDIN_FILENO
store i32 %36, i32* %6
br label %44
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %41)
%43 = call i32 @exit(i64 1)
unreachable
44: ; preds = %35
br label %45
45: ; preds = %44, %31
br label %46
46: ; preds = %51, %45
%47 = load i32, i32* %6
%48 = getelementptr inbounds [1048576 x i32], [1048576 x i32]* %7, i64 0, i64 0
%49 = call i64 @read(i32 %47, i32* %48, i32 1048576)
store i64 %49, i64* %8
%50 = icmp sgt i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %46
%52 = load i32, i32* %9
%53 = getelementptr inbounds [1048576 x i32], [1048576 x i32]* %7, i64 0, i64 0
%54 = load i64, i64* %8
%55 = call i32 @adler32(i32 %52, i32* %53, i64 %54)
store i32 %55, i32* %9
br label %46
56: ; preds = %46
%57 = load i64, i64* %8
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %67
59: ; preds = %56
%60 = load i64, i64* @errno
store i64 %60, i64* %11
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
%64 = call i32 @perror(i8* %63)
%65 = load i64, i64* %11
%66 = call i32 @exit(i64 %65)
unreachable
67: ; preds = %56
%68 = load i32, i32* %9
%69 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %68)
br label %70
70: ; preds = %67
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
ret i32 0
}

LLVM IR (-O2):
@Z_NULL = global i32* null
@O_RDONLY = global i32 0
@errno = global i64 0
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s FILE_NAME\0A\00"
@.str.1 = [4 x i8] c"%x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1048576 x i32]
%4 = bitcast [1048576 x i32]* %3 to i8*
%5 = load i32*, i32** @Z_NULL
%6 = tail call i32 @adler32(i32 0, i32* %5, i64 0)
switch i32 %0, label %20 [
i32 2, label %7
i32 1, label %18
]
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %25
13: ; preds = %7
%14 = load i64, i64* @errno
%15 = load i8*, i8** %8
%16 = tail call i32 @perror(i8* %15)
%17 = tail call i32 @exit(i64 %14)
unreachable
18: ; preds = %2
%19 = load i32, i32* @STDIN_FILENO
br label %25
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %1
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = tail call i32 @exit(i64 1)
unreachable
25: ; preds = %7, %18
%26 = phi i32 [ %11, %7 ], [ %19, %18 ]
%27 = getelementptr inbounds [1048576 x i32], [1048576 x i32]* %3, i64 0, i64 0
%28 = call i64 @read(i32 %26, i32* %27, i32 1048576)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %25, %30
%31 = phi i64 [ %34, %30 ], [ %28, %25 ]
%32 = phi i32 [ %33, %30 ], [ %6, %25 ]
%33 = call i32 @adler32(i32 %32, i32* %27, i64 %31)
%34 = call i64 @read(i32 %26, i32* %27, i32 1048576)
%35 = icmp sgt i64 %34, 0
br i1 %35, label %30, label %36
36: ; preds = %30, %25
%37 = phi i32 [ %6, %25 ], [ %33, %30 ]
%38 = phi i64 [ %28, %25 ], [ %34, %30 ]
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %36
%41 = load i64, i64* @errno
%42 = getelementptr inbounds i8*, i8** %1, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @perror(i8* %43)
%45 = call i32 @exit(i64 %41)
unreachable
46: ; preds = %36
%47 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%48 = call i32 @close(i32 %26)
ret i32 0
}