#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;
  int /*<<< orphan*/ c_lflag;
  int /*<<< orphan*/ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int B115200;
int CLOCAL;
int CREAD;
int CS8;
scalar_t__ EINTR;
int EINVAL;
int EIO;
int /*<<< orphan*/ NOFLSH;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCEXCL;
size_t VMIN;
size_t VTIME;
scalar_t__ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/);
int open(char const *, int);
int /*<<< orphan*/ serial_close(int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_open(const char *device_path) {
  int fd = -1;
  if (!device_path || !*device_path) {
    return -EINVAL;
  }
  do {
    fd = open(device_path, O_RDWR | O_NOCTTY);
  } while (fd == -1 && errno == EINTR);
  if (fd < 0) {
    return fd;
  }
  if (ioctl(fd, TIOCEXCL)) {
    serial_close(fd);
    return -EIO;
  }
  struct termios tio = {
      .c_cflag = B115200 | CS8 | CLOCAL | CREAD,
      .c_iflag = 0,
      .c_oflag = 0,
      .c_lflag = NOFLSH,
      .c_cc = {0},
  };
  tio.c_cc[VMIN] = 1;
  tio.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSANOW, &tio)) {
    return -1;
  } else {
    tcflush(fd, TCIFLUSH);
    return fd;
  }
}


x86 assembly (clang, -O0):
<serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117b <serial_open+0x2b>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    118e <serial_open+0x3e>
xor    %eax,%eax
mov    0x2dfc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12e5 <serial_open+0x195>
jmp    1193 <serial_open+0x43>
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x14(%rbp)
mov    %al,-0x39(%rbp)
jne    11da <serial_open+0x8a>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1193 <serial_open+0x43>
cmpl   $0x0,-0x14(%rbp)
jge    11fa <serial_open+0xaa>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e5 <serial_open+0x195>
mov    -0x14(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1230 <serial_open+0xe0>
mov    -0x14(%rbp),%edi
call   1050 <serial_close@plt>
xor    %eax,%eax
mov    0x2d82(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12e5 <serial_open+0x195>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    0x2d88(%rip),%rcx
or     (%rcx),%eax
mov    0x2d17(%rip),%rcx
or     (%rcx),%eax
mov    0x2d7e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d3c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2cd7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x14(%rbp),%edi
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     12ce <serial_open+0x17e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <serial_open+0x195>
mov    -0x14(%rbp),%edi
mov    0x2cc8(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %rdi,%rdi
je     11d9 <serial_open+0x89>
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
je     11d9 <serial_open+0x89>
mov    0x2e76(%rip),%r15
mov    0x2e27(%rip),%r12
mov    0x2e18(%rip),%r13
mov    0x2e09(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    (%r12),%esi
or     (%r15),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11b0 <serial_open+0x60>
mov    0x0(%rbp),%rax
cmp    %rax,0x0(%r13)
je     1190 <serial_open+0x40>
test   %ebx,%ebx
js     11e4 <serial_open+0x94>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
je     11f5 <serial_open+0xa5>
mov    %ebx,%edi
call   1050 <serial_close@plt>
mov    0x2dd9(%rip),%rax
jmp    11e0 <serial_open+0x90>
mov    0x2da8(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d84(%rip),%rax
mov    0x2dc5(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d62(%rip),%rax
or     (%rax),%ecx
mov    0x2db9(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x1c(%rsp)
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <tcsetattr@plt>
mov    $0xffffffff,%ecx
test   %rax,%rax
jne    126d <serial_open+0x11d>
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    %ebx,%ecx
mov    %ecx,%ebx
jmp    11e4 <serial_open+0x94>

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i32, i32, i32 }
@EINVAL = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@TIOCEXCL = global i32 0
@EIO = global i32 0
@B115200 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@NOFLSH = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@TCIFLUSH = global i32 0
define i32 @serial_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
store i8* %0, i8** %3
store i32 -1, i32* %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %8, %1
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
store i32 %14, i32* %2
br label %78
15: ; preds = %8
br label %16
16: ; preds = %29, %15
%17 = load i8*, i8** %3
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_NOCTTY
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %17, i32 %20)
store i32 %21, i32* %4
br label %22
22: ; preds = %16
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br label %29
29: ; preds = %25, %22
%30 = phi i1 [ false, %22 ], [ %28, %25 ]
br i1 %30, label %16, label %31
31: ; preds = %29
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %78
36: ; preds = %31
%37 = load i32, i32* %4
%38 = load i32, i32* @TIOCEXCL
%39 = call i64 @ioctl(i32 %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = load i32, i32* %4
%43 = call i32 @serial_close(i32 %42)
%44 = load i32, i32* @EIO
%45 = sub nsw i32 0, %44
store i32 %45, i32* %2
br label %78
46: ; preds = %36
%47 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%48 = load i32, i32* @B115200
%49 = load i32, i32* @CS8
%50 = or i32 %48, %49
%51 = load i32, i32* @CLOCAL
%52 = or i32 %50, %51
%53 = load i32, i32* @CREAD
%54 = or i32 %52, %53
store i32 %54, i32* %47
%55 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
store i32* null, i32** %55
%56 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
%57 = load i32, i32* @NOFLSH
store i32 %57, i32* %56
%58 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 3
store i32 0, i32* %58
%59 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 4
store i32 0, i32* %59
%60 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%61 = load i32*, i32** %60
%62 = load i64, i64* @VMIN
%63 = getelementptr inbounds i32, i32* %61, i64 %62
store i32 1, i32* %63
%64 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%65 = load i32*, i32** %64
%66 = load i64, i64* @VTIME
%67 = getelementptr inbounds i32, i32* %65, i64 %66
store i32 0, i32* %67
%68 = load i32, i32* %4
%69 = load i32, i32* @TCSANOW
%70 = call i64 @tcsetattr(i32 %68, i32 %69, %struct.termios* %5)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %46
store i32 -1, i32* %2
br label %78
73: ; preds = %46
%74 = load i32, i32* %4
%75 = load i32, i32* @TCIFLUSH
%76 = call i32 @tcflush(i32 %74, i32 %75)
%77 = load i32, i32* %4
store i32 %77, i32* %2
br label %78
78: ; preds = %73, %72, %41, %34, %12
%79 = load i32, i32* %2
ret i32 %79
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i32, i32, i32 }
@EINVAL = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@TIOCEXCL = global i32 0
@EIO = global i32 0
@B115200 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@NOFLSH = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@TCIFLUSH = global i32 0
define i32 @serial_open(i8* %0) {
%2 = alloca %struct.termios
%3 = icmp eq i8* %0, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %4, %1
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %57
10: ; preds = %4, %10
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp eq i32 %14, -1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
%19 = select i1 %15, i1 %18, i1 false
br i1 %19, label %10, label %20
20: ; preds = %10
%21 = icmp slt i32 %14, 0
br i1 %21, label %57, label %22
22: ; preds = %20
%23 = load i32, i32* @TIOCEXCL
%24 = tail call i64 @ioctl(i32 %14, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = tail call i32 @serial_close(i32 %14)
%28 = load i32, i32* @EIO
%29 = sub nsw i32 0, %28
br label %57
30: ; preds = %22
%31 = bitcast %struct.termios* %2 to i8*
%32 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%33 = load i32, i32* @B115200
%34 = load i32, i32* @CS8
%35 = or i32 %34, %33
%36 = load i32, i32* @CLOCAL
%37 = or i32 %35, %36
%38 = load i32, i32* @CREAD
%39 = or i32 %37, %38
store i32 %39, i32* %32
%40 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
store i32* null, i32** %40
%41 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%42 = load i32, i32* @NOFLSH
store i32 %42, i32* %41
%43 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
store i32 0, i32* %43
%44 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 4
store i32 0, i32* %44
%45 = load i64, i64* @VMIN
%46 = getelementptr inbounds i32, i32* null, i64 %45
store i32 poison, i32* %46
%47 = load i64, i64* @VTIME
%48 = getelementptr inbounds i32, i32* null, i64 %47
store i32 poison, i32* %48
%49 = load i32, i32* @TCSANOW
%50 = call i64 @tcsetattr(i32 %14, i32 %49, %struct.termios* %2)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %30
%53 = load i32, i32* @TCIFLUSH
%54 = call i32 @tcflush(i32 %14, i32 %53)
br label %55
55: ; preds = %30, %52
%56 = phi i32 [ %14, %52 ], [ -1, %30 ]
br label %57
57: ; preds = %20, %55, %26, %7
%58 = phi i32 [ %29, %26 ], [ %56, %55 ], [ %9, %7 ], [ %14, %20 ]
ret i32 %58
}