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

/* Variables and functions */
int EINVAL;
int PARENB;
int PARODD;
int toupper(char);
int tty_set_parity(struct termios *tty, char *parity) {
  switch (toupper(*parity)) {
  case 'N':
    tty->c_cflag &= ~(PARENB | PARODD);
    break;
  case 'O':
    tty->c_cflag &= ~(PARENB | PARODD);
    tty->c_cflag |= (PARENB | PARODD);
    break;
  case 'E':
    tty->c_cflag &= ~(PARENB | PARODD);
    tty->c_cflag |= (PARENB);
    break;
  default:
    return -EINVAL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tty_set_parity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0x45,%eax
je     11bf <tty_set_parity+0xaf>
jmp    113f <tty_set_parity+0x2f>
mov    -0x1c(%rbp),%eax
sub    $0x4e,%eax
je     1161 <tty_set_parity+0x51>
jmp    1150 <tty_set_parity+0x40>
mov    -0x1c(%rbp),%eax
sub    $0x4f,%eax
je     1183 <tty_set_parity+0x73>
jmp    11f2 <tty_set_parity+0xe2>
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6f(%rip),%rax
or     (%rax),%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
jmp    1205 <tty_set_parity+0xf5>
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4d(%rip),%rax
or     (%rax),%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    0x2e30(%rip),%rax
or     (%rax),%ecx
mov    -0x10(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
jmp    1205 <tty_set_parity+0xf5>
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
mov    0x2e11(%rip),%rax
or     (%rax),%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
jmp    1205 <tty_set_parity+0xf5>
xor    %eax,%eax
mov    0x2dd5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <tty_set_parity+0xfc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_set_parity>:
push   %rbx
mov    %rdi,%rbx
movsbl (%rsi),%edi
call   1030 <toupper@plt>
cmp    $0x45,%eax
je     1143 <tty_set_parity+0x33>
cmp    $0x4f,%eax
je     1161 <tty_set_parity+0x51>
cmp    $0x4e,%eax
jne    1183 <tty_set_parity+0x73>
mov    0x2eb6(%rip),%rax
mov    0x2ea7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rbx)
jmp    117f <tty_set_parity+0x6f>
mov    0x2e9e(%rip),%rax
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    (%rbx),%ecx
mov    %ecx,(%rbx)
or     (%rax),%ecx
mov    %ecx,(%rbx)
jmp    117f <tty_set_parity+0x6f>
mov    0x2e80(%rip),%rax
mov    0x2e71(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
not    %edx
and    (%rbx),%edx
mov    %edx,(%rbx)
or     (%rax),%edx
or     (%rcx),%edx
mov    %edx,(%rbx)
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e46(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@PARENB = global i32 0
@PARODD = global i32 0
@EINVAL = global i32 0
define i32 @tty_set_parity(%struct.termios* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.termios*
%5 = alloca i8*
store %struct.termios* %0, %struct.termios** %4
store i8* %1, i8** %5
%6 = load i8*, i8** %5
%7 = load i8, i8* %6
%8 = call i32 @toupper(i8 signext %7)
switch i32 %8, label %48 [
i32 78, label %9
i32 79, label %18
i32 69, label %34
]
9: ; preds = %2
%10 = load i32, i32* @PARENB
%11 = load i32, i32* @PARODD
%12 = or i32 %10, %11
%13 = xor i32 %12, -1
%14 = load %struct.termios*, %struct.termios** %4
%15 = getelementptr inbounds %struct.termios, %struct.termios* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = and i32 %16, %13
store i32 %17, i32* %15
br label %51
18: ; preds = %2
%19 = load i32, i32* @PARENB
%20 = load i32, i32* @PARODD
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = load %struct.termios*, %struct.termios** %4
%24 = getelementptr inbounds %struct.termios, %struct.termios* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = and i32 %25, %22
store i32 %26, i32* %24
%27 = load i32, i32* @PARENB
%28 = load i32, i32* @PARODD
%29 = or i32 %27, %28
%30 = load %struct.termios*, %struct.termios** %4
%31 = getelementptr inbounds %struct.termios, %struct.termios* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = or i32 %32, %29
store i32 %33, i32* %31
br label %51
34: ; preds = %2
%35 = load i32, i32* @PARENB
%36 = load i32, i32* @PARODD
%37 = or i32 %35, %36
%38 = xor i32 %37, -1
%39 = load %struct.termios*, %struct.termios** %4
%40 = getelementptr inbounds %struct.termios, %struct.termios* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, %38
store i32 %42, i32* %40
%43 = load i32, i32* @PARENB
%44 = load %struct.termios*, %struct.termios** %4
%45 = getelementptr inbounds %struct.termios, %struct.termios* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = or i32 %46, %43
store i32 %47, i32* %45
br label %51
48: ; preds = %2
%49 = load i32, i32* @EINVAL
%50 = sub nsw i32 0, %49
store i32 %50, i32* %3
br label %52
51: ; preds = %34, %18, %9
store i32 0, i32* %3
br label %52
52: ; preds = %51, %48
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@PARENB = global i32 0
@PARODD = global i32 0
@EINVAL = global i32 0
define i32 @tty_set_parity(%struct.termios* nocapture %0, i8* nocapture %1) {
%3 = load i8, i8* %1
%4 = tail call i32 @toupper(i8 signext %3)
switch i32 %4, label %35 [
i32 78, label %5
i32 79, label %13
i32 69, label %25
]
5: ; preds = %2
%6 = load i32, i32* @PARENB
%7 = load i32, i32* @PARODD
%8 = or i32 %7, %6
%9 = xor i32 %8, -1
%10 = getelementptr inbounds %struct.termios, %struct.termios* %0, i64 0, i32 0
%11 = load i32, i32* %10
%12 = and i32 %11, %9
store i32 %12, i32* %10
br label %38
13: ; preds = %2
%14 = load i32, i32* @PARENB
%15 = load i32, i32* @PARODD
%16 = or i32 %15, %14
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %0, i64 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* @PARENB
%22 = load i32, i32* @PARODD
%23 = or i32 %21, %20
%24 = or i32 %23, %22
store i32 %24, i32* %18
br label %38
25: ; preds = %2
%26 = load i32, i32* @PARENB
%27 = load i32, i32* @PARODD
%28 = or i32 %27, %26
%29 = xor i32 %28, -1
%30 = getelementptr inbounds %struct.termios, %struct.termios* %0, i64 0, i32 0
%31 = load i32, i32* %30
%32 = and i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @PARENB
%34 = or i32 %33, %32
store i32 %34, i32* %30
br label %38
35: ; preds = %2
%36 = load i32, i32* @EINVAL
%37 = sub nsw i32 0, %36
br label %38
38: ; preds = %5, %13, %25, %35
%39 = phi i32 [ %37, %35 ], [ 0, %25 ], [ 0, %13 ], [ 0, %5 ]
ret i32 %39
}