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

/* Variables and functions */
int B57600;
int CBAUD;
int CBAUDEX;
int /*<<< orphan*/ EINVAL;
int __MAX_BAUD;
int /*<<< orphan*/ errno;
int cf2setispeed(struct termios *tios, speed_t speed) {
  if ((speed & ~CBAUD) != 0 && (speed < B57600 || speed > __MAX_BAUD)) {
    errno = EINVAL;
    return -1;
  }
  tios->c_ispeed = speed;
  tios->c_cflag &= ~((CBAUD | CBAUDEX) << 16);
  tios->c_cflag |= (speed << 16);
  return 0;
}


x86 assembly (clang, -O0):
<cf2setispeed>:
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 <cf2setispeed+0x67>
mov    -0x14(%rbp),%eax
mov    0x2eb9(%rip),%rcx
cmp    (%rcx),%eax
jl     1149 <cf2setispeed+0x49>
mov    -0x14(%rbp),%eax
mov    0x2e97(%rip),%rcx
cmp    (%rcx),%eax
jle    1167 <cf2setispeed+0x67>
mov    0x2e70(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <cf2setispeed+0xa9>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    0x2e50(%rip),%rax
or     (%rax),%ecx
shl    $0x10,%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%ecx
shl    $0x10,%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):
<cf2setispeed>:
mov    0x2ea9(%rip),%rax
mov    (%rax),%ecx
not    %ecx
test   %esi,%ecx
je     1125 <cf2setispeed+0x25>
mov    0x2ed2(%rip),%rcx
cmp    %esi,(%rcx)
jg     1145 <cf2setispeed+0x45>
mov    0x2eb7(%rip),%rcx
cmp    %esi,(%rcx)
jl     1145 <cf2setispeed+0x45>
mov    %esi,(%rdi)
mov    0x2ea2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
shl    $0x10,%ecx
not    %ecx
and    0x4(%rdi),%ecx
shl    $0x10,%esi
or     %ecx,%esi
mov    %esi,0x4(%rdi)
xor    %eax,%eax
ret
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    0x2e73(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@CBAUD = global i32 0
@B57600 = global i32 0
@__MAX_BAUD = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@CBAUDEX = global i32 0
define i32 @cf2setispeed(%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* @__MAX_BAUD
%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 %40
21: ; preds = %15, %2
%22 = load i32, i32* %5
%23 = load %struct.termios*, %struct.termios** %4
%24 = getelementptr inbounds %struct.termios, %struct.termios* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* @CBAUD
%26 = load i32, i32* @CBAUDEX
%27 = or i32 %25, %26
%28 = shl i32 %27, 16
%29 = xor i32 %28, -1
%30 = load %struct.termios*, %struct.termios** %4
%31 = getelementptr inbounds %struct.termios, %struct.termios* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = and i32 %32, %29
store i32 %33, i32* %31
%34 = load i32, i32* %5
%35 = shl i32 %34, 16
%36 = load %struct.termios*, %struct.termios** %4
%37 = getelementptr inbounds %struct.termios, %struct.termios* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = or i32 %38, %35
store i32 %39, i32* %37
store i32 0, i32* %3
br label %40
40: ; preds = %21, %19
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@CBAUD = global i32 0
@B57600 = global i32 0
@__MAX_BAUD = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@CBAUDEX = global i32 0
define i32 @cf2setispeed(%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* @__MAX_BAUD
%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 %27
15: ; preds = %7, %2
%16 = getelementptr inbounds %struct.termios, %struct.termios* %0, i64 0, i32 0
store i32 %1, i32* %16
%17 = load i32, i32* @CBAUD
%18 = load i32, i32* @CBAUDEX
%19 = or i32 %18, %17
%20 = shl i32 %19, 16
%21 = xor i32 %20, -1
%22 = getelementptr inbounds %struct.termios, %struct.termios* %0, i64 0, i32 1
%23 = load i32, i32* %22
%24 = and i32 %23, %21
%25 = shl i32 %1, 16
%26 = or i32 %24, %25
store i32 %26, i32* %22
br label %27
27: ; preds = %15, %13
%28 = phi i32 [ -1, %13 ], [ 0, %15 ]
ret i32 %28
}