#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 termios2 {
  int c_ispeed;
  int c_ospeed;
  int /*<<< orphan*/ c_cflag;
};

/* Variables and functions */
int /*<<< orphan*/ BOTHER;
int /*<<< orphan*/ CBAUD;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCGETS2;
int /*<<< orphan*/ TCSETS2;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct termios2 *);
int open(char const *, int);
int serial_open(const char *device, int rate) {
  int fd;
  struct termios2 tio;
  if ((fd = open(device, O_RDWR | O_NOCTTY)) == -1)
    return -1;
  ioctl(fd, TCGETS2, &tio);
  tio.c_cflag &= ~CBAUD;
  tio.c_cflag |= BOTHER;
  tio.c_ispeed = rate;
  tio.c_ospeed = rate;
  ioctl(fd, TCSETS2, &tio);
  return fd;
}


x86 assembly (clang, -O0):
<serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2eae(%rip),%rax
mov    (%rax),%esi
mov    0x2e85(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1162 <serial_open+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <serial_open+0x9f>
mov    -0x18(%rbp),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    0x2eb7(%rip),%rax
mov    0x2e90(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1194 <serial_open+0x74>
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r14
mov    %ebp,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2e4c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,(%rsp)
mov    %ebx,0x4(%rsp)
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios2 = type { i32, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCGETS2 = global i32 0
@CBAUD = global i32 0
@BOTHER = global i32 0
@TCSETS2 = global i32 0
define i32 @serial_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios2
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11)
store i32 %12, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %36
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* @TCGETS2
%18 = call i32 @ioctl(i32 %16, i32 %17, %struct.termios2* %7)
%19 = load i32, i32* @CBAUD
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios2, %struct.termios2* %7, i32 0, i32 2
%22 = load i32, i32* %21
%23 = and i32 %22, %20
store i32 %23, i32* %21
%24 = load i32, i32* @BOTHER
%25 = getelementptr inbounds %struct.termios2, %struct.termios2* %7, i32 0, i32 2
%26 = load i32, i32* %25
%27 = or i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* %5
%29 = getelementptr inbounds %struct.termios2, %struct.termios2* %7, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %5
%31 = getelementptr inbounds %struct.termios2, %struct.termios2* %7, i32 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* %6
%33 = load i32, i32* @TCSETS2
%34 = call i32 @ioctl(i32 %32, i32 %33, %struct.termios2* %7)
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %15, %14
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.termios2 = type { i32, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCGETS2 = global i32 0
@CBAUD = global i32 0
@BOTHER = global i32 0
@TCSETS2 = global i32 0
define i32 @serial_open(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios2
%4 = bitcast %struct.termios2* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %24, label %10
10: ; preds = %2
%11 = load i32, i32* @TCGETS2
%12 = call i32 @ioctl(i32 %8, i32 %11, %struct.termios2* %3)
%13 = load i32, i32* @CBAUD
%14 = xor i32 %13, -1
%15 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 2
%16 = load i32, i32* %15
%17 = and i32 %16, %14
%18 = load i32, i32* @BOTHER
%19 = or i32 %18, %17
store i32 %19, i32* %15
%20 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 0
store i32 %1, i32* %20
%21 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 1
store i32 %1, i32* %21
%22 = load i32, i32* @TCSETS2
%23 = call i32 @ioctl(i32 %8, i32 %22, %struct.termios2* %3)
br label %24
24: ; preds = %2, %10
ret i32 %8
}