#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
#define TCSADRAIN 130
#define TCSAFLUSH 129
#define TCSANOW 128
int TCSETS;
int TCSETSF;
int TCSETSW;
int /*<<< orphan*/ errno;
int ioctl(int, int, struct termios const *);
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p) {
  int cmd;
  switch (optional_actions) {
  case TCSANOW:
    cmd = TCSETS;
    break;
  case TCSADRAIN:
    cmd = TCSETSW;
    break;
  case TCSAFLUSH:
    cmd = TCSETSF;
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  return ioctl(fd, cmd, termios_p);
}


x86 assembly (clang, -O0):
<tcsetattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     115e <tcsetattr+0x4e>
jmp    1138 <tcsetattr+0x28>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
je     1180 <tcsetattr+0x70>
jmp    114b <tcsetattr+0x3b>
mov    -0x20(%rbp),%eax
sub    $0x82,%eax
je     116f <tcsetattr+0x5f>
jmp    1191 <tcsetattr+0x81>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11af <tcsetattr+0x9f>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11af <tcsetattr+0x9f>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11af <tcsetattr+0x9f>
mov    0x2e38(%rip),%rax
mov    (%rax),%ecx
mov    0x2e37(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <tcsetattr+0xb1>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -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):
<tcsetattr>:
add    $0xffffff80,%esi
cmp    $0x3,%esi
jae    112d <tcsetattr+0x1d>
movslq %esi,%rax
lea    0x2cd6(%rip),%rcx
mov    (%rcx,%rax,8),%rax
mov    (%rax),%esi
jmp    1030 <ioctl@plt>
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
mov    0x2ea3(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@TCSETS = global i32 0
@TCSETSW = global i32 0
@TCSETSF = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @tcsetattr(i32 %0, i32 %1, %struct.termios* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios*
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.termios* %2, %struct.termios** %7
%9 = load i32, i32* %6
switch i32 %9, label %16 [
i32 128, label %10
i32 130, label %12
i32 129, label %14
]
10: ; preds = %3
%11 = load i32, i32* @TCSETS
store i32 %11, i32* %8
br label %18
12: ; preds = %3
%13 = load i32, i32* @TCSETSW
store i32 %13, i32* %8
br label %18
14: ; preds = %3
%15 = load i32, i32* @TCSETSF
store i32 %15, i32* %8
br label %18
16: ; preds = %3
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32 -1, i32* %4
br label %23
18: ; preds = %14, %12, %10
%19 = load i32, i32* %5
%20 = load i32, i32* %8
%21 = load %struct.termios*, %struct.termios** %7
%22 = call i32 @ioctl(i32 %19, i32 %20, %struct.termios* %21)
store i32 %22, i32* %4
br label %23
23: ; preds = %18, %16
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TCSETS = global i32 0
@TCSETSW = global i32 0
@TCSETSF = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@switch.table.tcsetattr = [3 x i32*] [i32* @TCSETS, i32* @TCSETSF, i32* @TCSETSW]
define i32 @tcsetattr(i32 %0, i32 %1, %struct.termios* %2) {
%4 = add i32 %1, -128
%5 = icmp ult i32 %4, 3
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
br label %14
8: ; preds = %3
%9 = sext i32 %4 to i64
%10 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.tcsetattr, i64 0, i64 %9
%11 = load i32*, i32** %10
%12 = load i32, i32* %11
%13 = tail call i32 @ioctl(i32 %0, i32 %12, %struct.termios* %2)
br label %14
14: ; preds = %8, %6
%15 = phi i32 [ -1, %6 ], [ %13, %8 ]
ret i32 %15
}