#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 /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpagesize();
int open(char *, int /*<<< orphan*/);
int posix_memalign(void **, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, void *, int);
int main(int argc, char *argv[]) {
  int fd, rc = 0, nbytes, pagesize;
  void *buffer;
  char *filename = "/dev/mycdrv";
  pagesize = getpagesize();
  nbytes = pagesize;
  if (argc > 1)
    filename = argv[1];
  if (argc > 2)
    nbytes = atoi(argv[2]) * pagesize;
  rc = posix_memalign(&buffer, pagesize, nbytes);
  printf("rc=%d, buffer=%p, mod pagesize = %ld\n", rc, buffer,
         (unsigned long)buffer % pagesize);
  fd = open(filename, O_RDONLY);
  printf("opened file: %s,  with file descriptor = %d\n", filename, fd);
  rc = read(fd, buffer, nbytes);
  printf("read %d bytes which were:\n%s\n", rc, (char *)buffer);
  close(fd);
  exit(EXIT_SUCCESS);
}


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)
movl   $0x0,-0x18(%rbp)
lea    0xe5c(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ce <main+0x4e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11ec <main+0x6c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
imul   -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    -0x28(%rbp),%rdi
call   10a0 <posix_memalign@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    %rdx,%rcx
mov    -0x38(%rbp),%rdx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
xor    %eax,%eax
call   1080 <getpagesize@plt>
mov    %eax,%ebp
cmp    $0x2,%ebx
jl     11a5 <main+0x25>
mov    0x8(%r15),%r14
jne    11b1 <main+0x31>
mov    %ebp,%r15d
jmp    11c1 <main+0x41>
lea    0xe54(%rip),%r14
mov    %ebp,%r15d
jmp    11c1 <main+0x41>
mov    0x10(%r15),%rdi
call   1070 <atoi@plt>
mov    %eax,%r15d
imul   %ebp,%r15d
mov    %rsp,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
call   10a0 <posix_memalign@plt>
mov    %eax,%esi
mov    (%rsp),%rbx
movslq %ebp,%rcx
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
jne    11ed <main+0x6d>
mov    %ebx,%eax
xor    %edx,%edx
div    %ebp
mov    %edx,%ecx
jmp    11f8 <main+0x78>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
mov    %rdx,%rcx
lea    0xe0d(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
lea    0xe0f(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rsp),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
call   1050 <read@plt>
mov    (%rsp),%rdx
lea    0xe17(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/mycdrv\00"
@.str.1 = [38 x i8] c"rc=%d, buffer=%p, mod pagesize = %ld\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [45 x i8] c"opened file: %s, with file descriptor = %d\0A\00"
@.str.3 = [30 x i8] c"read %d bytes which were:\0A%s\0A\00"
@EXIT_SUCCESS = global i32 0
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 i32
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = call i32 (...) @getpagesize()
store i32 %12, i32* %9
%13 = load i32, i32* %9
store i32 %13, i32* %8
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %11
br label %20
20: ; preds = %16, %2
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
%28 = load i32, i32* %9
%29 = mul nsw i32 %27, %28
store i32 %29, i32* %8
br label %30
30: ; preds = %23, %20
%31 = load i32, i32* %9
%32 = load i32, i32* %8
%33 = call i32 @posix_memalign(i8** %10, i32 %31, i32 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = load i8*, i8** %10
%36 = load i8*, i8** %10
%37 = ptrtoint i8* %36 to i64
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = urem i64 %37, %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %34, i8* %35, i64 %40)
%42 = load i8*, i8** %11
%43 = load i32, i32* @O_RDONLY
%44 = call i32 @open(i8* %42, i32 %43)
store i32 %44, i32* %6
%45 = load i8*, i8** %11
%46 = load i32, i32* %6
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %45, i32 %46)
%48 = load i32, i32* %6
%49 = load i8*, i8** %10
%50 = load i32, i32* %8
%51 = call i32 @read(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = load i8*, i8** %10
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %52, i8* %53)
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* @EXIT_SUCCESS
%58 = call i32 @exit(i32 %57)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/mycdrv\00"
@.str.1 = [38 x i8] c"rc=%d, buffer=%p, mod pagesize = %ld\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [45 x i8] c"opened file: %s, with file descriptor = %d\0A\00"
@.str.3 = [30 x i8] c"read %d bytes which were:\0A%s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i32 (...) @getpagesize()
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = icmp eq i32 %0, 2
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = mul nsw i32 %14, %5
br label %16
16: ; preds = %2, %11, %7
%17 = phi i8* [ %9, %11 ], [ %9, %7 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%18 = phi i32 [ %15, %11 ], [ %5, %7 ], [ %5, %2 ]
%19 = call i32 @posix_memalign(i8** %3, i32 %5, i32 %18)
%20 = load i8*, i8** %3
%21 = ptrtoint i8* %20 to i64
%22 = sext i32 %5 to i64
%23 = urem i64 %21, %22
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %20, i64 %23)
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %17, i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %17, i32 %26)
%28 = load i8*, i8** %3
%29 = call i32 @read(i32 %26, i8* %28, i32 %18)
%30 = load i8*, i8** %3
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %29, i8* %30)
%32 = call i32 @close(i32 %26)
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
}