#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 */
typedef int /*<<< orphan*/ message;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int length, fd1, fd2, rc;
  char *nodename = "/dev/mycdrv";
  char message[] = " *** TESTING CHAR/DRIVER ***\n";
  length = sizeof(message);
  if (argc > 1)
    nodename = argv[1];
  fd1 = open(nodename, O_RDWR);
  printf(" opened file descriptor first time  = %d\n", fd1);
  fd2 = open(nodename, O_RDWR);
  printf(" opened file descriptor second time  = %d\n", fd2);
  rc = write(fd1, message, length);
  printf("return code from write = %d on %d, message=%s\n", rc, fd1, message);
  memset(message, 0, length);
  rc = read(fd2, message, length);
  printf("return code from read  = %d on %d, message=%s\n", rc, fd2, message);
  close(fd1);
  exit(0);
}


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)
lea    0xe73(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xf28(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xf25(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xf22(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xf20(%rip),%eax
mov    %eax,-0x38(%rbp)
mov    0xf1a(%rip),%ax
mov    %ax,-0x34(%rbp)
movl   $0x1e,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11e3 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    -0x50(%rbp),%rcx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x1c(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    -0x50(%rbp),%rcx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movups 0xf4f(%rip),%xmm0
movups %xmm0,0xe(%rsp)
movaps 0xf35(%rip),%xmm0
movaps %xmm0,(%rsp)
cmp    $0x2,%edi
jl     119a <main+0x2a>
mov    0x8(%rsi),%rbp
jmp    11a1 <main+0x31>
lea    0xe5f(%rip),%rbp
mov    0x2e40(%rip),%r14
mov    (%r14),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
lea    0xe50(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xe5d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1e,%edx
call   1030 <write@plt>
lea    0xe66(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1e,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1e,%edx
call   1070 <read@plt>
lea    0xe62(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/mycdrv\00"
@__const.main.message = [30 x i8] c" *** TESTING CHAR/DRIVER ***\0A\00"
@O_RDWR = global i32 0
@.str.1 = [42 x i8] c" opened file descriptor first time = %d\0A\00"
@.str.2 = [43 x i8] c" opened file descriptor second time = %d\0A\00"
@.str.3 = [47 x i8] c"return code from write = %d on %d, message=%s\0A\00"
@.str.4 = [47 x i8] c"return code from read = %d on %d, message=%s\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 i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [30 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %10
%12 = bitcast [30 x i8]* %11 to i8*
store i32 30, i32* %6
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %10
br label %19
19: ; preds = %15, %2
%20 = load i8*, i8** %10
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i8*, i8** %10
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %7
%31 = getelementptr inbounds [30 x i8], [30 x i8]* %11, i64 0, i64 0
%32 = load i32, i32* %6
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = load i32, i32* %7
%36 = getelementptr inbounds [30 x i8], [30 x i8]* %11, i64 0, i64 0
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %35, i8* %36)
%38 = getelementptr inbounds [30 x i8], [30 x i8]* %11, i64 0, i64 0
%39 = load i32, i32* %6
%40 = call i32 @memset(i8* %38, i32 0, i32 %39)
%41 = load i32, i32* %8
%42 = getelementptr inbounds [30 x i8], [30 x i8]* %11, i64 0, i64 0
%43 = load i32, i32* %6
%44 = call i32 @read(i32 %41, i8* %42, i32 %43)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = load i32, i32* %8
%47 = getelementptr inbounds [30 x i8], [30 x i8]* %11, i64 0, i64 0
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %45, i32 %46, i8* %47)
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
%51 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/mycdrv\00"
@__const.main.message = [30 x i8] c" *** TESTING CHAR/DRIVER ***\0A\00"
@O_RDWR = global i32 0
@.str.1 = [42 x i8] c" opened file descriptor first time = %d\0A\00"
@.str.2 = [43 x i8] c" opened file descriptor second time = %d\0A\00"
@.str.3 = [47 x i8] c"return code from write = %d on %d, message=%s\0A\00"
@.str.4 = [47 x i8] c"return code from read = %d on %d, message=%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [30 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %6, %2
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* %10, i32 %14)
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 @write(i32 %12, i8* %4, i32 30)
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %12, i8* %4)
%19 = call i32 @memset(i8* %4, i32 0, i32 30)
%20 = call i32 @read(i32 %15, i8* %4, i32 30)
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %15, i8* %4)
%22 = call i32 @close(i32 %12)
%23 = call i32 @exit(i32 0)
unreachable
}