#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_lflag;
  int c_cflag;
  int /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int /*<<< orphan*/ B115200;
int BRKINT;
int CS8;
int CSIZE;
int ECHO;
int ECHONL;
int ICANON;
int ICRNL;
int IEXTEN;
int IGNBRK;
int IGNCR;
int INLCR;
int ISIG;
int ISTRIP;
int IXON;
int /*<<< orphan*/ OPOST;
int PARENB;
int PARMRK;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int setup_serial(int fd) {
  int ret = 0;
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  tcgetattr(fd, &tty);
  cfsetospeed(&tty, B115200);
  cfsetispeed(&tty, B115200);
  tty.c_iflag &=
      ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  tty.c_oflag &= ~OPOST;
  tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  tty.c_cflag &= ~(CSIZE | PARENB);
  tty.c_cflag |= CS8;
  ret = tcsetattr(fd, TCSANOW, &tty);
  tcflush(fd, TCIOFLUSH);
  return ret;
}


x86 assembly (clang, -O0):
<setup_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    0x2dd6(%rip),%rcx
or     (%rcx),%eax
mov    0x2e25(%rip),%rcx
or     (%rcx),%eax
mov    0x2d94(%rip),%rcx
or     (%rcx),%eax
mov    0x2dfb(%rip),%rcx
or     (%rcx),%eax
mov    0x2dea(%rip),%rcx
or     (%rcx),%eax
mov    0x2df1(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc8(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    0x2d3b(%rip),%rcx
or     (%rcx),%eax
mov    0x2d1a(%rip),%rcx
or     (%rcx),%eax
mov    0x2d69(%rip),%rcx
or     (%rcx),%eax
mov    0x2d40(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
mov    0x2d5d(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_serial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
mov    0x2dc3(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1080 <cfsetispeed@plt>
mov    0x2dae(%rip),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e34(%rip),%rax
or     (%rax),%ecx
mov    0x2da3(%rip),%rax
or     (%rax),%ecx
mov    0x2e0a(%rip),%rax
or     (%rax),%ecx
mov    0x2df9(%rip),%rax
or     (%rax),%ecx
mov    0x2e00(%rip),%rax
or     (%rax),%ecx
mov    0x2dd7(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0xc(%rsp)
mov    0x2da2(%rip),%rax
mov    0x2d53(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d30(%rip),%rax
or     (%rax),%ecx
mov    0x2d7f(%rip),%rax
or     (%rax),%ecx
mov    0x2d56(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2d4f(%rip),%rax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    0x2d3f(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    %eax,%ebp
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32 }
@B115200 = global i32 0
@IGNBRK = global i32 0
@BRKINT = global i32 0
@PARMRK = global i32 0
@ISTRIP = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@CSIZE = global i32 0
@PARENB = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
@TCIOFLUSH = global i32 0
define i32 @setup_serial(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %2
store i32 0, i32* %3
%5 = call i32 @memset(%struct.termios* %4, i32 0, i32 16)
%6 = load i32, i32* %2
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %4)
%8 = load i32, i32* @B115200
%9 = call i32 @cfsetospeed(%struct.termios* %4, i32 %8)
%10 = load i32, i32* @B115200
%11 = call i32 @cfsetispeed(%struct.termios* %4, i32 %10)
%12 = load i32, i32* @IGNBRK
%13 = load i32, i32* @BRKINT
%14 = or i32 %12, %13
%15 = load i32, i32* @PARMRK
%16 = or i32 %14, %15
%17 = load i32, i32* @ISTRIP
%18 = or i32 %16, %17
%19 = load i32, i32* @INLCR
%20 = or i32 %18, %19
%21 = load i32, i32* @IGNCR
%22 = or i32 %20, %21
%23 = load i32, i32* @ICRNL
%24 = or i32 %22, %23
%25 = load i32, i32* @IXON
%26 = or i32 %24, %25
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @OPOST
%32 = xor i32 %31, -1
%33 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 3
%34 = load i32, i32* %33
%35 = and i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* @ECHO
%37 = load i32, i32* @ECHONL
%38 = or i32 %36, %37
%39 = load i32, i32* @ICANON
%40 = or i32 %38, %39
%41 = load i32, i32* @ISIG
%42 = or i32 %40, %41
%43 = load i32, i32* @IEXTEN
%44 = or i32 %42, %43
%45 = xor i32 %44, -1
%46 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%47 = load i32, i32* %46
%48 = and i32 %47, %45
store i32 %48, i32* %46
%49 = load i32, i32* @CSIZE
%50 = load i32, i32* @PARENB
%51 = or i32 %49, %50
%52 = xor i32 %51, -1
%53 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
%54 = load i32, i32* %53
%55 = and i32 %54, %52
store i32 %55, i32* %53
%56 = load i32, i32* @CS8
%57 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
%58 = load i32, i32* %57
%59 = or i32 %58, %56
store i32 %59, i32* %57
%60 = load i32, i32* %2
%61 = load i32, i32* @TCSANOW
%62 = call i32 @tcsetattr(i32 %60, i32 %61, %struct.termios* %4)
store i32 %62, i32* %3
%63 = load i32, i32* %2
%64 = load i32, i32* @TCIOFLUSH
%65 = call i32 @tcflush(i32 %63, i32 %64)
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32 }
@B115200 = global i32 0
@IGNBRK = global i32 0
@BRKINT = global i32 0
@PARMRK = global i32 0
@ISTRIP = global i32 0
@INLCR = global i32 0
@IGNCR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@CSIZE = global i32 0
@PARENB = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
@TCIOFLUSH = global i32 0
define i32 @setup_serial(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @memset(%struct.termios* %2, i32 0, i32 16)
%5 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%6 = load i32, i32* @B115200
%7 = call i32 @cfsetospeed(%struct.termios* %2, i32 %6)
%8 = load i32, i32* @B115200
%9 = call i32 @cfsetispeed(%struct.termios* %2, i32 %8)
%10 = load i32, i32* @IGNBRK
%11 = load i32, i32* @BRKINT
%12 = or i32 %11, %10
%13 = load i32, i32* @PARMRK
%14 = or i32 %12, %13
%15 = load i32, i32* @ISTRIP
%16 = or i32 %14, %15
%17 = load i32, i32* @INLCR
%18 = or i32 %16, %17
%19 = load i32, i32* @IGNCR
%20 = or i32 %18, %19
%21 = load i32, i32* @ICRNL
%22 = or i32 %20, %21
%23 = load i32, i32* @IXON
%24 = or i32 %22, %23
%25 = xor i32 %24, -1
%26 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%27 = load i32, i32* %26
%28 = and i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* @OPOST
%30 = xor i32 %29, -1
%31 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%32 = load i32, i32* %31
%33 = and i32 %32, %30
store i32 %33, i32* %31
%34 = load i32, i32* @ECHO
%35 = load i32, i32* @ECHONL
%36 = or i32 %35, %34
%37 = load i32, i32* @ICANON
%38 = or i32 %36, %37
%39 = load i32, i32* @ISIG
%40 = or i32 %38, %39
%41 = load i32, i32* @IEXTEN
%42 = or i32 %40, %41
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* @CSIZE
%48 = load i32, i32* @PARENB
%49 = or i32 %48, %47
%50 = xor i32 %49, -1
%51 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%52 = load i32, i32* %51
%53 = and i32 %52, %50
%54 = load i32, i32* @CS8
%55 = or i32 %54, %53
store i32 %55, i32* %51
%56 = load i32, i32* @TCSANOW
%57 = call i32 @tcsetattr(i32 %0, i32 %56, %struct.termios* %2)
%58 = load i32, i32* @TCIOFLUSH
%59 = call i32 @tcflush(i32 %0, i32 %58)
ret i32 %57
}