#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 /*<<< orphan*/ TCSETS;
int /*<<< orphan*/ TCSETSF;
int /*<<< orphan*/ TCSETSW;
int /*<<< orphan*/ errno;
int ioctl(int, int /*<<< orphan*/, struct termios const *);
int tcsetattr(int fildes, int optional_actions,
              const struct termios *termios_p) {
  int ret;
  switch (optional_actions) {
  case TCSANOW:
    ret = ioctl(fildes, TCSETS, termios_p);
    break;
  case TCSADRAIN:
    ret = ioctl(fildes, TCSETSW, termios_p);
    break;
  case TCSAFLUSH:
    ret = ioctl(fildes, TCSETSF, termios_p);
    break;
  default:
    errno = EINVAL;
    ret = -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<tcsetattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x80,%eax
je     115e <tcsetattr+0x4e>
jmp    1138 <tcsetattr+0x28>
mov    -0x18(%rbp),%eax
sub    $0x81,%eax
je     1198 <tcsetattr+0x88>
jmp    114b <tcsetattr+0x3b>
mov    -0x18(%rbp),%eax
sub    $0x82,%eax
je     117b <tcsetattr+0x6b>
jmp    11b5 <tcsetattr+0xa5>
mov    -0x4(%rbp),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
jmp    11ce <tcsetattr+0xbe>
mov    -0x4(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
jmp    11ce <tcsetattr+0xbe>
mov    -0x4(%rbp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
jmp    11ce <tcsetattr+0xbe>
mov    0x2e14(%rip),%rax
mov    (%rax),%ecx
mov    0x2e13(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcsetattr>:
cmp    $0x82,%esi
je     1136 <tcsetattr+0x26>
cmp    $0x81,%esi
je     1144 <tcsetattr+0x34>
cmp    $0x80,%esi
jne    1152 <tcsetattr+0x42>
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
jmp    1030 <ioctl@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
jmp    1030 <ioctl@plt>
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
jmp    1030 <ioctl@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    0x2e76(%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 %struct.termios*
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store %struct.termios* %2, %struct.termios** %6
%8 = load i32, i32* %5
switch i32 %8, label %24 [
i32 128, label %9
i32 130, label %14
i32 129, label %19
]
9: ; preds = %3
%10 = load i32, i32* %4
%11 = load i32, i32* @TCSETS
%12 = load %struct.termios*, %struct.termios** %6
%13 = call i32 @ioctl(i32 %10, i32 %11, %struct.termios* %12)
store i32 %13, i32* %7
br label %26
14: ; preds = %3
%15 = load i32, i32* %4
%16 = load i32, i32* @TCSETSW
%17 = load %struct.termios*, %struct.termios** %6
%18 = call i32 @ioctl(i32 %15, i32 %16, %struct.termios* %17)
store i32 %18, i32* %7
br label %26
19: ; preds = %3
%20 = load i32, i32* %4
%21 = load i32, i32* @TCSETSF
%22 = load %struct.termios*, %struct.termios** %6
%23 = call i32 @ioctl(i32 %20, i32 %21, %struct.termios* %22)
store i32 %23, i32* %7
br label %26
24: ; preds = %3
%25 = load i32, i32* @EINVAL
store i32 %25, i32* @errno
store i32 -1, i32* %7
br label %26
26: ; preds = %24, %19, %14, %9
%27 = load i32, i32* %7
ret i32 %27
}

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
define i32 @tcsetattr(i32 %0, i32 %1, %struct.termios* %2) {
switch i32 %1, label %13 [
i32 128, label %4
i32 130, label %7
i32 129, label %10
]
4: ; preds = %3
%5 = load i32, i32* @TCSETS
%6 = tail call i32 @ioctl(i32 %0, i32 %5, %struct.termios* %2)
br label %15
7: ; preds = %3
%8 = load i32, i32* @TCSETSW
%9 = tail call i32 @ioctl(i32 %0, i32 %8, %struct.termios* %2)
br label %15
10: ; preds = %3
%11 = load i32, i32* @TCSETSF
%12 = tail call i32 @ioctl(i32 %0, i32 %11, %struct.termios* %2)
br label %15
13: ; preds = %3
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
br label %15
15: ; preds = %13, %10, %7, %4
%16 = phi i32 [ -1, %13 ], [ %12, %10 ], [ %9, %7 ], [ %6, %4 ]
ret i32 %16
}