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

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ CS8;
int /*<<< orphan*/ CSIZE;
int /*<<< orphan*/ CSTOPB;
int /*<<< orphan*/ PARENB;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int setup_serial_device(int fd) {
  struct termios t;
  int ret;
  ret = tcgetattr(fd, &t);
  if (ret) {
    return ret;
  }
  cfmakeraw(&t);
  cfsetispeed(&t, B115200);
  cfsetospeed(&t, B115200);
  tcflush(fd, TCIFLUSH);
  t.c_cflag &= ~PARENB;
  t.c_cflag &= ~CSTOPB;
  t.c_cflag &= ~CSIZE;
  t.c_cflag |= CS8;
  ret = tcsetattr(fd, TCSANOW, &t);
  if (ret) {
    fprintf(stderr, "ERROR: setting terminal attributes\n");
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_serial_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     119f <setup_serial_device+0x2f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126b <setup_serial_device+0xfb>
lea    -0x10(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1090 <cfsetispeed@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1264 <setup_serial_device+0xf4>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126b <setup_serial_device+0xfb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_serial_device>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1070 <tcgetattr@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1228 <setup_serial_device+0xb8>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1060 <cfmakeraw@plt>
mov    0x2e0e(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1090 <cfsetispeed@plt>
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    0x2dc8(%rip),%rcx
or     (%rcx),%eax
mov    0x2def(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2de8(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
test   %eax,%eax
je     1226 <setup_serial_device+0xb6>
mov    %eax,%ebp
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1228 <setup_serial_device+0xb8>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@B115200 = global i32 0
@TCIFLUSH = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"ERROR: setting terminal attributes\0A\00"
define i32 @setup_serial_device(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %4)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* %5
store i32 %11, i32* %2
br label %50
12: ; preds = %1
%13 = call i32 @cfmakeraw(%struct.termios* %4)
%14 = load i32, i32* @B115200
%15 = call i32 @cfsetispeed(%struct.termios* %4, i32 %14)
%16 = load i32, i32* @B115200
%17 = call i32 @cfsetospeed(%struct.termios* %4, i32 %16)
%18 = load i32, i32* %3
%19 = load i32, i32* @TCIFLUSH
%20 = call i32 @tcflush(i32 %18, i32 %19)
%21 = load i32, i32* @PARENB
%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 = load i32, i32* @CSTOPB
%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* @CSIZE
%32 = xor i32 %31, -1
%33 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%34 = load i32, i32* %33
%35 = and i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* @CS8
%37 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%38 = load i32, i32* %37
%39 = or i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* %3
%41 = load i32, i32* @TCSANOW
%42 = call i32 @tcsetattr(i32 %40, i32 %41, %struct.termios* %4)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %12
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%48 = load i32, i32* %5
store i32 %48, i32* %2
br label %50
49: ; preds = %12
store i32 0, i32* %2
br label %50
50: ; preds = %49, %45, %10
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@B115200 = global i32 0
@TCIFLUSH = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"ERROR: setting terminal attributes\0A\00"
define i32 @setup_serial_device(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %31
6: ; preds = %1
%7 = call i32 @cfmakeraw(%struct.termios* %2)
%8 = load i32, i32* @B115200
%9 = call i32 @cfsetispeed(%struct.termios* %2, i32 %8)
%10 = load i32, i32* @B115200
%11 = call i32 @cfsetospeed(%struct.termios* %2, i32 %10)
%12 = load i32, i32* @TCIFLUSH
%13 = call i32 @tcflush(i32 %0, i32 %12)
%14 = load i32, i32* @PARENB
%15 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @CSTOPB
%18 = or i32 %14, %17
%19 = load i32, i32* @CSIZE
%20 = or i32 %18, %19
%21 = xor i32 %20, -1
%22 = and i32 %16, %21
%23 = load i32, i32* @CS8
%24 = or i32 %23, %22
store i32 %24, i32* %15
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %0, i32 %25, %struct.termios* %2)
%27 = icmp eq i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %6
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %6, %1, %28
%32 = phi i32 [ %26, %28 ], [ %4, %1 ], [ 0, %6 ]
ret i32 %32
}