#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;
};

/* Variables and functions */
int PARENB;
int PARODD;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int errno;
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_set_interface_attribs(int fd, int speed, int parity) {
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  if (tcgetattr(fd, &tty) != 0) {
    printf("error %d from tcgetattr", errno);
    return -1;
  }
  cfsetospeed(&tty, speed);
  cfsetispeed(&tty, speed);
  cfmakeraw(&tty);
  tty.c_cflag &= ~(PARENB | PARODD);
  tty.c_cflag |= parity;
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    printf("error %d from tcsetattr", errno);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<serial_set_interface_attribs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
je     11c8 <serial_set_interface_attribs+0x58>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <serial_set_interface_attribs+0xe4>
mov    -0xc(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0xc(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1090 <cfsetispeed@plt>
lea    -0x18(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
je     124d <serial_set_interface_attribs+0xdd>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    0xddc(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <serial_set_interface_attribs+0xe4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_set_interface_attribs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r13d
mov    %edi,%r12d
mov    %rsp,%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1070 <tcgetattr@plt>
test   %rax,%rax
je     11b1 <serial_set_interface_attribs+0x41>
lea    0xe51(%rip),%rdi
jmp    120f <serial_set_interface_attribs+0x9f>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r13d,%esi
call   1030 <cfsetospeed@plt>
mov    %r15,%rdi
mov    %r13d,%esi
call   1090 <cfsetispeed@plt>
mov    %r15,%rdi
call   1060 <cfmakeraw@plt>
mov    0x2e0f(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
or     %r14d,%ecx
mov    %ecx,(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r15,%rdx
call   1080 <tcsetattr@plt>
test   %rax,%rax
je     1222 <serial_set_interface_attribs+0xb2>
lea    0xe09(%rip),%rdi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [24 x i8] c"error %d from tcgetattr\00"
@errno = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@TCSANOW = global i32 0
@.str.1 = [24 x i8] c"error %d from tcsetattr\00"
define i32 @serial_set_interface_attribs(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = call i32 @memset(%struct.termios* %8, i32 0, i32 4)
%10 = load i32, i32* %5
%11 = call i64 @tcgetattr(i32 %10, %struct.termios* %8)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @errno
%15 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 -1, i32* %4
br label %41
16: ; preds = %3
%17 = load i32, i32* %6
%18 = call i32 @cfsetospeed(%struct.termios* %8, i32 %17)
%19 = load i32, i32* %6
%20 = call i32 @cfsetispeed(%struct.termios* %8, i32 %19)
%21 = call i32 @cfmakeraw(%struct.termios* %8)
%22 = load i32, i32* @PARENB
%23 = load i32, i32* @PARODD
%24 = or i32 %22, %23
%25 = xor i32 %24, -1
%26 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%27 = load i32, i32* %26
%28 = and i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* %7
%30 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* %5
%34 = load i32, i32* @TCSANOW
%35 = call i64 @tcsetattr(i32 %33, i32 %34, %struct.termios* %8)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %16
%38 = load i32, i32* @errno
%39 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %38)
store i32 -1, i32* %4
br label %41
40: ; preds = %16
store i32 0, i32* %4
br label %41
41: ; preds = %40, %37, %13
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [24 x i8] c"error %d from tcgetattr\00"
@errno = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@TCSANOW = global i32 0
@.str.1 = [24 x i8] c"error %d from tcsetattr\00"
define i32 @serial_set_interface_attribs(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = call i32 @memset(%struct.termios* %4, i32 0, i32 4)
%7 = call i64 @tcgetattr(i32 %0, %struct.termios* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %3
%10 = call i32 @cfsetospeed(%struct.termios* %4, i32 %1)
%11 = call i32 @cfsetispeed(%struct.termios* %4, i32 %1)
%12 = call i32 @cfmakeraw(%struct.termios* %4)
%13 = load i32, i32* @PARENB
%14 = load i32, i32* @PARODD
%15 = or i32 %14, %13
%16 = xor i32 %15, -1
%17 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %18, %16
%20 = or i32 %19, %2
store i32 %20, i32* %17
%21 = load i32, i32* @TCSANOW
%22 = call i64 @tcsetattr(i32 %0, i32 %21, %struct.termios* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %9, %3
%25 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %9 ]
%26 = load i32, i32* @errno
%27 = call i32 @printf(i8* %25, i32 %26)
br label %28
28: ; preds = %24, %9
%29 = phi i32 [ 0, %9 ], [ -1, %24 ]
ret i32 %29
}