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

/* Variables and functions */
int /*<<< orphan*/ BOTHER;
int /*<<< orphan*/ CBAUD;
int /*<<< orphan*/ TCGETS2;
int /*<<< orphan*/ TCSETS2;
int ioctl(int, int /*<<< orphan*/, struct termios2 *);
int set_custom_baudrate(int fd, unsigned int baud) {
  struct termios2 tio;
  if (ioctl(fd, TCGETS2, &tio)) {
    return -1;
  }
  tio.c_cflag &= ~CBAUD;
  tio.c_cflag |= BOTHER;
  tio.c_ispeed = baud;
  tio.c_ospeed = baud;
  return ioctl(fd, TCSETS2, &tio);
}


x86 assembly (clang, -O0):
<set_custom_baudrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eb8(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%eax
je     1148 <set_custom_baudrate+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    118d <set_custom_baudrate+0x7d>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_custom_baudrate>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2ebf(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    1171 <set_custom_baudrate+0x61>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x10(%rsp),%eax
mov    0x2e82(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,0x8(%rsp)
mov    %ebp,0xc(%rsp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

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