#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;
  scalar_t__ c_lflag;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CSTOPB;
int IXANY;
int IXOFF;
int IXON;
int PARODD;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int uart_init(int fd) {
  struct termios tty = {0};
  fprintf(stderr, "INIT: setting tty device paramters\n");
  if (tcgetattr(fd, &tty)) {
    perror("get term attr");
    return 1;
  }
  cfsetspeed(&tty, B115200);
  cfmakeraw(&tty);
  tty.c_iflag &= ~(IXON | IXOFF | IXANY);
  tty.c_lflag = 0;
  tty.c_cflag |= (CLOCAL | CREAD);
  tty.c_cflag &= ~(PARODD | CSTOPB | CRTSCTS);
  if (tcsetattr(fd, TCSANOW, &tty)) {
    perror("set term attr");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<uart_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
je     11ce <uart_init+0x5e>
lea    0xe67(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    128f <uart_init+0x11f>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1040 <cfsetspeed@plt>
lea    -0x20(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    0x2def(%rip),%rcx
or     (%rcx),%eax
mov    0x2db6(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    0x2da3(%rip),%rcx
or     (%rcx),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    0x2d53(%rip),%rcx
or     (%rcx),%eax
mov    0x2d52(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
je     1288 <uart_init+0x118>
lea    0xdbb(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    128f <uart_init+0x11f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_init>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1040 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1060 <tcgetattr@plt>
test   %rax,%rax
jne    1244 <uart_init+0xe4>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <cfsetspeed@plt>
mov    %r14,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2e1c(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de2(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2da4(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
movq   $0x0,0x8(%rsp)
or     0x4(%rsp),%ecx
mov    0x2dcd(%rip),%rax
mov    0x2d86(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    0x2d83(%rip),%rax
or     (%rax),%edx
not    %edx
and    %ecx,%edx
mov    %edx,0x4(%rsp)
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    124d <uart_init+0xed>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd9(%rip),%rdi
jmp    1254 <uart_init+0xf4>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    123c <uart_init+0xdc>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i64, i32 }
@stderr = global i32 0
@.str = [36 x i8] c"INIT: setting tty device paramters\0A\00"
@.str.1 = [14 x i8] c"get term attr\00"
@B115200 = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARODD = global i32 0
@CSTOPB = global i32 0
@CRTSCTS = global i32 0
@TCSANOW = global i32 0
@.str.2 = [14 x i8] c"set term attr\00"
define i32 @uart_init(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %3
%5 = bitcast %struct.termios* %4 to i8*
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %3
%9 = call i64 @tcgetattr(i32 %8, %struct.termios* %4)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %49
13: ; preds = %1
%14 = load i32, i32* @B115200
%15 = call i32 @cfsetspeed(%struct.termios* %4, i32 %14)
%16 = call i32 @cfmakeraw(%struct.termios* %4)
%17 = load i32, i32* @IXON
%18 = load i32, i32* @IXOFF
%19 = or i32 %17, %18
%20 = load i32, i32* @IXANY
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%26 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
store i64 0, i64* %26
%27 = load i32, i32* @CLOCAL
%28 = load i32, i32* @CREAD
%29 = or i32 %27, %28
%30 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @PARODD
%34 = load i32, i32* @CSTOPB
%35 = or i32 %33, %34
%36 = load i32, i32* @CRTSCTS
%37 = or i32 %35, %36
%38 = xor i32 %37, -1
%39 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%40 = load i32, i32* %39
%41 = and i32 %40, %38
store i32 %41, i32* %39
%42 = load i32, i32* %3
%43 = load i32, i32* @TCSANOW
%44 = call i64 @tcsetattr(i32 %42, i32 %43, %struct.termios* %4)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %13
%47 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %49
48: ; preds = %13
store i32 0, i32* %2
br label %49
49: ; preds = %48, %46, %11
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i64, i32 }
@stderr = global i32 0
@.str = [36 x i8] c"INIT: setting tty device paramters\0A\00"
@.str.1 = [14 x i8] c"get term attr\00"
@B115200 = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARODD = global i32 0
@CSTOPB = global i32 0
@CRTSCTS = global i32 0
@TCSANOW = global i32 0
@.str.2 = [14 x i8] c"set term attr\00"
define i32 @uart_init(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%6 = call i64 @tcgetattr(i32 %0, %struct.termios* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %38
8: ; preds = %1
%9 = load i32, i32* @B115200
%10 = call i32 @cfsetspeed(%struct.termios* %2, i32 %9)
%11 = call i32 @cfmakeraw(%struct.termios* %2)
%12 = load i32, i32* @IXON
%13 = load i32, i32* @IXOFF
%14 = or i32 %13, %12
%15 = load i32, i32* @IXANY
%16 = or i32 %14, %15
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
store i64 0, i64* %21
%22 = load i32, i32* @CLOCAL
%23 = load i32, i32* @CREAD
%24 = or i32 %23, %22
%25 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%26 = load i32, i32* %25
%27 = or i32 %24, %26
%28 = load i32, i32* @PARODD
%29 = load i32, i32* @CSTOPB
%30 = or i32 %29, %28
%31 = load i32, i32* @CRTSCTS
%32 = or i32 %30, %31
%33 = xor i32 %32, -1
%34 = and i32 %27, %33
store i32 %34, i32* %25
%35 = load i32, i32* @TCSANOW
%36 = call i64 @tcsetattr(i32 %0, i32 %35, %struct.termios* %2)
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %8, %1
%39 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %8 ]
%40 = call i32 @perror(i8* %39)
br label %41
41: ; preds = %38, %8
%42 = phi i32 [ 0, %8 ], [ 1, %38 ]
ret i32 %42
}