#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 dummy;
};
typedef int int32_t;

/* Variables and functions */
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*/ perror(char *);
int serial_convertBaudRateToFlag(int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int32_t serial_set_baudrate(int32_t fd, int32_t baudrate) {
  struct termios tty;
  int32_t speedFlags;
  speedFlags = serial_convertBaudRateToFlag(baudrate);
  memset(&tty, 0, sizeof tty);
  if (tcgetattr(fd, &tty) != 0) {
    perror("error from tcgetattr");
    return -1;
  }
  cfsetospeed(&tty, speedFlags);
  cfsetispeed(&tty, speedFlags);
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    perror("errorsetting term attributes");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<serial_set_baudrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <serial_convertBaudRateToFlag@plt>
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
je     11c7 <serial_set_baudrate+0x57>
lea    0xe4a(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <serial_set_baudrate+0xad>
mov    -0x14(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0x14(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     1216 <serial_set_baudrate+0xa6>
lea    0xe10(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <serial_set_baudrate+0xad>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_set_baudrate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r12d
mov    %esi,%edi
call   1050 <serial_convertBaudRateToFlag@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %r12d,%edi
mov    %r15,%rsi
call   1060 <tcgetattr@plt>
test   %rax,%rax
jne    11f1 <serial_set_baudrate+0x81>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <cfsetospeed@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1080 <cfsetispeed@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %rbp,%rdx
call   1070 <tcsetattr@plt>
test   %rax,%rax
jne    11fa <serial_set_baudrate+0x8a>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe08(%rip),%rdi
jmp    1201 <serial_set_baudrate+0x91>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11e1 <serial_set_baudrate+0x71>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [21 x i8] c"error from tcgetattr\00"
@TCSANOW = global i32 0
@.str.1 = [29 x i8] c"errorsetting term attributes\00"
define i32 @serial_set_baudrate(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = call i32 @serial_convertBaudRateToFlag(i32 %8)
store i32 %9, i32* %7
%10 = call i32 @memset(%struct.termios* %6, i32 0, i32 4)
%11 = load i32, i32* %4
%12 = call i64 @tcgetattr(i32 %11, %struct.termios* %6)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %28
16: ; preds = %2
%17 = load i32, i32* %7
%18 = call i32 @cfsetospeed(%struct.termios* %6, i32 %17)
%19 = load i32, i32* %7
%20 = call i32 @cfsetispeed(%struct.termios* %6, i32 %19)
%21 = load i32, i32* %4
%22 = load i32, i32* @TCSANOW
%23 = call i64 @tcsetattr(i32 %21, i32 %22, %struct.termios* %6)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %28
27: ; preds = %16
store i32 0, i32* %3
br label %28
28: ; preds = %27, %25, %14
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [21 x i8] c"error from tcgetattr\00"
@TCSANOW = global i32 0
@.str.1 = [29 x i8] c"errorsetting term attributes\00"
define i32 @serial_set_baudrate(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = tail call i32 @serial_convertBaudRateToFlag(i32 %1)
%6 = call i32 @memset(%struct.termios* %3, i32 0, i32 4)
%7 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = call i32 @cfsetospeed(%struct.termios* %3, i32 %5)
%11 = call i32 @cfsetispeed(%struct.termios* %3, i32 %5)
%12 = load i32, i32* @TCSANOW
%13 = call i64 @tcsetattr(i32 %0, i32 %12, %struct.termios* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %9, %2
%16 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %9 ]
%17 = call i32 @perror(i8* %16)
br label %18
18: ; preds = %15, %9
%19 = phi i32 [ 0, %9 ], [ -1, %15 ]
ret i32 %19
}