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

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ B19200;
int /*<<< orphan*/ B38400;
int /*<<< orphan*/ B57600;
int /*<<< orphan*/ B9600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_tty_defaults(int fd, int baud) {
  struct termios termios;
  if (tcgetattr(fd, &termios) != 0)
    return -1;
  termios.c_iflag = 0;
  termios.c_oflag = 0;
  termios.c_cflag = CS8 | CLOCAL | CREAD;
  termios.c_lflag = 0;
  switch (baud) {
  case 115200:
    cfsetspeed(&termios, B115200);
    break;
  case 57600:
    cfsetspeed(&termios, B57600);
    break;
  case 38400:
    cfsetspeed(&termios, B38400);
    break;
  case 19200:
    cfsetspeed(&termios, B19200);
    break;
  case 9600:
    cfsetspeed(&termios, B9600);
    break;
  default:
    return -1;
  }
  if (tcsetattr(fd, TCSANOW, &termios) != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<set_tty_defaults>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
je     1160 <set_tty_defaults+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <set_tty_defaults+0x17d>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%ecx
or     %ecx,%eax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%ecx
or     %ecx,%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x2580,%eax
je     1258 <set_tty_defaults+0x128>
jmp    11b0 <set_tty_defaults+0x80>
mov    -0x34(%rbp),%eax
sub    $0x4b00,%eax
je     1241 <set_tty_defaults+0x111>
jmp    11c3 <set_tty_defaults+0x93>
mov    -0x34(%rbp),%eax
sub    $0x9600,%eax
je     122a <set_tty_defaults+0xfa>
jmp    11d6 <set_tty_defaults+0xa6>
mov    -0x34(%rbp),%eax
sub    $0xe100,%eax
je     1213 <set_tty_defaults+0xe3>
jmp    11e9 <set_tty_defaults+0xb9>
mov    -0x34(%rbp),%eax
sub    $0x1c200,%eax
jne    126f <set_tty_defaults+0x13f>
jmp    11fc <set_tty_defaults+0xcc>
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetspeed@plt>
jmp    127b <set_tty_defaults+0x14b>
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetspeed@plt>
jmp    127b <set_tty_defaults+0x14b>
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetspeed@plt>
jmp    127b <set_tty_defaults+0x14b>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetspeed@plt>
jmp    127b <set_tty_defaults+0x14b>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetspeed@plt>
jmp    127b <set_tty_defaults+0x14b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <set_tty_defaults+0x17d>
mov    -0x8(%rbp),%edi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
je     12a6 <set_tty_defaults+0x176>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <set_tty_defaults+0x17d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_tty_defaults>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1040 <tcgetattr@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1207 <set_tty_defaults+0xd7>
mov    0x2e6c(%rip),%rcx
mov    0x2e3d(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
xorps  %xmm0,%xmm0
mov    0x2e5f(%rip),%rcx
or     (%rcx),%edx
movups %xmm0,0x18(%rsp)
mov    %edx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
cmp    $0x95ff,%ebp
jle    11ae <set_tty_defaults+0x7e>
cmp    $0x9600,%ebp
je     11c7 <set_tty_defaults+0x97>
cmp    $0x1c200,%ebp
je     11d0 <set_tty_defaults+0xa0>
cmp    $0xe100,%ebp
jne    1207 <set_tty_defaults+0xd7>
mov    0x2e14(%rip),%rax
jmp    11e0 <set_tty_defaults+0xb0>
cmp    $0x2580,%ebp
je     11d9 <set_tty_defaults+0xa9>
cmp    $0x4b00,%ebp
jne    1207 <set_tty_defaults+0xd7>
mov    0x2e2b(%rip),%rax
jmp    11e0 <set_tty_defaults+0xb0>
mov    0x2dca(%rip),%rax
jmp    11e0 <set_tty_defaults+0xb0>
mov    0x2dd9(%rip),%rax
jmp    11e0 <set_tty_defaults+0xb0>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <cfsetspeed@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1050 <tcsetattr@plt>
neg    %rax
sbb    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i64 }
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@B57600 = global i32 0
@B38400 = global i32 0
@B19200 = global i32 0
@B9600 = global i32 0
@TCSANOW = global i32 0
define i32 @set_tty_defaults(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = call i64 @tcgetattr(i32 %7, %struct.termios* %6)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %45
11: ; preds = %2
%12 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 0, i64* %13
%14 = load i32, i32* @CS8
%15 = load i32, i32* @CLOCAL
%16 = or i32 %14, %15
%17 = load i32, i32* @CREAD
%18 = or i32 %16, %17
%19 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* %5
switch i32 %21, label %37 [
i32 115200, label %22
i32 57600, label %25
i32 38400, label %28
i32 19200, label %31
i32 9600, label %34
]
22: ; preds = %11
%23 = load i32, i32* @B115200
%24 = call i32 @cfsetspeed(%struct.termios* %6, i32 %23)
br label %38
25: ; preds = %11
%26 = load i32, i32* @B57600
%27 = call i32 @cfsetspeed(%struct.termios* %6, i32 %26)
br label %38
28: ; preds = %11
%29 = load i32, i32* @B38400
%30 = call i32 @cfsetspeed(%struct.termios* %6, i32 %29)
br label %38
31: ; preds = %11
%32 = load i32, i32* @B19200
%33 = call i32 @cfsetspeed(%struct.termios* %6, i32 %32)
br label %38
34: ; preds = %11
%35 = load i32, i32* @B9600
%36 = call i32 @cfsetspeed(%struct.termios* %6, i32 %35)
br label %38
37: ; preds = %11
store i32 -1, i32* %3
br label %45
38: ; preds = %34, %31, %28, %25, %22
%39 = load i32, i32* %4
%40 = load i32, i32* @TCSANOW
%41 = call i64 @tcsetattr(i32 %39, i32 %40, %struct.termios* %6)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 -1, i32* %3
br label %45
44: ; preds = %38
store i32 0, i32* %3
br label %45
45: ; preds = %44, %43, %37, %10
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i64 }
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@B57600 = global i32 0
@B38400 = global i32 0
@B19200 = global i32 0
@B9600 = global i32 0
@TCSANOW = global i32 0
define i32 @set_tty_defaults(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %29
7: ; preds = %2
%8 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%9 = bitcast i64* %8 to i8*
%10 = load i32, i32* @CS8
%11 = load i32, i32* @CLOCAL
%12 = or i32 %11, %10
%13 = load i32, i32* @CREAD
%14 = or i32 %12, %13
%15 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
store i64 0, i64* %16
switch i32 %1, label %29 [
i32 115200, label %21
i32 57600, label %17
i32 38400, label %18
i32 19200, label %19
i32 9600, label %20
]
17: ; preds = %7
br label %21
18: ; preds = %7
br label %21
19: ; preds = %7
br label %21
20: ; preds = %7
br label %21
21: ; preds = %7, %20, %19, %18, %17
%22 = phi i32* [ @B9600, %20 ], [ @B19200, %19 ], [ @B38400, %18 ], [ @B57600, %17 ], [ @B115200, %7 ]
%23 = load i32, i32* %22
%24 = call i32 @cfsetspeed(%struct.termios* %3, i32 %23)
%25 = load i32, i32* @TCSANOW
%26 = call i64 @tcsetattr(i32 %0, i32 %25, %struct.termios* %3)
%27 = icmp ne i64 %26, 0
%28 = sext i1 %27 to i32
br label %29
29: ; preds = %21, %7, %2
%30 = phi i32 [ -1, %2 ], [ -1, %7 ], [ %28, %21 ]
ret i32 %30
}