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

/* Variables and functions */
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IXANY;
int ONLRET;
int ONOCR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_set_attribs(int fd) {
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  tcgetattr(fd, &tty);
  tty.c_iflag = IXANY;
  tty.c_oflag = ONOCR | ONLRET;
  tty.c_cflag = CS8 | CREAD | CLOCAL;
  tty.c_lflag = 0;
  cfsetispeed(&tty, 0010015);
  cfsetospeed(&tty, 0010015);
  if (tcsetattr(fd, TCSANOW, &tty) != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<serial_set_attribs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    0x2e25(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    0x2e30(%rip),%rcx
or     (%rcx),%eax
mov    0x2df7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x100d,%esi
call   1070 <cfsetispeed@plt>
lea    -0x20(%rbp),%rdi
mov    $0x100d,%esi
call   1030 <cfsetospeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     1205 <serial_set_attribs+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <serial_set_attribs+0xbc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_set_attribs>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e6c(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ecx,(%rsp)
mov    0x2e32(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2df8(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x4(%rsp)
movq   $0x0,0x8(%rsp)
mov    %r14,%rdi
mov    $0x100d,%esi
call   1070 <cfsetispeed@plt>
mov    %r14,%rdi
mov    $0x100d,%esi
call   1030 <cfsetospeed@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
neg    %rax
sbb    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i64, i32 }
@IXANY = global i32 0
@ONOCR = global i32 0
@ONLRET = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@TCSANOW = global i32 0
define i32 @serial_set_attribs(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %3
%5 = call i32 @memset(%struct.termios* %4, i32 0, i32 24)
%6 = load i32, i32* %3
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %4)
%8 = load i32, i32* @IXANY
%9 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* @ONOCR
%11 = load i32, i32* @ONLRET
%12 = or i32 %10, %11
%13 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @CS8
%15 = load i32, i32* @CREAD
%16 = or i32 %14, %15
%17 = load i32, i32* @CLOCAL
%18 = or i32 %16, %17
%19 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
store i64 0, i64* %20
%21 = call i32 @cfsetispeed(%struct.termios* %4, i32 4109)
%22 = call i32 @cfsetospeed(%struct.termios* %4, i32 4109)
%23 = load i32, i32* %3
%24 = load i32, i32* @TCSANOW
%25 = call i64 @tcsetattr(i32 %23, i32 %24, %struct.termios* %4)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %1
store i32 -1, i32* %2
br label %29
28: ; preds = %1
store i32 0, i32* %2
br label %29
29: ; preds = %28, %27
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i64, i32 }
@IXANY = global i32 0
@ONOCR = global i32 0
@ONLRET = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@TCSANOW = global i32 0
define i32 @serial_set_attribs(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @memset(%struct.termios* %2, i32 0, i32 24)
%5 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%6 = load i32, i32* @IXANY
%7 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
store i32 %6, i32* %7
%8 = load i32, i32* @ONOCR
%9 = load i32, i32* @ONLRET
%10 = or i32 %9, %8
%11 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @CS8
%13 = load i32, i32* @CREAD
%14 = or i32 %13, %12
%15 = load i32, i32* @CLOCAL
%16 = or i32 %14, %15
%17 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
store i64 0, i64* %18
%19 = call i32 @cfsetispeed(%struct.termios* %2, i32 4109)
%20 = call i32 @cfsetospeed(%struct.termios* %2, i32 4109)
%21 = load i32, i32* @TCSANOW
%22 = call i64 @tcsetattr(i32 %0, i32 %21, %struct.termios* %2)
%23 = icmp ne i64 %22, 0
%24 = sext i1 %23 to i32
ret i32 %24
}