#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;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
};

/* Variables and functions */
int B19200;
int CLOCAL;
int CREAD;
int CS8;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_init() {
  int sfd;
  struct termios tio;
  sfd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
  if (sfd < 0) {
    perror("Unable to open /dev/ttyS0");
    exit(1);
  }
  tcgetattr(sfd, &tio);
  tio.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  tio.c_iflag = 0;
  tio.c_oflag = 0;
  tio.c_lflag = 0;
  tcflush(sfd, TCIFLUSH);
  tcsetattr(sfd, TCSANOW, &tio);
  return sfd;
}


x86 assembly (clang, -O0):
<serial_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11a9 <serial_init+0x49>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    0x2e03(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd2(%rip),%rcx
or     (%rcx),%eax
mov    0x2df9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_init>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e72(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1200 <serial_init+0xa0>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    0x2e4f(%rip),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2ded(%rip),%rax
or     (%rax),%ecx
mov    0x2e14(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i64 }
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [26 x i8] c"Unable to open /dev/ttyS0\00"
@B19200 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @serial_init() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %3, %4
%6 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %1
%14 = call i32 @tcgetattr(i32 %13, %struct.termios* %2)
%15 = load i32, i32* @B19200
%16 = load i32, i32* @CS8
%17 = or i32 %15, %16
%18 = load i32, i32* @CLOCAL
%19 = or i32 %17, %18
%20 = load i32, i32* @CREAD
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 3
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 2
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
store i64 0, i64* %25
%26 = load i32, i32* %1
%27 = load i32, i32* @TCIFLUSH
%28 = call i32 @tcflush(i32 %26, i32 %27)
%29 = load i32, i32* %1
%30 = load i32, i32* @TCSANOW
%31 = call i32 @tcsetattr(i32 %29, i32 %30, %struct.termios* %2)
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i64 }
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [26 x i8] c"Unable to open /dev/ttyS0\00"
@B19200 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @serial_init() {
%1 = alloca %struct.termios
%2 = bitcast %struct.termios* %1 to i8*
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 @tcgetattr(i32 %6, %struct.termios* %1)
%13 = load i32, i32* @B19200
%14 = load i32, i32* @CS8
%15 = or i32 %14, %13
%16 = load i32, i32* @CLOCAL
%17 = or i32 %15, %16
%18 = load i32, i32* @CREAD
%19 = or i32 %17, %18
%20 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%22 = bitcast i64* %21 to i8*
%23 = load i32, i32* @TCIFLUSH
%24 = call i32 @tcflush(i32 %6, i32 %23)
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %6, i32 %25, %struct.termios* %1)
ret i32 %6
}