#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char **, int);
int main(void) {
  int fn = -1;
  char buf[10];
  printf("Attempting to open file module file...\n");
  fn = open("/dev/mymodule0", O_RDWR);
  if (fn < 0) {
    printf("Failed to open file!\n");
    return -1;
  }
  printf("Attempting to read from the file...\n");
  read(fn, &buf, 10);
  printf("Finished reading from file\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
lea    0xeb3(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xec6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118c <main+0x5c>
lea    0xebc(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <main+0x8c>
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0xa,%edx
call   1040 <read@plt>
lea    0xec2(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1184 <main+0x54>
mov    %eax,%ebx
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
lea    0x6(%rsp),%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1040 <read@plt>
xor    %ebx,%ebx
lea    0xed8(%rip),%rdi
jmp    1190 <main+0x60>
mov    $0xffffffff,%ebx
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [40 x i8] c"Attempting to open file module file...\0A\00"
@.str.1 = [15 x i8] c"/dev/mymodule0\00"
@O_RDWR = global i32 0
@.str.2 = [22 x i8] c"Failed to open file!\0A\00"
@.str.3 = [37 x i8] c"Attempting to read from the file...\0A\00"
@.str.4 = [28 x i8] c"Finished reading from file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [10 x i8]
store i32 0, i32* %1
store i32 -1, i32* %2
%4 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %17
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%13 = load i32, i32* %2
%14 = bitcast [10 x i8]* %3 to i8**
%15 = call i32 @read(i32 %13, i8** %14, i32 10)
%16 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %17
17: ; preds = %11, %9
%18 = load i32, i32* %1
ret i32 %18
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"/dev/mymodule0\00"
@O_RDWR = global i32 0
@str = [39 x i8] c"Attempting to open file module file...\00"
@str.5 = [36 x i8] c"Attempting to read from the file...\00"
@str.6 = [27 x i8] c"Finished reading from file\00"
@str.7 = [21 x i8] c"Failed to open file!\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.5, i64 0, i64 0))
%9 = bitcast [10 x i8]* %1 to i8**
%10 = call i32 @read(i32 %5, i8** %9, i32 10)
br label %11
11: ; preds = %0, %7
%12 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0), %7 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0), %0 ]
%13 = phi i32 [ 0, %7 ], [ -1, %0 ]
%14 = tail call i32 @puts(i8* dereferenceable(1) %12)
ret i32 %13
}