#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 c_cflag;
  int *c_cc;
  scalar_t__ c_lflag;
  int /*<<< orphan*/ c_iflag;
  scalar_t__ c_oflag;
};
typedef int /*<<< orphan*/ newtio;

/* Variables and functions */
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int _get_baudrate(int);
int /*<<< orphan*/ bzero(struct termios *, int);
int open(char const *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int uart_open(const char *dev, int baudrate) {
  int fd, c, res;
  struct termios newtio;
  fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd < 0)
    return -1;
  bzero(&newtio, sizeof(newtio));
  newtio.c_oflag = 0;
  newtio.c_cflag = _get_baudrate(baudrate) | CS8 | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR;
  newtio.c_lflag = 0;
  newtio.c_cc[VTIME] = 0;
  newtio.c_cc[VMIN] = 1;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &newtio);
  return fd;
}


x86 assembly (clang, -O0):
<uart_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2e64(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    119c <uart_open+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <uart_open+0xf2>
lea    -0x48(%rbp),%rdi
mov    $0x28,%esi
call   1070 <bzero@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <_get_baudrate@plt>
mov    0x2e07(%rip),%rcx
or     (%rcx),%eax
mov    0x2dbe(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2da9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x40(%rbp),%rax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x18(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e89(%rip),%rcx
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%ebp
mov    0x2e6d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1060 <open@plt>
test   %eax,%eax
js     1229 <uart_open+0xd9>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x28,%esi
call   1070 <bzero@plt>
movq   $0x0,0x28(%rsp)
mov    %ebp,%edi
call   1040 <_get_baudrate@plt>
mov    0x2e19(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd0(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2db7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
jmp    122e <uart_open+0xde>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i32, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @uart_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.termios
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %50
20: ; preds = %2
%21 = call i32 @bzero(%struct.termios* %9, i32 40)
%22 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 4
store i64 0, i64* %22
%23 = load i32, i32* %5
%24 = call i32 @_get_baudrate(i32 %23)
%25 = load i32, i32* @CS8
%26 = or i32 %24, %25
%27 = load i32, i32* @CLOCAL
%28 = or i32 %26, %27
%29 = load i32, i32* @CREAD
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* @IGNPAR
%33 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 3
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 2
store i64 0, i64* %34
%35 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 1
%36 = load i32*, i32** %35
%37 = load i64, i64* @VTIME
%38 = getelementptr inbounds i32, i32* %36, i64 %37
store i32 0, i32* %38
%39 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 1
%40 = load i32*, i32** %39
%41 = load i64, i64* @VMIN
%42 = getelementptr inbounds i32, i32* %40, i64 %41
store i32 1, i32* %42
%43 = load i32, i32* %6
%44 = load i32, i32* @TCIFLUSH
%45 = call i32 @tcflush(i32 %43, i32 %44)
%46 = load i32, i32* %6
%47 = load i32, i32* @TCSANOW
%48 = call i32 @tcsetattr(i32 %46, i32 %47, %struct.termios* %9)
%49 = load i32, i32* %6
store i32 %49, i32* %3
br label %50
50: ; preds = %20, %19
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i32, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @uart_open(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %36, label %12
12: ; preds = %2
%13 = call i32 @bzero(%struct.termios* %3, i32 40)
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 4
store i64 0, i64* %14
%15 = tail call i32 @_get_baudrate(i32 %1)
%16 = load i32, i32* @CS8
%17 = or i32 %16, %15
%18 = load i32, i32* @CLOCAL
%19 = or i32 %17, %18
%20 = load i32, i32* @CREAD
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @IGNPAR
%24 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%27 = load i32*, i32** %26
%28 = load i64, i64* @VTIME
%29 = getelementptr inbounds i32, i32* %27, i64 %28
store i32 0, i32* %29
%30 = load i64, i64* @VMIN
%31 = getelementptr inbounds i32, i32* %27, i64 %30
store i32 1, i32* %31
%32 = load i32, i32* @TCIFLUSH
%33 = tail call i32 @tcflush(i32 %10, i32 %32)
%34 = load i32, i32* @TCSANOW
%35 = call i32 @tcsetattr(i32 %10, i32 %34, %struct.termios* %3)
br label %36
36: ; preds = %2, %12
%37 = phi i32 [ %10, %12 ], [ -1, %2 ]
ret i32 %37
}