#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
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) {
    fprintf(stderr, "tcgetattr failed, %s\n", strerror(errno));
    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, %s\n",
            strerror(errno));
    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     11db <setup_serial_device+0x5b>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe35(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12be <setup_serial_device+0x13e>
lea    -0x10(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1090 <cfsetispeed@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12b7 <setup_serial_device+0x137>
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xd6f(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12be <setup_serial_device+0x13e>
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>
test   %eax,%eax
je     11a6 <setup_serial_device+0x26>
mov    %eax,%ebp
lea    0xe5f(%rip),%r14
jmp    122e <setup_serial_device+0xae>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1060 <cfmakeraw@plt>
mov    0x2dee(%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    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    0x2da8(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd7(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2dd0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
test   %eax,%eax
je     125d <setup_serial_device+0xdd>
mov    %eax,%ebp
lea    0xde8(%rip),%r14
mov    0x2d83(%rip),%rax
mov    (%rax),%ebx
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebp,%ebp
jmp    1252 <setup_serial_device+0xd2>

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"tcgetattr failed, %s\0A\00"
@errno = global i32 0
@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
@.str.1 = [40 x i8] c"ERROR: setting terminal attributes, %s\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 %28
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 %35, label %28
28: ; preds = %6, %1
%29 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), %6 ]
%30 = phi i32 [ %4, %1 ], [ %26, %6 ]
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* %29, i8* %33)
br label %35
35: ; preds = %28, %6
%36 = phi i32 [ 0, %6 ], [ %30, %28 ]
ret i32 %36
}