#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int open(char const *, int, int /*<<< orphan*/);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int open_device(const char *dev_name) {
  struct stat st;
  int fd;
  if (stat(dev_name, &st) == -1) {
    fprintf(stderr, "Cannot identify '%s': %d, %s\n", dev_name, errno,
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (!S_ISCHR(st.st_mode)) {
    fprintf(stderr, "%s is no device\n", dev_name);
    exit(EXIT_FAILURE);
  }
  fd = open(dev_name, O_RDWR | O_NONBLOCK, 0);
  if (fd == -1) {
    fprintf(stderr, "Cannot open '%s': %d, %s\n", dev_name, errno,
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    11d9 <open_device+0x79>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%ecx
mov    %rax,%r8
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%edi
call   1050 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    1213 <open_device+0xb3>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2db9(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1294 <open_device+0x134>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    %rax,%r8
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_device>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     11b1 <open_device+0x51>
mov    0x8(%rsp),%edi
call   1050 <S_ISCHR@plt>
test   %eax,%eax
je     11d4 <open_device+0x74>
mov    0x2e5a(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11f0 <open_device+0x90>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e18(%rip),%rax
mov    (%rax),%r14d
mov    0x2e06(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe2e(%rip),%rsi
jmp    1211 <open_device+0xb1>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    1223 <open_device+0xc3>
mov    0x2dd9(%rip),%rax
mov    (%rax),%r14d
mov    0x2dc7(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe1e(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %ebp,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Cannot identify '%s': %d, %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"%s is no device\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [26 x i8] c"Cannot open '%s': %d, %s\0A\00"
define i32 @open_device(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @stat(i8* %5, %struct.stat* %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %17
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %2
%11 = load i32, i32* @errno
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %11, i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @S_ISCHR(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %2
%25 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %17
%29 = load i8*, i8** %2
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_NONBLOCK
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %29, i32 %32, i32 0)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %45
36: ; preds = %28
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %2
%39 = load i32, i32* @errno
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %38, i32 %39, i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %28
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [30 x i8] c"Cannot identify '%s': %d, %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"%s is no device\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [26 x i8] c"Cannot open '%s': %d, %s\0A\00"
define i32 @open_device(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %8, i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @S_ISCHR(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %13
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %24
%27 = tail call i32 @open(i8* %0, i32 %26, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = tail call i8* @strerror(i32 %31)
%33 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %31, i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = tail call i32 @exit(i32 %34)
unreachable
36: ; preds = %23
ret i32 %27
}