#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 char *caddr_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *mmap(char *, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd, offset;
  char *data;
  struct stat sbuf;
  if (argc != 2) {
    fprintf(stderr, "usage: mmapdemo offset\n");
    exit(1);
  }
  if ((fd = open("mmapdemo.c", O_RDONLY)) == -1) {
    perror("open");
    exit(1);
  }
  if (stat("mmapdemo.c", &sbuf) == -1) {
    perror("stat");
    exit(1);
  }
  offset = atoi(argv[1]);
  if (offset < 0 || offset > sbuf.st_size - 1) {
    fprintf(stderr, "mmapdemo: offset must be in the range 0-%d\n",
            sbuf.st_size - 1);
    exit(1);
  }
  data = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (data == (caddr_t)(-1)) {
    perror("mmap");
    exit(1);
  }
  printf("byte at offset %d is '%c'\n", offset, data[offset]);
  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)
cmpl   $0x2,-0x8(%rbp)
je     11c1 <main+0x41>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11f8 <main+0x78>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe19(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    1227 <main+0xa7>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1252 <main+0xd2>
mov    -0x18(%rbp),%eax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1279 <main+0xf9>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
sub    $0x1,%edx
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%esi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
mov    0x2d5c(%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    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    12c9 <main+0x149>
lea    0xd9f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
lea    0xd83(%rip),%rdi
movsbl (%rax,%rcx,1),%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1221 <main+0xa1>
mov    %rsi,%rbp
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xc2>
mov    %eax,%ebx
lea    0xe62(%rip),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xcb>
mov    0x8(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    (%rsp),%eax
test   %ebp,%ebp
js     1254 <main+0xd4>
cmp    %eax,%ebp
jge    1254 <main+0xd4>
mov    0x2e14(%rip),%rcx
mov    (%rcx),%edx
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     127a <main+0xfa>
mov    %ebp,%ecx
movsbl (%rax,%rcx,1),%edx
lea    0xe4d(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdda(%rip),%rdi
jmp    1281 <main+0x101>
lea    0xdd6(%rip),%rdi
jmp    1281 <main+0x101>
mov    0x2d6d(%rip),%rcx
mov    (%rcx),%edi
add    $0xffffffff,%eax
lea    0xdc6(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdd8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage: mmapdemo offset\0A\00"
@.str.1 = [11 x i8] c"mmapdemo.c\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [44 x i8] c"mmapdemo: offset must be in the range 0-%d\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [5 x i8] c"mmap\00"
@.str.6 = [27 x i8] c"byte at offset %d is '%c'\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 i8*
%9 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 %18, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %9)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %29
%37 = load i32, i32* %7
%38 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%39 = load i32, i32* %38
%40 = sub nsw i32 %39, 1
%41 = icmp sgt i32 %37, %40
br i1 %41, label %42, label %49
42: ; preds = %36, %29
%43 = load i32, i32* @stderr
%44 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%45 = load i32, i32* %44
%46 = sub nsw i32 %45, 1
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %36
%50 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%51 = load i32, i32* %50
%52 = load i32, i32* @PROT_READ
%53 = load i32, i32* @MAP_SHARED
%54 = load i32, i32* %6
%55 = call i8* @mmap(i8* null, i32 %51, i32 %52, i32 %53, i32 %54, i32 0)
store i8* %55, i8** %8
%56 = load i8*, i8** %8
%57 = icmp eq i8* %56, inttoptr (i64 -1 to i8*)
br i1 %57, label %58, label %61
58: ; preds = %49
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %49
%62 = load i32, i32* %7
%63 = load i8*, i8** %8
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8, i8* %66
%68 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %62, i8 signext %67)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"usage: mmapdemo offset\0A\00"
@.str.1 = [11 x i8] c"mmapdemo.c\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [44 x i8] c"mmapdemo: offset must be in the range 0-%d\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [5 x i8] c"mmap\00"
@.str.6 = [27 x i8] c"byte at offset %d is '%c'\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = tail call i32 @atoi(i8* %25)
%27 = icmp sgt i32 %26, -1
%28 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%29 = load i32, i32* %28
%30 = icmp slt i32 %26, %29
%31 = select i1 %27, i1 %30, i1 false
br i1 %31, label %37, label %32
32: ; preds = %23
%33 = load i32, i32* @stderr
%34 = add nsw i32 %29, -1
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %23
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @MAP_SHARED
%40 = tail call i8* @mmap(i8* null, i32 %29, i32 %38, i32 %39, i32 %12, i32 0)
%41 = icmp eq i8* %40, inttoptr (i64 -1 to i8*)
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%44 = tail call i32 @exit(i32 1)
unreachable
45: ; preds = %37
%46 = zext i32 %26 to i64
%47 = getelementptr inbounds i8, i8* %40, i64 %46
%48 = load i8, i8* %47
%49 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %26, i8 signext %48)
ret i32 0
}