#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;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
#define CS7 129
#define CS8 128
int CSIZE;
int CSTOPB;
int PARENB;
int PARODD;
char *baud_string(int /*<<< orphan*/);
int /*<<< orphan*/ cfgetospeed(struct termios *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char, char, char);
void serparm_to_str(char *str, int strlen, struct termios *termctl) {
  speed_t speed = cfgetospeed(termctl);
  int stopbits = termctl->c_cflag & CSTOPB;
  int databits = termctl->c_cflag & CSIZE;
  int parity_enabled = termctl->c_cflag & PARENB;
  int parity = termctl->c_cflag & PARODD;
  char *sstr;
  char pchar, schar, dchar;
  sstr = baud_string(speed);
  if (stopbits)
    schar = '2';
  else
    schar = '1';
  switch (databits) {
  case CS7:
    dchar = '7';
    break;
  case CS8:
    dchar = '8';
    break;
  default:
    dchar = '?';
  }
  if (parity_enabled) {
    if (parity) {
      pchar = 'O';
    } else {
      pchar = 'E';
    }
  } else {
    pchar = 'N';
  }
  snprintf(str, strlen, "%s %c%c%c", sstr, pchar, dchar, schar);
}


x86 assembly (clang, -O0):
<serparm_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <cfgetospeed@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e64(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e6a(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e68(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e4e(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <baud_string@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11b6 <serparm_to_str+0x86>
movb   $0x32,-0x3a(%rbp)
jmp    11ba <serparm_to_str+0x8a>
movb   $0x31,-0x3a(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0x80,%eax
je     11ec <serparm_to_str+0xbc>
jmp    11d0 <serparm_to_str+0xa0>
mov    -0x40(%rbp),%eax
sub    $0x81,%eax
jne    11f5 <serparm_to_str+0xc5>
jmp    11e3 <serparm_to_str+0xb3>
movb   $0x37,-0x3b(%rbp)
jmp    11f9 <serparm_to_str+0xc9>
movb   $0x38,-0x3b(%rbp)
jmp    11f9 <serparm_to_str+0xc9>
movb   $0x3f,-0x3b(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     121f <serparm_to_str+0xef>
cmpl   $0x0,-0x2c(%rbp)
je     1216 <serparm_to_str+0xe6>
movb   $0x4f,-0x39(%rbp)
jmp    121a <serparm_to_str+0xea>
movb   $0x45,-0x39(%rbp)
jmp    1223 <serparm_to_str+0xf3>
movb   $0x4e,-0x39(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    -0x39(%rbp),%r8b
mov    -0x3b(%rbp),%al
lea    0xdc4(%rip),%rdx
movsbl %r8b,%r8d
movsbl %al,%r9d
movsbl -0x3a(%rbp),%eax
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serparm_to_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rdx,%rdi
call   1050 <cfgetospeed@plt>
mov    0x0(%rbp),%ebx
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%r12d
and    %ebx,%r12d
mov    0x2e75(%rip),%rcx
mov    (%rcx),%ebp
and    %ebx,%ebp
mov    0x2e7a(%rip),%rcx
mov    (%rcx),%r13d
and    %ebx,%r13d
mov    0x2e65(%rip),%rcx
and    (%rcx),%ebx
mov    %eax,%edi
call   1030 <baud_string@plt>
cmp    $0x80,%ebp
mov    $0x38,%ecx
mov    $0x3f,%edx
cmove  %ecx,%edx
cmp    $0x81,%ebp
mov    $0x37,%r9d
cmovne %edx,%r9d
test   %ebx,%ebx
mov    $0x45,%ecx
mov    $0x4f,%edx
cmove  %ecx,%edx
test   %r13d,%r13d
mov    $0x4e,%r8d
cmovne %edx,%r8d
cmp    $0x1,%r12d
mov    $0x32,%ecx
sbb    $0x0,%ecx
mov    %ecx,(%rsp)
lea    0xe27(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@CSTOPB = global i32 0
@CSIZE = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@.str = [10 x i8] c"%s %c%c%c\00"
define void @serparm_to_str(i8* %0, i32 %1, %struct.termios* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8
%14 = alloca i8
%15 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.termios* %2, %struct.termios** %6
%16 = load %struct.termios*, %struct.termios** %6
%17 = call i32 @cfgetospeed(%struct.termios* %16)
store i32 %17, i32* %7
%18 = load %struct.termios*, %struct.termios** %6
%19 = getelementptr inbounds %struct.termios, %struct.termios* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @CSTOPB
%22 = and i32 %20, %21
store i32 %22, i32* %8
%23 = load %struct.termios*, %struct.termios** %6
%24 = getelementptr inbounds %struct.termios, %struct.termios* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @CSIZE
%27 = and i32 %25, %26
store i32 %27, i32* %9
%28 = load %struct.termios*, %struct.termios** %6
%29 = getelementptr inbounds %struct.termios, %struct.termios* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @PARENB
%32 = and i32 %30, %31
store i32 %32, i32* %10
%33 = load %struct.termios*, %struct.termios** %6
%34 = getelementptr inbounds %struct.termios, %struct.termios* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @PARODD
%37 = and i32 %35, %36
store i32 %37, i32* %11
%38 = load i32, i32* %7
%39 = call i8* @baud_string(i32 %38)
store i8* %39, i8** %12
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %3
store i8 50, i8* %14
br label %44
43: ; preds = %3
store i8 49, i8* %14
br label %44
44: ; preds = %43, %42
%45 = load i32, i32* %9
switch i32 %45, label %48 [
i32 129, label %46
i32 128, label %47
]
46: ; preds = %44
store i8 55, i8* %15
br label %49
47: ; preds = %44
store i8 56, i8* %15
br label %49
48: ; preds = %44
store i8 63, i8* %15
br label %49
49: ; preds = %48, %47, %46
%50 = load i32, i32* %10
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %49
%53 = load i32, i32* %11
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %52
store i8 79, i8* %13
br label %57
56: ; preds = %52
store i8 69, i8* %13
br label %57
57: ; preds = %56, %55
br label %59
58: ; preds = %49
store i8 78, i8* %13
br label %59
59: ; preds = %58, %57
%60 = load i8*, i8** %4
%61 = load i32, i32* %5
%62 = load i8*, i8** %12
%63 = load i8, i8* %13
%64 = load i8, i8* %15
%65 = load i8, i8* %14
%66 = call i32 @snprintf(i8* %60, i32 %61, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %62, i8 signext %63, i8 signext %64, i8 signext %65)
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@CSTOPB = global i32 0
@CSIZE = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@.str = [10 x i8] c"%s %c%c%c\00"
define void @serparm_to_str(i8* %0, i32 %1, %struct.termios* %2) {
%4 = tail call i32 @cfgetospeed(%struct.termios* %2)
%5 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%6 = load i32, i32* %5
%7 = load i32, i32* @CSTOPB
%8 = and i32 %7, %6
%9 = load i32, i32* @CSIZE
%10 = and i32 %9, %6
%11 = load i32, i32* @PARENB
%12 = and i32 %11, %6
%13 = load i32, i32* @PARODD
%14 = and i32 %13, %6
%15 = tail call i8* @baud_string(i32 %4)
%16 = icmp eq i32 %8, 0
%17 = select i1 %16, i8 49, i8 50
%18 = icmp eq i32 %10, 128
%19 = select i1 %18, i8 56, i8 63
%20 = icmp eq i32 %10, 129
%21 = select i1 %20, i8 55, i8 %19
%22 = icmp eq i32 %12, 0
%23 = icmp eq i32 %14, 0
%24 = select i1 %23, i8 69, i8 79
%25 = select i1 %22, i8 78, i8 %24
%26 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %15, i8 signext %25, i8 signext %21, i8 signext %17)
ret void
}