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

/* Variables and functions */
int B115200;
int B19200;
int B2400;
int B38400;
int B4800;
int B57600;
int B9600;
int /*<<< orphan*/ CRTSCTS;
int /*<<< orphan*/ CS7;
int /*<<< orphan*/ CS8;
int /*<<< orphan*/ CSIZE;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_com_config(int fd, int baud_rate, int data_bits, char parity,
                   int stop_bits) {
  struct termios cfg;
  int speed;
  if (tcgetattr(fd, &cfg) != 0) {
    perror("tcgetttr");
    return -1;
  }
  switch (baud_rate) {
  case 2400:
    speed = B2400;
    break;
  case 4800:
    speed = B4800;
    break;
  case 9600:
    speed = B9600;
    break;
  case 19200:
    speed = B19200;
    break;
  case 38400:
    speed = B38400;
    break;
  case 57600:
    speed = B57600;
    break;
  default:
  case 115200:
    speed = B115200;
    break;
  }
  cfsetispeed(&cfg, speed);
  cfsetospeed(&cfg, speed);
  cfg.c_cflag &= ~CSIZE;
  switch (data_bits) {
  case 7:
    cfg.c_cflag |= CS7;
    break;
  default:
  case 8:
    cfg.c_cflag |= CS8;
    break;
  }
  cfg.c_cc[VTIME] = 0;
  cfg.c_cc[VMIN] = 50;
  cfg.c_cflag &= ~CRTSCTS;
  cfg.c_lflag &= ~ICANON;
  if ((tcsetattr(fd, TCSANOW, &cfg)) != 0) {
    perror("tcsetattr");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_com_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %cl,%al
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
je     1198 <set_com_config+0x48>
lea    0xe79(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <set_com_config+0x24f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x960,%eax
je     1220 <set_com_config+0xd0>
jmp    11ae <set_com_config+0x5e>
mov    -0x30(%rbp),%eax
sub    $0x12c0,%eax
je     1231 <set_com_config+0xe1>
jmp    11c1 <set_com_config+0x71>
mov    -0x30(%rbp),%eax
sub    $0x2580,%eax
je     1242 <set_com_config+0xf2>
jmp    11d4 <set_com_config+0x84>
mov    -0x30(%rbp),%eax
sub    $0x4b00,%eax
je     1253 <set_com_config+0x103>
jmp    11e7 <set_com_config+0x97>
mov    -0x30(%rbp),%eax
sub    $0x9600,%eax
je     1264 <set_com_config+0x114>
jmp    11fa <set_com_config+0xaa>
mov    -0x30(%rbp),%eax
sub    $0xe100,%eax
je     1275 <set_com_config+0x125>
jmp    120d <set_com_config+0xbd>
mov    -0x30(%rbp),%eax
sub    $0x1c200,%eax
je     128b <set_com_config+0x13b>
jmp    1286 <set_com_config+0x136>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1297 <set_com_config+0x147>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1297 <set_com_config+0x147>
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1297 <set_com_config+0x147>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1297 <set_com_config+0x147>
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1297 <set_com_config+0x147>
mov    0x2d34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1297 <set_com_config+0x147>
jmp    128b <set_com_config+0x13b>
mov    0x2d06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    -0x28(%rbp),%rdi
mov    %rdi,-0x40(%rbp)
call   1060 <cfsetispeed@plt>
mov    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <cfsetospeed@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%ecx
not    %ecx
mov    -0x1c(%rbp),%eax
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x7,%eax
je     12eb <set_com_config+0x19b>
jmp    12da <set_com_config+0x18a>
mov    -0x34(%rbp),%eax
sub    $0x8,%eax
je     1304 <set_com_config+0x1b4>
jmp    12ff <set_com_config+0x1af>
mov    0x2c7e(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1313 <set_com_config+0x1c3>
jmp    1304 <set_com_config+0x1b4>
mov    0x2cbd(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2c72(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2c8d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x32,(%rax,%rcx,4)
mov    0x2c34(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2c32(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2c75(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
je     1398 <set_com_config+0x248>
lea    0xc82(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <set_com_config+0x24f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_com_config>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%r15d
lea    0x8(%rsp),%rsi
call   1040 <tcgetattr@plt>
test   %rax,%rax
jne    129e <set_com_config+0x14e>
cmp    $0x4aff,%ebp
jg     119e <set_com_config+0x4e>
cmp    $0x960,%ebp
je     11bf <set_com_config+0x6f>
cmp    $0x12c0,%ebp
je     11c8 <set_com_config+0x78>
cmp    $0x2580,%ebp
jne    11e3 <set_com_config+0x93>
mov    0x2e34(%rip),%rax
jmp    11ea <set_com_config+0x9a>
cmp    $0x4b00,%ebp
je     11d1 <set_com_config+0x81>
cmp    $0x9600,%ebp
je     11da <set_com_config+0x8a>
cmp    $0xe100,%ebp
jne    11e3 <set_com_config+0x93>
mov    0x2df3(%rip),%rax
jmp    11ea <set_com_config+0x9a>
mov    0x2dda(%rip),%rax
jmp    11ea <set_com_config+0x9a>
mov    0x2e09(%rip),%rax
jmp    11ea <set_com_config+0x9a>
mov    0x2e18(%rip),%rax
jmp    11ea <set_com_config+0x9a>
mov    0x2d87(%rip),%rax
jmp    11ea <set_com_config+0x9a>
mov    0x2dae(%rip),%rax
mov    (%rax),%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
call   1060 <cfsetispeed@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <cfsetospeed@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x14(%rsp),%eax
cmp    $0x7,%r14d
jne    1223 <set_com_config+0xd3>
mov    0x2d4f(%rip),%rcx
jmp    122a <set_com_config+0xda>
mov    0x2d9e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x14(%rsp)
mov    0x8(%rsp),%rax
mov    0x2d54(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d73(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x32,(%rax,%rcx,4)
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x14(%rsp)
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x10(%rsp)
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %rbp,%rdx
call   1050 <tcsetattr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    12a7 <set_com_config+0x157>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5b(%rip),%rdi
jmp    12ae <set_com_config+0x15e>
lea    0xd5b(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    1293 <set_com_config+0x143>

LLVM IR (-O0):
%struct.termios = type { i32*, i32, i32 }
@.str = [9 x i8] c"tcgetttr\00"
@B2400 = global i32 0
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B115200 = global i32 0
@CSIZE = global i32 0
@CS7 = global i32 0
@CS8 = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@CRTSCTS = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @set_com_config(i32 %0, i32 %1, i32 %2, i8 signext %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca %struct.termios
%13 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8 %3, i8* %10
store i32 %4, i32* %11
%14 = load i32, i32* %7
%15 = call i64 @tcgetattr(i32 %14, %struct.termios* %12)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %84
19: ; preds = %5
%20 = load i32, i32* %8
switch i32 %20, label %33 [
i32 2400, label %21
i32 4800, label %23
i32 9600, label %25
i32 19200, label %27
i32 38400, label %29
i32 57600, label %31
i32 115200, label %34
]
21: ; preds = %19
%22 = load i32, i32* @B2400
store i32 %22, i32* %13
br label %36
23: ; preds = %19
%24 = load i32, i32* @B4800
store i32 %24, i32* %13
br label %36
25: ; preds = %19
%26 = load i32, i32* @B9600
store i32 %26, i32* %13
br label %36
27: ; preds = %19
%28 = load i32, i32* @B19200
store i32 %28, i32* %13
br label %36
29: ; preds = %19
%30 = load i32, i32* @B38400
store i32 %30, i32* %13
br label %36
31: ; preds = %19
%32 = load i32, i32* @B57600
store i32 %32, i32* %13
br label %36
33: ; preds = %19
br label %34
34: ; preds = %19, %33
%35 = load i32, i32* @B115200
store i32 %35, i32* %13
br label %36
36: ; preds = %34, %31, %29, %27, %25, %23, %21
%37 = load i32, i32* %13
%38 = call i32 @cfsetispeed(%struct.termios* %12, i32 %37)
%39 = load i32, i32* %13
%40 = call i32 @cfsetospeed(%struct.termios* %12, i32 %39)
%41 = load i32, i32* @CSIZE
%42 = xor i32 %41, -1
%43 = getelementptr inbounds %struct.termios, %struct.termios* %12, i32 0, i32 2
%44 = load i32, i32* %43
%45 = and i32 %44, %42
store i32 %45, i32* %43
%46 = load i32, i32* %9
switch i32 %46, label %52 [
i32 7, label %47
i32 8, label %53
]
47: ; preds = %36
%48 = load i32, i32* @CS7
%49 = getelementptr inbounds %struct.termios, %struct.termios* %12, i32 0, i32 2
%50 = load i32, i32* %49
%51 = or i32 %50, %48
store i32 %51, i32* %49
br label %58
52: ; preds = %36
br label %53
53: ; preds = %36, %52
%54 = load i32, i32* @CS8
%55 = getelementptr inbounds %struct.termios, %struct.termios* %12, i32 0, i32 2
%56 = load i32, i32* %55
%57 = or i32 %56, %54
store i32 %57, i32* %55
br label %58
58: ; preds = %53, %47
%59 = getelementptr inbounds %struct.termios, %struct.termios* %12, i32 0, i32 0
%60 = load i32*, i32** %59
%61 = load i64, i64* @VTIME
%62 = getelementptr inbounds i32, i32* %60, i64 %61
store i32 0, i32* %62
%63 = getelementptr inbounds %struct.termios, %struct.termios* %12, i32 0, i32 0
%64 = load i32*, i32** %63
%65 = load i64, i64* @VMIN
%66 = getelementptr inbounds i32, i32* %64, i64 %65
store i32 50, i32* %66
%67 = load i32, i32* @CRTSCTS
%68 = xor i32 %67, -1
%69 = getelementptr inbounds %struct.termios, %struct.termios* %12, i32 0, i32 2
%70 = load i32, i32* %69
%71 = and i32 %70, %68
store i32 %71, i32* %69
%72 = load i32, i32* @ICANON
%73 = xor i32 %72, -1
%74 = getelementptr inbounds %struct.termios, %struct.termios* %12, i32 0, i32 1
%75 = load i32, i32* %74
%76 = and i32 %75, %73
store i32 %76, i32* %74
%77 = load i32, i32* %7
%78 = load i32, i32* @TCSANOW
%79 = call i64 @tcsetattr(i32 %77, i32 %78, %struct.termios* %12)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %58
%82 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %84
83: ; preds = %58
store i32 0, i32* %6
br label %84
84: ; preds = %83, %81, %17
%85 = load i32, i32* %6
ret i32 %85
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32, i32 }
@.str = [9 x i8] c"tcgetttr\00"
@B2400 = global i32 0
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B115200 = global i32 0
@CSIZE = global i32 0
@CS7 = global i32 0
@CS8 = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@CRTSCTS = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @set_com_config(i32 %0, i32 %1, i32 %2, i8 signext %3, i32 %4) {
%6 = alloca %struct.termios
%7 = bitcast %struct.termios* %6 to i8*
%8 = call i64 @tcgetattr(i32 %0, %struct.termios* %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %50
10: ; preds = %5
switch i32 %1, label %16 [
i32 2400, label %17
i32 4800, label %11
i32 9600, label %12
i32 19200, label %13
i32 38400, label %14
i32 57600, label %15
]
11: ; preds = %10
br label %17
12: ; preds = %10
br label %17
13: ; preds = %10
br label %17
14: ; preds = %10
br label %17
15: ; preds = %10
br label %17
16: ; preds = %10
br label %17
17: ; preds = %10, %16, %15, %14, %13, %12, %11
%18 = phi i32* [ @B115200, %16 ], [ @B57600, %15 ], [ @B38400, %14 ], [ @B19200, %13 ], [ @B9600, %12 ], [ @B4800, %11 ], [ @B2400, %10 ]
%19 = load i32, i32* %18
%20 = call i32 @cfsetispeed(%struct.termios* %6, i32 %19)
%21 = call i32 @cfsetospeed(%struct.termios* %6, i32 %19)
%22 = load i32, i32* @CSIZE
%23 = xor i32 %22, -1
%24 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 2
%25 = load i32, i32* %24
%26 = and i32 %25, %23
%27 = icmp eq i32 %2, 7
%28 = load i32, i32* @CS7
%29 = load i32, i32* @CS8
%30 = select i1 %27, i32 %28, i32 %29
%31 = or i32 %30, %26
store i32 %31, i32* %24
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 0
%33 = load i32*, i32** %32
%34 = load i64, i64* @VTIME
%35 = getelementptr inbounds i32, i32* %33, i64 %34
store i32 0, i32* %35
%36 = load i64, i64* @VMIN
%37 = getelementptr inbounds i32, i32* %33, i64 %36
store i32 50, i32* %37
%38 = load i32, i32* @CRTSCTS
%39 = xor i32 %38, -1
%40 = load i32, i32* %24
%41 = and i32 %40, %39
store i32 %41, i32* %24
%42 = load i32, i32* @ICANON
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 1
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* @TCSANOW
%48 = call i64 @tcsetattr(i32 %0, i32 %47, %struct.termios* %6)
%49 = icmp eq i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %17, %5
%51 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %17 ]
%52 = call i32 @perror(i8* %51)
br label %53
53: ; preds = %50, %17
%54 = phi i32 [ 0, %17 ], [ -1, %50 ]
ret i32 %54
}