#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_iflag;
  int c_cflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
};

/* Variables and functions */
int /*<<< orphan*/ B1200;
int CLOCAL;
int CREAD;
int CS7;
int CS8;
int CSIZE;
int CSTOPB;
int IGNBRK;
int IGNPAR;
int PARENB;
int PARODD;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
scalar_t__ cfsetispeed(struct termios *, int /*<<< orphan*/);
scalar_t__ cfsetospeed(struct termios *, int /*<<< orphan*/);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int setup_serial_port(int fd, int nbits, struct termios *attr) {
  ;
  ;
  attr->c_iflag = IGNBRK | IGNPAR;
  attr->c_cflag = 0;
  attr->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | PARENB);
  attr->c_cflag |= CREAD | CLOCAL;
  if (nbits == 7)
    attr->c_cflag |= CS7 | CSTOPB;
  else
    attr->c_cflag |= CS8;
  attr->c_oflag = 0;
  attr->c_lflag = 0;
  attr->c_cc[VMIN] = 1;
  attr->c_cc[VTIME] = 5;
  if (cfsetospeed(attr, B1200))
    return -1;
  if (cfsetispeed(attr, B1200))
    return -1;
  return tcsetattr(fd, TCSANOW, attr);
}


x86 assembly (clang, -O0):
<setup_serial_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3e(%rip),%rax
or     (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0b(%rip),%rax
or     (%rax),%ecx
mov    0x2e6a(%rip),%rax
or     (%rax),%ecx
mov    0x2e51(%rip),%rax
or     (%rax),%ecx
mov    0x2e58(%rip),%rax
or     (%rax),%ecx
xor    $0xffffffff,%ecx
mov    -0x18(%rbp),%rax
and    0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
mov    0x2e2a(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc1(%rip),%rax
or     (%rax),%ecx
mov    -0x18(%rbp),%rax
or     0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
cmpl   $0x7,-0xc(%rbp)
jne    11e6 <setup_serial_port+0xb6>
mov    0x2dac(%rip),%rax
mov    (%rax),%ecx
mov    0x2dab(%rip),%rax
or     (%rax),%ecx
mov    -0x18(%rbp),%rax
or     0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
jmp    11f9 <setup_serial_port+0xc9>
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
or     0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2da0(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d5f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x18(%rbp),%rdi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
call   1030 <cfsetospeed@plt>
cmp    $0x0,%rax
je     126b <setup_serial_port+0x13b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <setup_serial_port+0x17b>
mov    -0x18(%rbp),%rdi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
call   1050 <cfsetispeed@plt>
cmp    $0x0,%rax
je     1293 <setup_serial_port+0x163>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <setup_serial_port+0x17b>
mov    -0x8(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_serial_port>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e7d(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %rdx,%rbx
mov    %edi,%r14d
mov    %ecx,(%rdx)
mov    0x2e8b(%rip),%rcx
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
cmp    $0x7,%esi
je     1170 <setup_serial_port+0x40>
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%ecx
jmp    1182 <setup_serial_port+0x52>
mov    0x2e19(%rip),%rdx
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rdx),%ecx
or     %eax,%ecx
mov    %ecx,0x4(%rbx)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rbx)
mov    0x8(%rbx),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e06(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2e05(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1030 <cfsetospeed@plt>
test   %rax,%rax
jne    11db <setup_serial_port+0xab>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1050 <cfsetispeed@plt>
test   %rax,%rax
je     11e5 <setup_serial_port+0xb5>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <tcsetattr@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@CSIZE = global i32 0
@CSTOPB = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@CS7 = global i32 0
@CS8 = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B1200 = global i32 0
@TCSANOW = global i32 0
define i32 @setup_serial_port(i32 %0, i32 %1, %struct.termios* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios*
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.termios* %2, %struct.termios** %7
%8 = load i32, i32* @IGNBRK
%9 = load i32, i32* @IGNPAR
%10 = or i32 %8, %9
%11 = load %struct.termios*, %struct.termios** %7
%12 = getelementptr inbounds %struct.termios, %struct.termios* %11, i32 0, i32 0
store i32 %10, i32* %12
%13 = load %struct.termios*, %struct.termios** %7
%14 = getelementptr inbounds %struct.termios, %struct.termios* %13, i32 0, i32 1
store i32 0, i32* %14
%15 = load i32, i32* @CSIZE
%16 = load i32, i32* @CSTOPB
%17 = or i32 %15, %16
%18 = load i32, i32* @PARENB
%19 = or i32 %17, %18
%20 = load i32, i32* @PARODD
%21 = or i32 %19, %20
%22 = load i32, i32* @PARENB
%23 = or i32 %21, %22
%24 = xor i32 %23, -1
%25 = load %struct.termios*, %struct.termios** %7
%26 = getelementptr inbounds %struct.termios, %struct.termios* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = and i32 %27, %24
store i32 %28, i32* %26
%29 = load i32, i32* @CREAD
%30 = load i32, i32* @CLOCAL
%31 = or i32 %29, %30
%32 = load %struct.termios*, %struct.termios** %7
%33 = getelementptr inbounds %struct.termios, %struct.termios* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = or i32 %34, %31
store i32 %35, i32* %33
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 7
br i1 %37, label %38, label %46
38: ; preds = %3
%39 = load i32, i32* @CS7
%40 = load i32, i32* @CSTOPB
%41 = or i32 %39, %40
%42 = load %struct.termios*, %struct.termios** %7
%43 = getelementptr inbounds %struct.termios, %struct.termios* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = or i32 %44, %41
store i32 %45, i32* %43
br label %52
46: ; preds = %3
%47 = load i32, i32* @CS8
%48 = load %struct.termios*, %struct.termios** %7
%49 = getelementptr inbounds %struct.termios, %struct.termios* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = or i32 %50, %47
store i32 %51, i32* %49
br label %52
52: ; preds = %46, %38
%53 = load %struct.termios*, %struct.termios** %7
%54 = getelementptr inbounds %struct.termios, %struct.termios* %53, i32 0, i32 4
store i64 0, i64* %54
%55 = load %struct.termios*, %struct.termios** %7
%56 = getelementptr inbounds %struct.termios, %struct.termios* %55, i32 0, i32 3
store i64 0, i64* %56
%57 = load %struct.termios*, %struct.termios** %7
%58 = getelementptr inbounds %struct.termios, %struct.termios* %57, i32 0, i32 2
%59 = load i32*, i32** %58
%60 = load i64, i64* @VMIN
%61 = getelementptr inbounds i32, i32* %59, i64 %60
store i32 1, i32* %61
%62 = load %struct.termios*, %struct.termios** %7
%63 = getelementptr inbounds %struct.termios, %struct.termios* %62, i32 0, i32 2
%64 = load i32*, i32** %63
%65 = load i64, i64* @VTIME
%66 = getelementptr inbounds i32, i32* %64, i64 %65
store i32 5, i32* %66
%67 = load %struct.termios*, %struct.termios** %7
%68 = load i32, i32* @B1200
%69 = call i64 @cfsetospeed(%struct.termios* %67, i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %52
store i32 -1, i32* %4
br label %83
72: ; preds = %52
%73 = load %struct.termios*, %struct.termios** %7
%74 = load i32, i32* @B1200
%75 = call i64 @cfsetispeed(%struct.termios* %73, i32 %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %72
store i32 -1, i32* %4
br label %83
78: ; preds = %72
%79 = load i32, i32* %5
%80 = load i32, i32* @TCSANOW
%81 = load %struct.termios*, %struct.termios** %7
%82 = call i32 @tcsetattr(i32 %79, i32 %80, %struct.termios* %81)
store i32 %82, i32* %4
br label %83
83: ; preds = %78, %77, %71
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@CSIZE = global i32 0
@CSTOPB = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@CS7 = global i32 0
@CS8 = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B1200 = global i32 0
@TCSANOW = global i32 0
define i32 @setup_serial_port(i32 %0, i32 %1, %struct.termios* %2) {
%4 = load i32, i32* @IGNBRK
%5 = load i32, i32* @IGNPAR
%6 = or i32 %5, %4
%7 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%9 = load i32, i32* @CREAD
%10 = load i32, i32* @CLOCAL
%11 = or i32 %10, %9
%12 = icmp eq i32 %1, 7
%13 = load i32, i32* @CS8
%14 = load i32, i32* @CS7
%15 = load i32, i32* @CSTOPB
%16 = or i32 %15, %14
%17 = select i1 %12, i32 %16, i32 %13
%18 = or i32 %17, %11
store i32 %18, i32* %8
%19 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%21 = bitcast i64* %19 to i8*
%22 = load i32*, i32** %20
%23 = load i64, i64* @VMIN
%24 = getelementptr inbounds i32, i32* %22, i64 %23
store i32 1, i32* %24
%25 = load i64, i64* @VTIME
%26 = getelementptr inbounds i32, i32* %22, i64 %25
store i32 5, i32* %26
%27 = load i32, i32* @B1200
%28 = tail call i64 @cfsetospeed(%struct.termios* %2, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %3
%31 = load i32, i32* @B1200
%32 = tail call i64 @cfsetispeed(%struct.termios* %2, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @TCSANOW
%36 = tail call i32 @tcsetattr(i32 %0, i32 %35, %struct.termios* %2)
br label %37
37: ; preds = %30, %3, %34
%38 = phi i32 [ %36, %34 ], [ -1, %3 ], [ -1, %30 ]
ret i32 %38
}