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

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


x86 assembly (clang, -O0):
<cf2setispeed_custom>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1133 <cf2setispeed_custom+0x33>
mov    0x2eac(%rip),%rax
mov    (%rax),%ecx
mov    0x2eab(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <cf2setispeed_custom+0xbb>
cmpl   $0x0,-0x14(%rbp)
jne    1176 <cf2setispeed_custom+0x76>
mov    0x2e74(%rip),%rax
mov    (%rax),%ecx
mov    0x2e8b(%rip),%rax
or     (%rax),%ecx
shl    $0x10,%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
mov    0x2e7c(%rip),%rax
mov    (%rax),%ecx
shl    $0x10,%ecx
mov    -0x10(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
jmp    11b4 <cf2setispeed_custom+0xb4>
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e52(%rip),%rax
or     (%rax),%ecx
shl    $0x10,%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
shl    $0x10,%ecx
mov    -0x10(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<cf2setispeed_custom>:
test   %esi,%esi
js     1132 <cf2setispeed_custom+0x32>
mov    0x2ead(%rip),%rdx
mov    0x2ec6(%rip),%rcx
mov    (%rcx),%eax
or     (%rdx),%eax
shl    $0x10,%eax
not    %eax
and    (%rdi),%eax
mov    %eax,(%rdi)
test   %esi,%esi
je     114a <cf2setispeed_custom+0x4a>
mov    (%rcx),%ecx
shl    $0x10,%ecx
or     %eax,%ecx
mov    %ecx,(%rdi)
mov    %esi,0x4(%rdi)
xor    %eax,%eax
ret
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    0x2e8e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%ecx
shl    $0x10,%ecx
or     %eax,%ecx
mov    %ecx,(%rdi)
xor    %eax,%eax
ret

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

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