#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_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  int index = 0, nbytes;
  char data[15];
  if (argc < 2) {
    perror("usage: ./test_module /dev/[device_name]");
    return -1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("Error openning device");
    return -1;
  }
  do {
    nbytes = read(fd, &data[index], 15);
    index++;
  } while (nbytes != 0);
  data[index + 1] = '\0';
  puts(data);
  return 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   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    117f <main+0x3f>
lea    0xe92(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <main+0xca>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ba <main+0x7a>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <main+0xca>
jmp    11bf <main+0x7f>
mov    -0x14(%rbp),%edi
movslq -0x18(%rbp),%rax
lea    -0x2b(%rbp),%rsi
add    %rax,%rsi
mov    $0xf,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11bf <main+0x7f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0x2b(%rbp,%rax,1)
lea    -0x2b(%rbp),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jle    11a6 <main+0x66>
mov    0x8(%rsi),%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11af <main+0x6f>
mov    %eax,%ebp
mov    $0x1,%ebx
nopl   0x0(%rax)
lea    (%rsp,%rbx,1),%rsi
add    $0x8,%rsi
mov    %ebp,%edi
mov    $0xf,%edx
call   1040 <read@plt>
add    $0x1,%rbx
test   %eax,%eax
jne    1170 <main+0x30>
mov    %ebx,%eax
movb   $0x0,0x9(%rsp,%rax,1)
lea    0x9(%rsp),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe53(%rip),%rdi
jmp    11b6 <main+0x76>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    119f <main+0x5f>

LLVM IR (-O0):
@.str = [40 x i8] c"usage: ./test_module /dev/[device_name]\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Error openning device\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 [15 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
24: ; preds = %14
br label %25
25: ; preds = %33, %24
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 %28
%30 = call i32 @read(i32 %26, i8* %29, i32 15)
store i32 %30, i32* %8
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %33
33: ; preds = %25
%34 = load i32, i32* %8
%35 = icmp ne i32 %34, 0
br i1 %35, label %25, label %36
36: ; preds = %33
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%42 = call i32 @puts(i8* %41)
store i32 0, i32* %3
br label %43
43: ; preds = %36, %22, %12
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [40 x i8] c"usage: ./test_module /dev/[device_name]\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Error openning device\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [15 x i8]
%4 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %27
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %27
16: ; preds = %8, %16
%17 = phi i64 [ %20, %16 ], [ 0, %8 ]
%18 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 %17
%19 = call i32 @read(i32 %12, i8* %18, i32 15)
%20 = add nuw i64 %17, 1
%21 = icmp eq i32 %19, 0
br i1 %21, label %22, label %16
22: ; preds = %16
%23 = add i64 %17, 2
%24 = and i64 %23, 4294967295
%25 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 @puts(i8* %4)
br label %27
27: ; preds = %22, %14, %6
%28 = phi i32 [ -1, %6 ], [ -1, %14 ], [ 0, %22 ]
ret i32 %28
}