#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 O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpagesize();
int open(char *, int, int);
int posix_memalign(void **, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(void *, char *);
int strlen(void *);
int write(int, void *, int);
int main(int argc, char *argv[]) {
  int fd, rc = 0, size, pagesize;
  void *buffer;
  char *filename = "/dev/mycdrv";
  size = strlen("TESTING DRIVER WITH A WRITE") + 1;
  pagesize = getpagesize();
  rc = posix_memalign(&buffer, pagesize, size);
  printf("rc=%d, buffer=%p, mod pagesize = %ld\n", rc, buffer,
         (unsigned long)buffer % pagesize);
  strcpy(buffer, "TESTING DRIVER WITH A WRITE");
  if (argc > 1)
    filename = argv[1];
  fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  printf("opened file: %s,  with file descriptor = %d\n", filename, fd);
  rc = write(fd, buffer, strlen(buffer) + 1);
  printf("wrote %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    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xe4c(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe4d(%rip),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    -0x28(%rbp),%rdi
call   10b0 <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    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1030 <strcpy@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1239 <main+0xa9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
mov    0x2d9a(%rip),%rax
or     (%rax),%esi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
mov    0x2d68(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe68(%rip),%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%ebp
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %eax,%ebx
mov    %rsp,%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   10b0 <posix_memalign@plt>
mov    %eax,%esi
mov    (%rsp),%rbp
movslq %ebx,%rcx
mov    %rbp,%rax
or     %rcx,%rax
shr    $0x20,%rax
jne    11e0 <main+0x50>
mov    %ebp,%eax
xor    %edx,%edx
div    %ebx
mov    %edx,%ecx
jmp    11eb <main+0x5b>
mov    %rbp,%rax
xor    %edx,%edx
div    %rcx
mov    %rdx,%rcx
lea    0xe36(%rip),%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%rdi
lea    0xe05(%rip),%rsi
call   1030 <strcpy@plt>
cmp    $0x2,%r15d
jl     1218 <main+0x88>
mov    0x8(%r14),%rbp
jmp    121f <main+0x8f>
lea    0xde1(%rip),%rbp
mov    0x2db2(%rip),%rax
mov    0x2dcb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db8(%rip),%rax
or     (%rax),%esi
mov    0x2d7f(%rip),%rax
mov    0x2d88(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
lea    0xdf1(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <write@plt>
mov    (%rsp),%rdx
lea    0xdee(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/mycdrv\00"
@.str.1 = [28 x i8] c"TESTING DRIVER WITH A WRITE\00"
@.str.2 = [38 x i8] c"rc=%d, buffer=%p, mod pagesize = %ld\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [45 x i8] c"opened file: %s, with file descriptor = %d\0A\00"
@.str.4 = [31 x i8] c"wrote %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 @strlen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%6 = add nsw i32 %5, 1
%7 = tail call i32 (...) @getpagesize()
%8 = call i32 @posix_memalign(i8** %3, i32 %7, i32 %6)
%9 = load i8*, i8** %3
%10 = ptrtoint i8* %9 to i64
%11 = sext i32 %7 to i64
%12 = urem i64 %10, %11
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %8, i8* %9, i64 %12)
%14 = load i8*, i8** %3
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp sgt i32 %0, 1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
br label %20
20: ; preds = %17, %2
%21 = phi i8* [ %19, %17 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %28, %27
%30 = call i32 @open(i8* %21, i32 %26, i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %21, i32 %30)
%32 = load i8*, i8** %3
%33 = call i32 @strlen(i8* %32)
%34 = add nsw i32 %33, 1
%35 = call i32 @write(i32 %30, i8* %32, i32 %34)
%36 = load i8*, i8** %3
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %35, i8* %36)
%38 = call i32 @close(i32 %30)
%39 = load i32, i32* @EXIT_SUCCESS
%40 = call i32 @exit(i32 %39)
unreachable
}