#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 getpid();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int, int, char *);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd, rc;
  char *buffer = "TESTING CHAR DRIVER WITH A WRITE", *filename = "/dev/mycdrv";
  if (argc > 1)
    filename = argv[1];
  fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  rc = write(fd, buffer, strlen(buffer));
  printf("process %d wrote %d bytes to %s\n", getpid(), rc, filename);
  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)
lea    0xe73(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe89(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    0x2def(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
lea    0xe03(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jl     117f <main+0xf>
mov    0x8(%rsi),%r14
jmp    1186 <main+0x16>
lea    0xe9b(%rip),%r14
mov    0x2e4b(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
mov    0x2e18(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xe3c(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe45(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %r14,%rcx
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"TESTING CHAR DRIVER WITH A WRITE\00"
@.str.1 = [12 x i8] c"/dev/mycdrv\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.2 = [33 x i8] c"process %d wrote %d bytes to %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 i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8** %8
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %9
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %9
br label %16
16: ; preds = %12, %2
%17 = load i8*, i8** %9
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %17, i32 %22, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = load i8*, i8** %8
%29 = load i8*, i8** %8
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i32 %30)
store i32 %31, i32* %7
%32 = call i32 (...) @getpid()
%33 = load i32, i32* %7
%34 = load i8*, i8** %9
%35 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33, i8* %34)
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @EXIT_SUCCESS
%39 = call i32 @exit(i32 %38)
unreachable
}

LLVM IR (-O2):
@.str = [33 x i8] c"TESTING CHAR DRIVER WITH A WRITE\00"
@.str.1 = [12 x i8] c"/dev/mycdrv\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.2 = [33 x i8] c"process %d wrote %d bytes to %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
br label %7
7: ; preds = %4, %2
%8 = phi i8* [ %6, %4 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %2 ]
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* %8, i32 %13, i32 %16)
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %19, i8* %8)
%22 = tail call i32 @close(i32 %17)
%23 = load i32, i32* @EXIT_SUCCESS
%24 = tail call i32 @exit(i32 %23)
unreachable
}