#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 termios {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
scalar_t__ isatty(int);
int open(char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int eng_open_dev(char *dev, int mode) {
  int fd;
  struct termios ser_settings;
  fd = open(dev, mode);
  if (fd < 0)
    return -1;
  if (isatty(fd)) {
    tcgetattr(fd, &ser_settings);
    cfmakeraw(&ser_settings);
    tcsetattr(fd, TCSANOW, &ser_settings);
  }
  return fd;
}


x86 assembly (clang, -O0):
<eng_open_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1184 <eng_open_dev+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <eng_open_dev+0x76>
mov    -0x18(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     11c0 <eng_open_dev+0x70>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <tcgetattr@plt>
lea    -0x20(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    -0x18(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eng_open_dev>:
push   %r14
push   %rbx
push   %rax
call   1070 <open@plt>
test   %eax,%eax
js     1195 <eng_open_dev+0x45>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     119a <eng_open_dev+0x4a>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    %r14,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
jmp    119a <eng_open_dev+0x4a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@TCSANOW = global i32 0
define i32 @eng_open_dev(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %27
14: ; preds = %2
%15 = load i32, i32* %6
%16 = call i64 @isatty(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load i32, i32* %6
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %7)
%21 = call i32 @cfmakeraw(%struct.termios* %7)
%22 = load i32, i32* %6
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %22, i32 %23, %struct.termios* %7)
br label %25
25: ; preds = %18, %14
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %27
27: ; preds = %25, %13
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TCSANOW = global i32 0
define i32 @eng_open_dev(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = tail call i32 @open(i8* %0, i32 %1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %2
%8 = tail call i64 @isatty(i32 %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = call i32 @tcgetattr(i32 %5, %struct.termios* %3)
%12 = call i32 @cfmakeraw(%struct.termios* %3)
%13 = load i32, i32* @TCSANOW
%14 = call i32 @tcsetattr(i32 %5, i32 %13, %struct.termios* %3)
br label %15
15: ; preds = %7, %10, %2
%16 = phi i32 [ -1, %2 ], [ %5, %10 ], [ %5, %7 ]
ret i32 %16
}