#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 lseek(int, int, 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, position = 0, fd, rc;
  char *message, *nodename = "/dev/mycdrv";
  if (argc > 1)
    nodename = argv[1];
  if (argc > 2)
    position = atoi(argv[2]);
  if (argc > 3)
    length = atoi(argv[3]);
  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 = lseek(fd, position, SEEK_SET);
  printf("return code from lseek = %d\n", rc);
  rc = write(fd, message, length);
  printf("return code from write = %d\n", rc);
  rc = write(fd, message, length);
  printf("return code from write = %d\n", rc);
  memset(message, 0, length);
  rc = lseek(fd, position, SEEK_SET);
  printf("return code from lseek = %d\n", rc);
  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)
movl   $0x0,-0x18(%rbp)
lea    0xe35(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11e5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11ff <main+0x5f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1219 <main+0x79>
mov    -0x10(%rbp),%rax
mov    0x18(%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    0x2d96(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    0x2d5e(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd86(%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    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    0x2ce2(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%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   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x14,%ebp
xor    %r14d,%r14d
cmp    $0x2,%edi
jl     11e1 <main+0x41>
mov    %rsi,%r15
mov    0x8(%rsi),%r12
je     11e8 <main+0x48>
mov    %edi,%ebx
mov    0x10(%r15),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
cmp    $0x4,%ebx
jb     11e8 <main+0x48>
mov    0x18(%r15),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
jmp    11e8 <main+0x48>
lea    0xe18(%rip),%r12
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,%r15
movb   $0x0,-0x1(%r15,%rbx,1)
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
lea    0xde8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <lseek@plt>
lea    0xdf6(%rip),%r12
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
lea    0xdf3(%rip),%r13
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1060 <memset@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <lseek@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1080 <read@plt>
lea    0xdab(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdb7(%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"
@SEEK_SET = global i32 0
@.str.2 = [29 x i8] c"return code from lseek = %d\0A\00"
@.str.3 = [29 x i8] c"return code from write = %d\0A\00"
@.str.4 = [28 x i8] c"return code from read = %d\0A\00"
@.str.5 = [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 i32
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 20, i32* %6
store i32 0, i32* %7
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %11
br label %18
18: ; preds = %14, %2
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %7
br label %26
26: ; preds = %21, %18
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 3
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %6
br label %34
34: ; preds = %29, %26
%35 = load i32, i32* %6
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %10
%37 = load i8*, i8** %10
%38 = load i32, i32* %6
%39 = call i32 @memset(i8* %37, i8 signext 120, i32 %38)
%40 = load i8*, i8** %10
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 0, i8* %44
%45 = load i8*, i8** %11
%46 = load i32, i32* @O_RDWR
%47 = call i32 @open(i8* %45, i32 %46)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %8
%51 = load i32, i32* %7
%52 = load i32, i32* @SEEK_SET
%53 = call i32 @lseek(i32 %50, i32 %51, i32 %52)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %8
%57 = load i8*, i8** %10
%58 = load i32, i32* %6
%59 = call i32 @write(i32 %56, i8* %57, i32 %58)
store i32 %59, i32* %9
%60 = load i32, i32* %9
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %8
%63 = load i8*, i8** %10
%64 = load i32, i32* %6
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %66)
%68 = load i8*, i8** %10
%69 = load i32, i32* %6
%70 = call i32 @memset(i8* %68, i8 signext 0, i32 %69)
%71 = load i32, i32* %8
%72 = load i32, i32* %7
%73 = load i32, i32* @SEEK_SET
%74 = call i32 @lseek(i32 %71, i32 %72, i32 %73)
store i32 %74, i32* %9
%75 = load i32, i32* %9
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %75)
%77 = load i32, i32* %8
%78 = load i8*, i8** %10
%79 = load i32, i32* %6
%80 = call i32 @read(i32 %77, i8* %78, i32 %79)
store i32 %80, i32* %9
%81 = load i32, i32* %9
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %81)
%83 = load i8*, i8** %10
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %83)
%85 = load i32, i32* %8
%86 = call i32 @close(i32 %85)
%87 = 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"
@SEEK_SET = global i32 0
@.str.2 = [29 x i8] c"return code from lseek = %d\0A\00"
@.str.3 = [29 x i8] c"return code from write = %d\0A\00"
@.str.4 = [28 x i8] c"return code from read = %d\0A\00"
@.str.5 = [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 %17
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 %17, 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)
%12 = icmp ugt i32 %0, 3
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
br label %17
17: ; preds = %2, %4, %13, %8
%18 = phi i32 [ %11, %13 ], [ %11, %8 ], [ 0, %4 ], [ 0, %2 ]
%19 = phi i8* [ %6, %13 ], [ %6, %8 ], [ %6, %4 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%20 = phi i32 [ %16, %13 ], [ 20, %8 ], [ 20, %4 ], [ 20, %2 ]
%21 = tail call i8* @malloc(i32 %20)
%22 = tail call i32 @memset(i8* %21, i8 signext 120, i32 %20)
%23 = add nsw i32 %20, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 @open(i8* %19, i32 %26)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i32, i32* @SEEK_SET
%30 = tail call i32 @lseek(i32 %27, i32 %18, i32 %29)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = tail call i32 @write(i32 %27, i8* %21, i32 %20)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = tail call i32 @write(i32 %27, i8* %21, i32 %20)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = tail call i32 @memset(i8* %21, i8 signext 0, i32 %20)
%37 = load i32, i32* @SEEK_SET
%38 = tail call i32 @lseek(i32 %27, i32 %18, i32 %37)
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = tail call i32 @read(i32 %27, i8* %21, i32 %20)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %21)
%43 = tail call i32 @close(i32 %27)
%44 = tail call i32 @exit(i32 0)
unreachable
}