#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 O_NDELAY;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int main(void) {
  int ret;
  char buf[32];
  int fd = open("/dev/my_misc_dev", O_RDONLY | O_NDELAY);
  if (fd < 0) {
    printf("open file %s failed\n", "/dev/my_misc_dev");
    return -1;
  }
  read(fd, buf, 32);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1199 <main+0x59>
lea    0xe90(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <main+0x79>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1050 <read@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e9c(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea2(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1183 <main+0x43>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    119b <main+0x5b>
lea    0xe87(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/dev/my_misc_dev\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [21 x i8] c"open file %s failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NDELAY
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %19
13: ; preds = %0
%14 = load i32, i32* %4
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 32)
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
store i32 0, i32* %1
br label %19
19: ; preds = %13, %11
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
@.str = [17 x i8] c"/dev/my_misc_dev\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [21 x i8] c"open file %s failed\0A\00"
define i32 @main() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_NDELAY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %13
10: ; preds = %0
%11 = call i32 @read(i32 %6, i8* %2, i32 32)
%12 = tail call i32 @close(i32 %6)
br label %13
13: ; preds = %10, %8
%14 = phi i32 [ -1, %8 ], [ 0, %10 ]
ret i32 %14
}