#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_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
};

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int);
int /*<<< orphan*/ printf(char *, int, char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int init_serial(char *path, int baud, int custom_baud) {
  struct termios term;
  int fd = open(path, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    printf("init_serial %d: path=%s: %s\n", 202, path, strerror(errno));
    return -1;
  }
  if (tcgetattr(fd, &term)) {
    printf("init_serial %d: path=%s %s\n", 208, path, strerror(errno));
    close(fd);
    return -1;
  }
  tcflush(fd, TCIOFLUSH);
  cfsetispeed(&term, baud);
  cfsetospeed(&term, baud);
  term.c_iflag = 0;
  term.c_oflag = 0;
  term.c_lflag = 0;
  term.c_cc[VTIME] = 1;
  term.c_cc[VMIN] = 1;
  if (tcsetattr(fd, TCSANOW, &term)) {
    printf("init_serial %d: path=%s %s\n", 244, path, strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<init_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    120d <init_serial+0x7d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xe09(%rip),%rdi
mov    $0xca,%esi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1337 <init_serial+0x1a7>
mov    -0x3c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
je     1265 <init_serial+0xd5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd6(%rip),%rdi
mov    $0xd0,%esi
call   1050 <printf@plt>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1337 <init_serial+0x1a7>
mov    -0x3c(%rbp),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x14(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   10a0 <cfsetispeed@plt>
mov    -0x14(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2cf7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
mov    0x2d0a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x3c(%rbp),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
je     1331 <init_serial+0x1a1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xd0a(%rip),%rdi
mov    $0xf4,%esi
call   1050 <printf@plt>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1337 <init_serial+0x1a7>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_serial>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e47(%rip),%rcx
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e28(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1090 <open@plt>
test   %eax,%eax
js     11f7 <init_serial+0x67>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <tcgetattr@plt>
test   %rax,%rax
je     1221 <init_serial+0x91>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe30(%rip),%rdi
mov    $0xd0,%esi
jmp    12b4 <init_serial+0x124>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdf4(%rip),%rdi
mov    $0xca,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <printf@plt>
jmp    12c6 <init_serial+0x136>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    %ebp,%esi
call   10a0 <cfsetispeed@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x8(%rsp),%rax
mov    0x2d41(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d58(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1080 <tcsetattr@plt>
test   %rax,%rax
je     12cb <init_serial+0x13b>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd6e(%rip),%rdi
mov    $0xf4,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i64, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str = [29 x i8] c"init_serial %d: path=%s: %s\0A\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"init_serial %d: path=%s %s\0A\00"
@TCIOFLUSH = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @init_serial(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = load i32, i32* @O_SYNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 202, i8* %20, i8* %22)
store i32 -1, i32* %4
br label %67
24: ; preds = %3
%25 = load i32, i32* %9
%26 = call i64 @tcgetattr(i32 %25, %struct.termios* %8)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = load i8*, i8** %5
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 208, i8* %29, i8* %31)
%33 = load i32, i32* %9
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %4
br label %67
35: ; preds = %24
%36 = load i32, i32* %9
%37 = load i32, i32* @TCIOFLUSH
%38 = call i32 @tcflush(i32 %36, i32 %37)
%39 = load i32, i32* %6
%40 = call i32 @cfsetispeed(%struct.termios* %8, i32 %39)
%41 = load i32, i32* %6
%42 = call i32 @cfsetospeed(%struct.termios* %8, i32 %41)
%43 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 3
store i64 0, i64* %43
%44 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 2
store i64 0, i64* %44
%45 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 1
store i64 0, i64* %45
%46 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%47 = load i32*, i32** %46
%48 = load i64, i64* @VTIME
%49 = getelementptr inbounds i32, i32* %47, i64 %48
store i32 1, i32* %49
%50 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%51 = load i32*, i32** %50
%52 = load i64, i64* @VMIN
%53 = getelementptr inbounds i32, i32* %51, i64 %52
store i32 1, i32* %53
%54 = load i32, i32* %9
%55 = load i32, i32* @TCSANOW
%56 = call i64 @tcsetattr(i32 %54, i32 %55, %struct.termios* %8)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %65
58: ; preds = %35
%59 = load i8*, i8** %5
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 244, i8* %59, i8* %61)
%63 = load i32, i32* %9
%64 = call i32 @close(i32 %63)
store i32 -1, i32* %4
br label %67
65: ; preds = %35
%66 = load i32, i32* %9
store i32 %66, i32* %4
br label %67
67: ; preds = %65, %58, %28, %19
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
%struct.termios = type { i32*, i64, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str = [29 x i8] c"init_serial %d: path=%s: %s\0A\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"init_serial %d: path=%s %s\0A\00"
@TCIOFLUSH = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @init_serial(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 202, i8* %0, i8* %15)
br label %46
17: ; preds = %3
%18 = call i64 @tcgetattr(i32 %11, %struct.termios* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 208, i8* %0, i8* %22)
%24 = call i32 @close(i32 %11)
br label %46
25: ; preds = %17
%26 = load i32, i32* @TCIOFLUSH
%27 = call i32 @tcflush(i32 %11, i32 %26)
%28 = call i32 @cfsetispeed(%struct.termios* %4, i32 %1)
%29 = call i32 @cfsetospeed(%struct.termios* %4, i32 %1)
%30 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%31 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%32 = bitcast i64* %30 to i8*
%33 = load i32*, i32** %31
%34 = load i64, i64* @VTIME
%35 = getelementptr inbounds i32, i32* %33, i64 %34
store i32 1, i32* %35
%36 = load i64, i64* @VMIN
%37 = getelementptr inbounds i32, i32* %33, i64 %36
store i32 1, i32* %37
%38 = load i32, i32* @TCSANOW
%39 = call i64 @tcsetattr(i32 %11, i32 %38, %struct.termios* %4)
%40 = icmp eq i64 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %25
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 244, i8* %0, i8* %43)
%45 = call i32 @close(i32 %11)
br label %46
46: ; preds = %25, %41, %20, %13
%47 = phi i32 [ -1, %13 ], [ -1, %20 ], [ -1, %41 ], [ %11, %25 ]
ret i32 %47
}