#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*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int length = 20, fd, rc;
  char *message, *nodename = "/dev/mycdrv";
  if (argc > 1)
    nodename = argv[1];
  if (argc > 2)
    length = atoi(argv[2]);
  message = malloc(length);
  memset(message, 'x', length);
  message[length - 1] = '\0';
  fd = open(nodename, O_RDWR);
  printf(" I opened the device node, file descriptor = %d\n", fd);
  rc = write(fd, message, length);
  printf("return code from write = %d\n", rc);
  memset(message, 0, length);
  lseek(fd, 0, SEEK_SET);
  rc = read(fd, message, length);
  printf("return code from read = %d\n", rc);
  printf(" the message was: %s\n", message);
  close(fd);
  exit(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)
movl   $0x14,-0x14(%rbp)
lea    0xe3c(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11de <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11f8 <main+0x58>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0x78,%esi
call   1060 <memset@plt>
mov    -0x28(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x14,%ebp
cmp    $0x2,%edi
jl     11c4 <main+0x24>
mov    0x8(%rsi),%r15
je     11cb <main+0x2b>
mov    0x10(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
jmp    11cb <main+0x2b>
lea    0xe35(%rip),%r15
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x78,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
movslq %ebp,%r14
movb   $0x0,-0x1(%r14,%rbx,1)
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
lea    0xe05(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
lea    0xe19(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1060 <memset@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1080 <read@plt>
lea    0xdfa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe06(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/mycdrv\00"
@O_RDWR = global i32 0
@.str.1 = [49 x i8] c" I opened the device node, file descriptor = %d\0A\00"
@.str.2 = [29 x i8] c"return code from write = %d\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [28 x i8] c"return code from read = %d\0A\00"
@.str.4 = [22 x i8] c" the message was: %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 i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 20, i32* %6
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %10
br label %17
17: ; preds = %13, %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 2
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %6
br label %25
25: ; preds = %20, %17
%26 = load i32, i32* %6
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = load i32, i32* %6
%30 = call i32 @memset(i8* %28, i8 signext 120, i32 %29)
%31 = load i8*, i8** %9
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %10
%37 = load i32, i32* @O_RDWR
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %7
%42 = load i8*, i8** %9
%43 = load i32, i32* %6
%44 = call i32 @write(i32 %41, i8* %42, i32 %43)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = load i8*, i8** %9
%48 = load i32, i32* %6
%49 = call i32 @memset(i8* %47, i8 signext 0, i32 %48)
%50 = load i32, i32* %7
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @lseek(i32 %50, i32 0, i32 %51)
%53 = load i32, i32* %7
%54 = load i8*, i8** %9
%55 = load i32, i32* %6
%56 = call i32 @read(i32 %53, i8* %54, i32 %55)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %57)
%59 = load i8*, i8** %9
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %59)
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
%63 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/mycdrv\00"
@O_RDWR = global i32 0
@.str.1 = [49 x i8] c" I opened the device node, file descriptor = %d\0A\00"
@.str.2 = [29 x i8] c"return code from write = %d\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [28 x i8] c"return code from read = %d\0A\00"
@.str.4 = [22 x i8] c" the message was: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %12
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
br label %12
12: ; preds = %2, %8, %4
%13 = phi i8* [ %6, %8 ], [ %6, %4 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%14 = phi i32 [ %11, %8 ], [ 20, %4 ], [ 20, %2 ]
%15 = tail call i8* @malloc(i32 %14)
%16 = tail call i32 @memset(i8* %15, i8 signext 120, i32 %14)
%17 = add nsw i32 %14, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
store i8 0, i8* %19
%20 = load i32, i32* @O_RDWR
%21 = tail call i32 @open(i8* %13, i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = tail call i32 @write(i32 %21, i8* %15, i32 %14)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = tail call i32 @memset(i8* %15, i8 signext 0, i32 %14)
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @lseek(i32 %21, i32 0, i32 %26)
%28 = tail call i32 @read(i32 %21, i8* %15, i32 %14)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %15)
%31 = tail call i32 @close(i32 %21)
%32 = tail call i32 @exit(i32 0)
unreachable
}