#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 /*<<< orphan*/ c_cflag;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ B19200;
int /*<<< orphan*/ B38400;
int /*<<< orphan*/ B57600;
int /*<<< orphan*/ B9600;
int /*<<< orphan*/ CLOCAL;
int O_CLOEXEC;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int open(char const *, int);
int sscanf(char const *, char *, int *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ warn(char *, char const *, ...);
int openserialdevice(const char *device, const char *speed) {
  int fd;
  int baudrate;
  struct termios t;
  speed_t spd;
  if (sscanf(speed, "%d", &baudrate) != 1) {
    warn("invalid serial speed setting: %s", speed);
    return -1;
  }
  fd = open(device, O_RDWR | O_NONBLOCK | O_CLOEXEC | O_NOCTTY);
  if (fd < 0) {
    warn("failed to open %s (errno=%d)\n", device, errno);
    return -2;
  }
  if (tcgetattr(fd, &t)) {
    warn("tcgetattr() failed on %s\n", device);
    return -3;
  }
  switch (baudrate) {
  case 9600:
    spd = B9600;
    break;
  case 19200:
    spd = B19200;
    break;
  case 38400:
    spd = B38400;
    break;
  case 57600:
    spd = B57600;
    break;
  case 115200:
    spd = B115200;
    break;
  default:
    spd = B9600;
    warn("invalid serial port speed setting (%d) falling back to default",
         speed);
  }
  cfmakeraw(&t);
  cfsetspeed(&t, spd);
  t.c_cflag |= CLOCAL;
  if (tcsetattr(fd, TCSANOW, &t)) {
    warn("tcsetattr() failed on %s\n", device);
    return -4;
  }
  return fd;
}


x86 assembly (clang, -O0):
<openserialdevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe75(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     11bb <openserialdevice+0x4b>
mov    -0x18(%rbp),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138c <openserialdevice+0x21c>
mov    -0x10(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2db8(%rip),%rax
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    121c <openserialdevice+0xac>
mov    -0x10(%rbp),%rsi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    138c <openserialdevice+0x21c>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
je     1250 <openserialdevice+0xe0>
mov    -0x10(%rbp),%rsi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    138c <openserialdevice+0x21c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x2580,%eax
je     12b2 <openserialdevice+0x142>
jmp    1266 <openserialdevice+0xf6>
mov    -0x30(%rbp),%eax
sub    $0x4b00,%eax
je     12c3 <openserialdevice+0x153>
jmp    1279 <openserialdevice+0x109>
mov    -0x30(%rbp),%eax
sub    $0x9600,%eax
je     12d4 <openserialdevice+0x164>
jmp    128c <openserialdevice+0x11c>
mov    -0x30(%rbp),%eax
sub    $0xe100,%eax
je     12e5 <openserialdevice+0x175>
jmp    129f <openserialdevice+0x12f>
mov    -0x30(%rbp),%eax
sub    $0x1c200,%eax
je     12f6 <openserialdevice+0x186>
jmp    1307 <openserialdevice+0x197>
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1325 <openserialdevice+0x1b5>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1325 <openserialdevice+0x1b5>
mov    0x2ca5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1325 <openserialdevice+0x1b5>
mov    0x2cd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1325 <openserialdevice+0x1b5>
mov    0x2ca3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1325 <openserialdevice+0x1b5>
mov    0x2cba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
lea    -0x28(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    -0x2c(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1040 <cfsetspeed@plt>
mov    0x2c47(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2c7d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
je     1386 <openserialdevice+0x216>
mov    -0x10(%rbp),%rsi
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    138c <openserialdevice+0x21c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openserialdevice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe7c(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11f2 <openserialdevice+0x82>
mov    0x2e43(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     120d <openserialdevice+0x9d>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <tcgetattr@plt>
test   %rax,%rax
je     1231 <openserialdevice+0xc1>
lea    0xe64(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
mov    $0xfffffffd,%ebp
jmp    1306 <openserialdevice+0x196>
lea    0xe0a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
mov    $0xffffffff,%ebp
jmp    1306 <openserialdevice+0x196>
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
lea    0xe07(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
mov    $0xfffffffe,%ebp
jmp    1306 <openserialdevice+0x196>
mov    0x4(%rsp),%eax
cmp    $0x95ff,%eax
jle    125d <openserialdevice+0xed>
cmp    $0x9600,%eax
je     1277 <openserialdevice+0x107>
cmp    $0xe100,%eax
je     1283 <openserialdevice+0x113>
cmp    $0x1c200,%eax
jne    129b <openserialdevice+0x12b>
mov    0x2d48(%rip),%rax
mov    (%rax),%r15d
jmp    12b6 <openserialdevice+0x146>
cmp    $0x2580,%eax
je     128f <openserialdevice+0x11f>
cmp    $0x4b00,%eax
jne    129b <openserialdevice+0x12b>
mov    0x2d7e(%rip),%rax
mov    (%rax),%r15d
jmp    12b6 <openserialdevice+0x146>
mov    0x2d02(%rip),%rax
mov    (%rax),%r15d
jmp    12b6 <openserialdevice+0x146>
mov    0x2d36(%rip),%rax
mov    (%rax),%r15d
jmp    12b6 <openserialdevice+0x146>
mov    0x2d32(%rip),%rax
mov    (%rax),%r15d
jmp    12b6 <openserialdevice+0x146>
mov    0x2d26(%rip),%rax
mov    (%rax),%r15d
lea    0xdb0(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <cfmakeraw@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <cfsetspeed@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%eax
or     %eax,(%rsp)
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1080 <tcsetattr@plt>
test   %rax,%rax
je     1306 <openserialdevice+0x196>
lea    0xda4(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
mov    $0xfffffffc,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [3 x i8] c"%d\00"
@.str.1 = [33 x i8] c"invalid serial speed setting: %s\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@O_CLOEXEC = global i32 0
@O_NOCTTY = global i32 0
@.str.2 = [30 x i8] c"failed to open %s (errno=%d)\0A\00"
@errno = global i32 0
@.str.3 = [26 x i8] c"tcgetattr() failed on %s\0A\00"
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B115200 = global i32 0
@.str.4 = [63 x i8] c"invalid serial port speed setting (%d) falling back to default\00"
@CLOCAL = global i32 0
@TCSANOW = global i32 0
@.str.5 = [26 x i8] c"tcsetattr() failed on %s\0A\00"
define i32 @openserialdevice(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
%12 = icmp ne i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %3
br label %72
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = load i32, i32* @O_CLOEXEC
%22 = or i32 %20, %21
%23 = load i32, i32* @O_NOCTTY
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %17, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = load i8*, i8** %4
%30 = load i32, i32* @errno
%31 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %29, i32 %30)
store i32 -2, i32* %3
br label %72
32: ; preds = %16
%33 = load i32, i32* %6
%34 = call i64 @tcgetattr(i32 %33, %struct.termios* %8)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i8*, i8** %4
%38 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %37)
store i32 -3, i32* %3
br label %72
39: ; preds = %32
%40 = load i32, i32* %7
switch i32 %40, label %51 [
i32 9600, label %41
i32 19200, label %43
i32 38400, label %45
i32 57600, label %47
i32 115200, label %49
]
41: ; preds = %39
%42 = load i32, i32* @B9600
store i32 %42, i32* %9
br label %55
43: ; preds = %39
%44 = load i32, i32* @B19200
store i32 %44, i32* %9
br label %55
45: ; preds = %39
%46 = load i32, i32* @B38400
store i32 %46, i32* %9
br label %55
47: ; preds = %39
%48 = load i32, i32* @B57600
store i32 %48, i32* %9
br label %55
49: ; preds = %39
%50 = load i32, i32* @B115200
store i32 %50, i32* %9
br label %55
51: ; preds = %39
%52 = load i32, i32* @B9600
store i32 %52, i32* %9
%53 = load i8*, i8** %5
%54 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %51, %49, %47, %45, %43, %41
%56 = call i32 @cfmakeraw(%struct.termios* %8)
%57 = load i32, i32* %9
%58 = call i32 @cfsetspeed(%struct.termios* %8, i32 %57)
%59 = load i32, i32* @CLOCAL
%60 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%61 = load i32, i32* %60
%62 = or i32 %61, %59
store i32 %62, i32* %60
%63 = load i32, i32* %6
%64 = load i32, i32* @TCSANOW
%65 = call i64 @tcsetattr(i32 %63, i32 %64, %struct.termios* %8)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %55
%68 = load i8*, i8** %4
%69 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %68)
store i32 -4, i32* %3
br label %72
70: ; preds = %55
%71 = load i32, i32* %6
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %67, %36, %28, %13
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [3 x i8] c"%d\00"
@.str.1 = [33 x i8] c"invalid serial speed setting: %s\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@O_CLOEXEC = global i32 0
@O_NOCTTY = global i32 0
@.str.2 = [30 x i8] c"failed to open %s (errno=%d)\0A\00"
@errno = global i32 0
@.str.3 = [26 x i8] c"tcgetattr() failed on %s\0A\00"
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B115200 = global i32 0
@.str.4 = [63 x i8] c"invalid serial port speed setting (%d) falling back to default\00"
@CLOCAL = global i32 0
@TCSANOW = global i32 0
@.str.5 = [26 x i8] c"tcsetattr() failed on %s\0A\00"
define i32 @openserialdevice(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.termios
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
%7 = call i32 @sscanf(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%8 = icmp eq i32 %7, 1
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %57
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %13, %12
%15 = load i32, i32* @O_CLOEXEC
%16 = or i32 %14, %15
%17 = load i32, i32* @O_NOCTTY
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %0, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = load i32, i32* @errno
%23 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %22)
br label %57
24: ; preds = %11
%25 = call i64 @tcgetattr(i32 %19, %struct.termios* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %57
29: ; preds = %24
%30 = load i32, i32* %3
switch i32 %30, label %41 [
i32 9600, label %31
i32 19200, label %33
i32 38400, label %35
i32 57600, label %37
i32 115200, label %39
]
31: ; preds = %29
%32 = load i32, i32* @B9600
br label %44
33: ; preds = %29
%34 = load i32, i32* @B19200
br label %44
35: ; preds = %29
%36 = load i32, i32* @B38400
br label %44
37: ; preds = %29
%38 = load i32, i32* @B57600
br label %44
39: ; preds = %29
%40 = load i32, i32* @B115200
br label %44
41: ; preds = %29
%42 = load i32, i32* @B9600
%43 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %44
44: ; preds = %41, %39, %37, %35, %33, %31
%45 = phi i32 [ %42, %41 ], [ %40, %39 ], [ %38, %37 ], [ %36, %35 ], [ %34, %33 ], [ %32, %31 ]
%46 = call i32 @cfmakeraw(%struct.termios* %4)
%47 = call i32 @cfsetspeed(%struct.termios* %4, i32 %45)
%48 = load i32, i32* @CLOCAL
%49 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%50 = load i32, i32* %49
%51 = or i32 %50, %48
store i32 %51, i32* %49
%52 = load i32, i32* @TCSANOW
%53 = call i64 @tcsetattr(i32 %19, i32 %52, %struct.termios* %4)
%54 = icmp eq i64 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %44
%56 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %57
57: ; preds = %44, %55, %27, %21, %9
%58 = phi i32 [ -1, %9 ], [ -2, %21 ], [ -3, %27 ], [ -4, %55 ], [ %19, %44 ]
ret i32 %58
}