#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*/ ENOTTY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ assert(char const *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ grf_logging_log(int, char *, ...);
int /*<<< orphan*/ isatty(int);
int open(char const *, int);
int grf_uart_open(const char *dev) {
  assert(dev);
  int fd;
  grf_logging_log(2, ("Opening %s..."), dev);
  fd = open(dev, O_RDWR | O_NOCTTY);
  if (fd < 0)
    return fd;
  grf_logging_log(3, ("    got fd=%d..."), fd);
  if (!isatty(fd)) {
    errno = ENOTTY;
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<grf_uart_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdx
mov    $0x2,%edi
lea    0xe9b(%rip),%rsi
mov    $0x0,%al
call   1050 <grf_logging_log@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119f <grf_uart_open+0x5f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ea <grf_uart_open+0xaa>
mov    -0x14(%rbp),%edx
mov    $0x3,%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <grf_logging_log@plt>
mov    -0x14(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    11e4 <grf_uart_open+0xa4>
mov    0x2e13(%rip),%rax
mov    (%rax),%ecx
mov    0x2df2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <grf_uart_open+0xaa>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grf_uart_open>:
push   %rbx
mov    %rdi,%rbx
call   1040 <assert@plt>
lea    0xeb0(%rip),%rsi
mov    $0x2,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <grf_logging_log@plt>
mov    0x2e82(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b6 <grf_uart_open+0x76>
lea    0xe88(%rip),%rsi
mov    $0x3,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <grf_logging_log@plt>
mov    %ebx,%edi
call   1030 <isatty@plt>
test   %eax,%eax
jne    11b6 <grf_uart_open+0x76>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    0x2e19(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Opening %s...\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [17 x i8] c" got fd=%d...\00"
@ENOTTY = global i32 0
@errno = global i32 0
define i32 @grf_uart_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @assert(i8* %5)
%7 = load i8*, i8** %3
%8 = call i32 (i32, i8*, ...) @grf_logging_log(i32 2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = load i32, i32* %4
store i32 %17, i32* %2
br label %28
18: ; preds = %1
%19 = load i32, i32* %4
%20 = call i32 (i32, i8*, ...) @grf_logging_log(i32 3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %4
%22 = call i32 @isatty(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = load i32, i32* @ENOTTY
store i32 %25, i32* @errno
store i32 -1, i32* %2
br label %28
26: ; preds = %18
%27 = load i32, i32* %4
store i32 %27, i32* %2
br label %28
28: ; preds = %26, %24, %16
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [14 x i8] c"Opening %s...\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [17 x i8] c" got fd=%d...\00"
@ENOTTY = global i32 0
@errno = global i32 0
define i32 @grf_uart_open(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = tail call i32 (i32, i8*, ...) @grf_logging_log(i32 2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %1
%10 = tail call i32 (i32, i8*, ...) @grf_logging_log(i32 3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%11 = tail call i32 @isatty(i32 %7)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load i32, i32* @ENOTTY
store i32 %14, i32* @errno
br label %15
15: ; preds = %9, %1, %13
%16 = phi i32 [ -1, %13 ], [ %7, %1 ], [ %7, %9 ]
ret i32 %16
}