#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;
scalar_t__ check_fd_device(int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int check_device(char *device) {
  int fd;
  int exit_value = 0;
  if ((fd = open(device, O_RDONLY)) < 0) {
    int errsv = errno;
    fprintf(stderr, "Error while opening %s: %s.\n", device, strerror(errsv));
    return 0;
  }
  if (check_fd_device(fd)) {
    exit_value = 1;
  }
  close(fd);
  return exit_value;
}


x86 assembly (clang, -O0):
<check_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11cb <check_device+0x7b>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe46(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f2 <check_device+0xa2>
mov    -0x14(%rbp),%edi
call   1070 <check_fd_device@plt>
cmp    $0x0,%rax
je     11e4 <check_device+0x94>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_device>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1182 <check_device+0x32>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <check_fd_device@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11af <check_device+0x5f>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%ebp
call   1060 <strerror@plt>
lea    0xe60(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Error while opening %s: %s.\0A\00"
define i32 @check_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load i32, i32* @errno
store i32 %12, i32* %6
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = load i32, i32* %6
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %16)
store i32 0, i32* %2
br label %27
18: ; preds = %1
%19 = load i32, i32* %4
%20 = call i64 @check_fd_device(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 1, i32* %5
br label %23
23: ; preds = %22, %18
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %5
store i32 %26, i32* %2
br label %27
27: ; preds = %23, %11
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Error while opening %s: %s.\0A\00"
define i32 @check_device(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i32, i32* @errno
%7 = load i32, i32* @stderr
%8 = tail call i8* @strerror(i32 %6)
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %8)
br label %15
10: ; preds = %1
%11 = tail call i64 @check_fd_device(i32 %3)
%12 = icmp ne i64 %11, 0
%13 = zext i1 %12 to i32
%14 = tail call i32 @close(i32 %3)
br label %15
15: ; preds = %10, %5
%16 = phi i32 [ 0, %5 ], [ %13, %10 ]
ret i32 %16
}