#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 BUFSIZ;
int /*<<< orphan*/ O_RDWR;
int R_OK;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int W_OK;
int access(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int open_device(char *device_name) {
  struct stat s;
  int ret;
  char full_device_name[BUFSIZ];
  sprintf(full_device_name, "/dev/%s", device_name);
  ret = stat(full_device_name, &s);
  if (ret < 0) {
    fprintf(stderr, "Problem statting %s, %s\n", full_device_name,
            strerror(errno));
    return -1;
  }
  if (!S_ISCHR(s.st_mode)) {
    fprintf(stderr, "Error: %s is not a TTY character device.",
            full_device_name);
    return -1;
  }
  ret = access(full_device_name, R_OK | W_OK);
  if (ret) {
    fprintf(stderr, "Error: %s is not writable, %s.", full_device_name,
            strerror(errno));
    return -1;
  }
  ret = open(full_device_name, O_RDWR);
  if (ret < 0) {
    fprintf(stderr, "Error! Could not open %s, %s", full_device_name,
            strerror(errno));
    return -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<open_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdx
lea    0xe4d(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1217 <open_device+0xa7>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1334 <open_device+0x1c4>
mov    -0x18(%rbp),%edi
call   1050 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    1256 <open_device+0xe6>
mov    -0x40(%rbp),%rdx
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1334 <open_device+0x1c4>
mov    -0x40(%rbp),%rdi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    0x2d76(%rip),%rax
or     (%rax),%esi
call   1070 <access@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12c3 <open_device+0x153>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xda1(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1334 <open_device+0x1c4>
mov    -0x40(%rbp),%rdi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1327 <open_device+0x1b7>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd5c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1334 <open_device+0x1c4>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_device>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rdx
mov    %rsp,%r15
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe5f(%rip),%rsi
mov    %rbx,%rdi
call   1080 <sprintf@plt>
lea    -0x20(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
js     1204 <open_device+0x94>
mov    -0x20(%rbp),%edi
call   1050 <S_ISCHR@plt>
test   %eax,%eax
je     1235 <open_device+0xc5>
mov    0x2dec(%rip),%rax
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %eax,%eax
je     1262 <open_device+0xf2>
mov    0x2de6(%rip),%rax
mov    (%rax),%r14d
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe48(%rip),%rsi
jmp    1223 <open_device+0xb3>
mov    0x2dc5(%rip),%rax
mov    (%rax),%r14d
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde5(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    124f <open_device+0xdf>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    1254 <open_device+0xe4>
mov    0x2d52(%rip),%rax
mov    (%rax),%r14d
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd3(%rip),%rsi
jmp    1223 <open_device+0xb3>

LLVM IR (-O0):
%struct.stat = type { i32 }
@BUFSIZ = global i32 0
@.str = [8 x i8] c"/dev/%s\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Problem statting %s, %s\0A\00"
@errno = global i32 0
@.str.2 = [41 x i8] c"Error: %s is not a TTY character device.\00"
@R_OK = global i32 0
@W_OK = global i32 0
@.str.3 = [31 x i8] c"Error: %s is not writable, %s.\00"
@O_RDWR = global i32 0
@.str.4 = [29 x i8] c"Error! Could not open %s, %s\00"
define i32 @open_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = load i8*, i8** %3
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @stat(i8* %12, %struct.stat* %4)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %21)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %55
23: ; preds = %1
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @S_ISCHR(i32 %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %12)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %55
31: ; preds = %23
%32 = load i32, i32* @R_OK
%33 = load i32, i32* @W_OK
%34 = or i32 %32, %33
%35 = call i32 @access(i8* %12, i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %31
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %12, i8* %41)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %55
43: ; preds = %31
%44 = load i32, i32* @O_RDWR
%45 = call i32 @open(i8* %12, i32 %44)
store i32 %45, i32* %5
%46 = load i32, i32* %5
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %12, i8* %51)
store i32 -1, i32* %2
store i32 1, i32* %8
br label %55
53: ; preds = %43
%54 = load i32, i32* %5
store i32 %54, i32* %2
store i32 1, i32* %8
br label %55
55: ; preds = %53, %48, %38, %28, %18
%56 = load i8*, i8** %6
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@BUFSIZ = global i32 0
@.str = [8 x i8] c"/dev/%s\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Problem statting %s, %s\0A\00"
@errno = global i32 0
@.str.2 = [41 x i8] c"Error: %s is not a TTY character device.\00"
@R_OK = global i32 0
@W_OK = global i32 0
@.str.3 = [31 x i8] c"Error: %s is not writable, %s.\00"
@O_RDWR = global i32 0
@.str.4 = [29 x i8] c"Error! Could not open %s, %s\00"
define i32 @open_device(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = call i32 @stat(i8* %7, %struct.stat* %2)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %14)
br label %44
16: ; preds = %1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @S_ISCHR(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %7)
br label %44
24: ; preds = %16
%25 = load i32, i32* @R_OK
%26 = load i32, i32* @W_OK
%27 = or i32 %26, %25
%28 = call i32 @access(i8* %7, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = tail call i8* @strerror(i32 %32)
%34 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %7, i8* %33)
br label %44
35: ; preds = %24
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* %7, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %7, i8* %42)
br label %44
44: ; preds = %35, %39, %30, %21, %11
%45 = phi i32 [ -1, %11 ], [ -1, %30 ], [ -1, %39 ], [ -1, %21 ], [ %37, %35 ]
ret i32 %45
}