#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;
scalar_t__ _IOC(int /*<<< orphan*/, char, int, int);
int /*<<< orphan*/ _IOC_READ;
int /*<<< orphan*/ _IOC_WRITE;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int ioctl(int, int, char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int fd, rc, i, lbuf;
  char *buffer, *nodename = "/dev/mycdrv";
  int MYIOC_X;
  if (argc > 1)
    nodename = argv[1];
  fd = open(nodename, O_RDWR);
  printf(" I opened the device node, file descriptor = %d\n", fd);
  lbuf = 1000;
  if (argc > 2)
    lbuf = atoi(argv[1]);
  printf(" I am going to send back and forth a buffer of %d bytes\n", lbuf);
  buffer = malloc(lbuf);
  printf("\n Getting data from the kernel:\n");
  MYIOC_X = (int)_IOC(_IOC_READ, ('k'), (1), lbuf);
  rc = ioctl(fd, MYIOC_X, buffer);
  printf("\n rc from ioctl = %d \n\n", rc);
  printf(" buffer in user-space is =\n   ");
  for (i = 0; i < lbuf; i++)
    printf("%c", buffer[i]);
  printf("\n");
  memset(buffer, '0', lbuf);
  printf("\n Sending data to the kernel:\n");
  MYIOC_X = (int)_IOC(_IOC_WRITE, ('k'), (1), lbuf);
  rc = ioctl(fd, MYIOC_X, buffer);
  printf("\n rc from ioctl = %d \n\n", rc);
  printf(" buffer in user-space is =\n   ");
  for (i = 0; i < lbuf; i++)
    printf("%c", buffer[i]);
  printf("\n");
  close(fd);
  exit(0);
}


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    0xe53(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c7 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x3e8,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    120e <main+0x7e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%ecx
mov    $0x6b,%esi
mov    $0x1,%edx
call   1030 <_IOC@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12c3 <main+0x133>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    128f <main+0xff>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    $0x30,%esi
call   1050 <memset@plt>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%ecx
mov    $0x6b,%esi
mov    $0x1,%edx
call   1030 <_IOC@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    137a <main+0x1ea>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1346 <main+0x1b6>
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%ebx
cmp    $0x2,%edi
jl     11c8 <main+0x18>
mov    0x8(%r15),%rdi
jmp    11cf <main+0x1f>
lea    0xe31(%rip),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,%r14d
lea    0xe25(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3e8,%r12d
cmp    $0x3,%ebx
jl     1207 <main+0x57>
mov    0x8(%r15),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r12d
lea    0xe2f(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
lea    0xe83(%rip),%rdi
call   1040 <puts@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    $0x6b,%esi
mov    $0x1,%edx
mov    %r12d,%ecx
call   1050 <_IOC@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %r15,%rdx
call   1080 <ioctl@plt>
lea    0xe18(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe20(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r12d,%r12d
jle    1335 <main+0x185>
mov    %r12d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%r15,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1290 <main+0xe0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15,%rdi
mov    $0x30,%esi
mov    %r12d,%edx
call   1070 <memset@plt>
lea    0xe09(%rip),%rdi
call   1040 <puts@plt>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
mov    $0x6b,%esi
mov    $0x1,%edx
mov    %r12d,%ecx
call   1050 <_IOC@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %r15,%rdx
call   1080 <ioctl@plt>
lea    0xd7e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd86(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
test   %r12d,%r12d
jle    13a1 <main+0x1f1>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1320 <main+0x170>
jmp    13a1 <main+0x1f1>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15,%rdi
mov    $0x30,%esi
mov    %r12d,%edx
call   1070 <memset@plt>
lea    0xd77(%rip),%rdi
call   1040 <puts@plt>
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
mov    $0x6b,%esi
mov    $0x1,%edx
mov    %r12d,%ecx
call   1050 <_IOC@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %r15,%rdx
call   1080 <ioctl@plt>
lea    0xcec(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xcf4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10d0 <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 = [57 x i8] c" I am going to send back and forth a buffer of %d bytes\0A\00"
@.str.3 = [33 x i8] c"\0A Getting data from the kernel:\0A\00"
@_IOC_READ = global i32 0
@.str.4 = [24 x i8] c"\0A rc from ioctl = %d \0A\0A\00"
@.str.5 = [31 x i8] c" buffer in user-space is =\0A \00"
@.str.6 = [3 x i8] c"%c\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [31 x i8] c"\0A Sending data to the kernel:\0A\00"
@_IOC_WRITE = 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*
%12 = alloca i32
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** %11
%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** %11
br label %19
19: ; preds = %15, %2
%20 = load i8*, i8** %11
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 1000, i32* %9
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 2
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %9
br label %32
32: ; preds = %27, %19
%33 = load i32, i32* %9
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %9
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %10
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @_IOC_READ
%39 = load i32, i32* %9
%40 = call i64 @_IOC(i32 %38, i8 signext 107, i32 1, i32 %39)
%41 = trunc i64 %40 to i32
store i32 %41, i32* %12
%42 = load i32, i32* %6
%43 = load i32, i32* %12
%44 = load i8*, i8** %10
%45 = call i32 @ioctl(i32 %42, i32 %43, i8* %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %8
br label %49
49: ; preds = %61, %32
%50 = load i32, i32* %8
%51 = load i32, i32* %9
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %64
53: ; preds = %49
%54 = load i8*, i8** %10
%55 = load i32, i32* %8
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %53
%62 = load i32, i32* %8
%63 = add nsw i32 %62, 1
store i32 %63, i32* %8
br label %49
64: ; preds = %49
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%66 = load i8*, i8** %10
%67 = load i32, i32* %9
%68 = call i32 @memset(i8* %66, i32 48, i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0))
%70 = load i32, i32* @_IOC_WRITE
%71 = load i32, i32* %9
%72 = call i64 @_IOC(i32 %70, i8 signext 107, i32 1, i32 %71)
%73 = trunc i64 %72 to i32
store i32 %73, i32* %12
%74 = load i32, i32* %6
%75 = load i32, i32* %12
%76 = load i8*, i8** %10
%77 = call i32 @ioctl(i32 %74, i32 %75, i8* %76)
store i32 %77, i32* %7
%78 = load i32, i32* %7
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %78)
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %8
br label %81
81: ; preds = %93, %64
%82 = load i32, i32* %8
%83 = load i32, i32* %9
%84 = icmp slt i32 %82, %83
br i1 %84, label %85, label %96
85: ; preds = %81
%86 = load i8*, i8** %10
%87 = load i32, i32* %8
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %86, i64 %88
%90 = load i8, i8* %89
%91 = sext i8 %90 to i32
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %91)
br label %93
93: ; preds = %85
%94 = load i32, i32* %8
%95 = add nsw i32 %94, 1
store i32 %95, i32* %8
br label %81
96: ; preds = %81
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%98 = load i32, i32* %6
%99 = call i32 @close(i32 %98)
%100 = 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 = [57 x i8] c" I am going to send back and forth a buffer of %d bytes\0A\00"
@_IOC_READ = global i32 0
@.str.4 = [24 x i8] c"\0A rc from ioctl = %d \0A\0A\00"
@.str.5 = [31 x i8] c" buffer in user-space is =\0A \00"
@_IOC_WRITE = global i32 0
@str = [32 x i8] c"\0A Getting data from the kernel:\00"
@str.9 = [30 x i8] c"\0A Sending data to the kernel:\00"
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, i64 0, i64 0), %2 ]
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp sgt i32 %0, 2
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
br label %17
17: ; preds = %13, %7
%18 = phi i32 [ %16, %13 ], [ 1000, %7 ]
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = tail call i8* @malloc(i32 %18)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%22 = load i32, i32* @_IOC_READ
%23 = tail call i64 @_IOC(i32 %22, i8 signext 107, i32 1, i32 %18)
%24 = trunc i64 %23 to i32
%25 = tail call i32 @ioctl(i32 %10, i32 %24, i8* %20)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%28 = icmp sgt i32 %18, 0
br i1 %28, label %29, label %59
29: ; preds = %17
%30 = zext i32 %18 to i64
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %37, %31 ]
%33 = getelementptr inbounds i8, i8* %20, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = tail call i32 @putchar(i32 %35)
%37 = add nuw nsw i64 %32, 1
%38 = icmp eq i64 %37, %30
br i1 %38, label %39, label %31
39: ; preds = %31
%40 = tail call i32 @putchar(i32 10)
%41 = tail call i32 @memset(i8* %20, i32 48, i32 %18)
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.9, i64 0, i64 0))
%43 = load i32, i32* @_IOC_WRITE
%44 = tail call i64 @_IOC(i32 %43, i8 signext 107, i32 1, i32 %18)
%45 = trunc i64 %44 to i32
%46 = tail call i32 @ioctl(i32 %10, i32 %45, i8* %20)
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br i1 %28, label %49, label %69
49: ; preds = %39
%50 = zext i32 %18 to i64
br label %51
51: ; preds = %49, %51
%52 = phi i64 [ 0, %49 ], [ %57, %51 ]
%53 = getelementptr inbounds i8, i8* %20, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = tail call i32 @putchar(i32 %55)
%57 = add nuw nsw i64 %52, 1
%58 = icmp eq i64 %57, %50
br i1 %58, label %69, label %51
59: ; preds = %17
%60 = tail call i32 @putchar(i32 10)
%61 = tail call i32 @memset(i8* %20, i32 48, i32 %18)
%62 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.9, i64 0, i64 0))
%63 = load i32, i32* @_IOC_WRITE
%64 = tail call i64 @_IOC(i32 %63, i8 signext 107, i32 1, i32 %18)
%65 = trunc i64 %64 to i32
%66 = tail call i32 @ioctl(i32 %10, i32 %65, i8* %20)
%67 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %66)
%68 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %51, %59, %39
%70 = tail call i32 @putchar(i32 10)
%71 = tail call i32 @close(i32 %10)
%72 = tail call i32 @exit(i32 0)
unreachable
}