#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 */

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *mapdev(char *, unsigned long, unsigned long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  unsigned long offset, size, i;
  char *addr1, *addr2;
  if (argc != 5) {
    fprintf(stderr, "Usage: mapcmp dev1 dev2 offset pages\n");
    exit(1);
  }
  offset = strtoul(argv[3], NULL, 16);
  size = atoi(argv[4]) * 4096;
  printf("Offset is 0x%lx\n", offset);
  addr1 = mapdev(argv[1], offset, size);
  addr2 = mapdev(argv[2], offset, size);
  printf("Comparing...");
  fflush(stdout);
  for (i = 0; i < size; i++)
    if (*addr1++ != *addr2++) {
      printf("areas differ at byte %ld\n", i);
      exit(0);
    }
  printf("areas are identical.\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     11af <main+0x3f>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1080 <atoi@plt>
shl    $0xc,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <mapdev@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <mapdev@plt>
mov    %rax,-0x38(%rbp)
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    12b0 <main+0x140>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movsbl (%rax),%eax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     129a <main+0x12a>
mov    -0x28(%rbp),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    129f <main+0x12f>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1247 <main+0xd7>
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x5,%edi
jne    1246 <main+0xc6>
mov    %rsi,%r15
mov    0x18(%rsi),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %rax,%rbp
mov    0x20(%r15),%rdi
call   1090 <atoi@plt>
mov    %eax,%r12d
shl    $0xc,%eax
movslq %eax,%r14
lea    0xe66(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%r15),%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1040 <mapdev@plt>
mov    %rax,%rbx
mov    0x10(%r15),%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1040 <mapdev@plt>
mov    %rax,%rbp
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
test   %r12d,%r12d
je     1233 <main+0xb3>
cmp    $0x2,%r14
mov    $0x1,%eax
cmovae %r14,%rax
xor    %esi,%esi
xchg   %ax,%ax
movzbl (%rbx,%rsi,1),%ecx
cmp    0x0(%rbp,%rsi,1),%cl
jne    1265 <main+0xe5>
add    $0x1,%rsi
cmp    %rsi,%rax
jne    1220 <main+0xa0>
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [38 x i8] c"Usage: mapcmp dev1 dev2 offset pages\0A\00"
@.str.1 = [17 x i8] c"Offset is 0x%lx\0A\00"
@.str.2 = [13 x i8] c"Comparing...\00"
@stdout = global i32 0
@.str.3 = [26 x i8] c"areas differ at byte %ld\0A\00"
@.str.4 = [22 x i8] c"areas are identical.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 5
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 3
%20 = load i8*, i8** %19
%21 = call i64 @strtoul(i8* %20, i32* null, i32 16)
store i64 %21, i64* %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 4
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = mul nsw i32 %25, 4096
%27 = sext i32 %26 to i64
store i64 %27, i64* %7
%28 = load i64, i64* %6
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %28)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i64, i64* %6
%34 = load i64, i64* %7
%35 = call i8* @mapdev(i8* %32, i64 %33, i64 %34)
store i8* %35, i8** %9
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = load i64, i64* %6
%40 = load i64, i64* %7
%41 = call i8* @mapdev(i8* %38, i64 %39, i64 %40)
store i8* %41, i8** %10
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @stdout
%44 = call i32 @fflush(i32 %43)
store i64 0, i64* %8
br label %45
45: ; preds = %64, %17
%46 = load i64, i64* %8
%47 = load i64, i64* %7
%48 = icmp ult i64 %46, %47
br i1 %48, label %49, label %67
49: ; preds = %45
%50 = load i8*, i8** %9
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %9
%52 = load i8, i8* %50
%53 = sext i8 %52 to i32
%54 = load i8*, i8** %10
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %10
%56 = load i8, i8* %54
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %53, %57
br i1 %58, label %59, label %63
59: ; preds = %49
%60 = load i64, i64* %8
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %60)
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %49
br label %64
64: ; preds = %63
%65 = load i64, i64* %8
%66 = add i64 %65, 1
store i64 %66, i64* %8
br label %45
67: ; preds = %45
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%69 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [38 x i8] c"Usage: mapcmp dev1 dev2 offset pages\0A\00"
@.str.1 = [17 x i8] c"Offset is 0x%lx\0A\00"
@.str.2 = [13 x i8] c"Comparing...\00"
@stdout = global i32 0
@.str.3 = [26 x i8] c"areas differ at byte %ld\0A\00"
@str = [21 x i8] c"areas are identical.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 5
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 3
%10 = load i8*, i8** %9
%11 = tail call i64 @strtoul(i8* nocapture %10, i32* null, i32 16)
%12 = getelementptr inbounds i8*, i8** %1, i64 4
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = shl nsw i32 %14, 12
%16 = sext i32 %15 to i64
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %11)
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i8* @mapdev(i8* %19, i64 %11, i64 %16)
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i8* @mapdev(i8* %22, i64 %11, i64 %16)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @stdout
%26 = tail call i32 @fflush(i32 %25)
%27 = icmp eq i32 %14, 0
br i1 %27, label %45, label %28
28: ; preds = %8
%29 = call i64 @llvm.umax.i64(i64 %16, i64 1)
br label %30
30: ; preds = %28, %40
%31 = phi i8* [ %41, %40 ], [ %23, %28 ]
%32 = phi i8* [ %42, %40 ], [ %20, %28 ]
%33 = phi i64 [ %43, %40 ], [ 0, %28 ]
%34 = load i8, i8* %32
%35 = load i8, i8* %31
%36 = icmp eq i8 %34, %35
br i1 %36, label %40, label %37
37: ; preds = %30
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %33)
%39 = tail call i32 @exit(i32 0)
unreachable
40: ; preds = %30
%41 = getelementptr inbounds i8, i8* %31, i64 1
%42 = getelementptr inbounds i8, i8* %32, i64 1
%43 = add nuw i64 %33, 1
%44 = icmp eq i64 %43, %29
br i1 %44, label %45, label %30
45: ; preds = %40, %8
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%47 = tail call i32 @exit(i32 0)
unreachable
}