#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_iflag;
  int c_cflag;
};
typedef int speed_t;

/* Variables and functions */
int B460800;
int B57600;
int CBAUD;
int CBAUDEX;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int cfsetispeed(struct termios *termios_p, speed_t speed) {
  if ((speed & (speed_t)~CBAUD) != 0 && (speed < B57600 || speed > B460800)) {
    errno = EINVAL;
    return -1;
  }
  if (speed == 0)
    termios_p->c_iflag |= 020000000000;
  else {
    termios_p->c_iflag &= ~020000000000;
    termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
    termios_p->c_cflag |= speed;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cfsetispeed>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e9b(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1167 <cfsetispeed+0x67>
mov    -0x14(%rbp),%eax
mov    0x2eb9(%rip),%rcx
cmp    (%rcx),%eax
jl     1149 <cfsetispeed+0x49>
mov    -0x14(%rbp),%eax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%eax
jle    1167 <cfsetispeed+0x67>
mov    0x2e70(%rip),%rax
mov    (%rax),%ecx
mov    0x2e77(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <cfsetispeed+0xc5>
cmpl   $0x0,-0x14(%rbp)
jne    1184 <cfsetispeed+0x84>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
or     $0x80000000,%ecx
mov    %ecx,(%rax)
jmp    11be <cfsetispeed+0xbe>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
and    $0x7fffffff,%ecx
mov    %ecx,(%rax)
mov    0x2e17(%rip),%rax
mov    (%rax),%ecx
mov    0x2e36(%rip),%rax
or     (%rax),%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
or     0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<cfsetispeed>:
mov    0x2ea9(%rip),%rax
mov    (%rax),%ecx
not    %ecx
test   %esi,%ecx
je     1125 <cfsetispeed+0x25>
mov    0x2ed2(%rip),%rcx
cmp    %esi,(%rcx)
jg     114b <cfsetispeed+0x4b>
mov    0x2ea7(%rip),%rcx
cmp    %esi,(%rcx)
jl     114b <cfsetispeed+0x4b>
mov    (%rdi),%ecx
test   %esi,%esi
je     1163 <cfsetispeed+0x63>
and    $0x7fffffff,%ecx
mov    %ecx,(%rdi)
mov    0x2e9e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x4(%rdi),%ecx
or     %esi,%ecx
mov    %ecx,0x4(%rdi)
xor    %eax,%eax
ret
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    0x2e75(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret
or     $0x80000000,%ecx
mov    %ecx,(%rdi)
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@CBAUD = global i32 0
@B57600 = global i32 0
@B460800 = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@CBAUDEX = global i32 0
define i32 @cfsetispeed(%struct.termios* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.termios*
%5 = alloca i32
store %struct.termios* %0, %struct.termios** %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = load i32, i32* @CBAUD
%8 = xor i32 %7, -1
%9 = and i32 %6, %8
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %2
%12 = load i32, i32* %5
%13 = load i32, i32* @B57600
%14 = icmp slt i32 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* %5
%17 = load i32, i32* @B460800
%18 = icmp sgt i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15, %11
%20 = load i32, i32* @EINVAL
store i32 %20, i32* @errno
store i32 -1, i32* %3
br label %48
21: ; preds = %15, %2
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load %struct.termios*, %struct.termios** %4
%26 = getelementptr inbounds %struct.termios, %struct.termios* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = or i32 %27, -2147483648
store i32 %28, i32* %26
br label %47
29: ; preds = %21
%30 = load %struct.termios*, %struct.termios** %4
%31 = getelementptr inbounds %struct.termios, %struct.termios* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = and i32 %32, 2147483647
store i32 %33, i32* %31
%34 = load i32, i32* @CBAUD
%35 = load i32, i32* @CBAUDEX
%36 = or i32 %34, %35
%37 = xor i32 %36, -1
%38 = load %struct.termios*, %struct.termios** %4
%39 = getelementptr inbounds %struct.termios, %struct.termios* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = and i32 %40, %37
store i32 %41, i32* %39
%42 = load i32, i32* %5
%43 = load %struct.termios*, %struct.termios** %4
%44 = getelementptr inbounds %struct.termios, %struct.termios* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = or i32 %45, %42
store i32 %46, i32* %44
br label %47
47: ; preds = %29, %24
store i32 0, i32* %3
br label %48
48: ; preds = %47, %19
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@CBAUD = global i32 0
@B57600 = global i32 0
@B460800 = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@CBAUDEX = global i32 0
define i32 @cfsetispeed(%struct.termios* nocapture %0, i32 %1) {
%3 = load i32, i32* @CBAUD
%4 = xor i32 %3, -1
%5 = and i32 %4, %1
%6 = icmp eq i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %2
%8 = load i32, i32* @B57600
%9 = icmp sgt i32 %8, %1
%10 = load i32, i32* @B460800
%11 = icmp slt i32 %10, %1
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
br label %31
15: ; preds = %7, %2
%16 = icmp eq i32 %1, 0
%17 = getelementptr inbounds %struct.termios, %struct.termios* %0, i64 0, i32 0
%18 = load i32, i32* %17
br i1 %16, label %19, label %21
19: ; preds = %15
%20 = or i32 %18, -2147483648
store i32 %20, i32* %17
br label %31
21: ; preds = %15
%22 = and i32 %18, 2147483647
store i32 %22, i32* %17
%23 = load i32, i32* @CBAUD
%24 = load i32, i32* @CBAUDEX
%25 = or i32 %24, %23
%26 = xor i32 %25, -1
%27 = getelementptr inbounds %struct.termios, %struct.termios* %0, i64 0, i32 1
%28 = load i32, i32* %27
%29 = and i32 %28, %26
%30 = or i32 %29, %1
store i32 %30, i32* %27
br label %31
31: ; preds = %19, %21, %13
%32 = phi i32 [ -1, %13 ], [ 0, %21 ], [ 0, %19 ]
ret i32 %32
}