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

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ CSTOPB;
int /*<<< orphan*/ IGNPAR;
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*/ perr(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int setup_serial_device(int dev_fd) {
  struct termios t;
  int ret;
  ret = tcgetattr(dev_fd, &t);
  if (ret)
    return ret;
  cfmakeraw(&t);
  cfsetispeed(&t, B115200);
  cfsetospeed(&t, B115200);
  tcflush(dev_fd, TCIFLUSH);
  t.c_iflag |= IGNPAR;
  t.c_cflag &= ~CSTOPB;
  ret = tcsetattr(dev_fd, TCSANOW, &t);
  if (ret) {
    perr("setting terminal attributes");
    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    123e <setup_serial_device+0xce>
lea    -0x10(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1090 <cfsetispeed@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1237 <setup_serial_device+0xc7>
lea    0xdd9(%rip),%rdi
call   1050 <perr@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123e <setup_serial_device+0xce>
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    1209 <setup_serial_device+0x99>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1060 <cfmakeraw@plt>
mov    0x2e32(%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    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
or     %eax,0xc(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x8(%rsp)
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
test   %eax,%eax
je     1207 <setup_serial_device+0x97>
mov    %eax,%ebp
lea    0xe00(%rip),%rdi
call   1050 <perr@plt>
jmp    1209 <setup_serial_device+0x99>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@B115200 = global i32 0
@TCIFLUSH = global i32 0
@IGNPAR = global i32 0
@CSTOPB = global i32 0
@TCSANOW = global i32 0
@.str = [28 x i8] c"setting terminal attributes\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 %39
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* @IGNPAR
%22 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%23 = load i32, i32* %22
%24 = or i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* @CSTOPB
%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* %3
%31 = load i32, i32* @TCSANOW
%32 = call i32 @tcsetattr(i32 %30, i32 %31, %struct.termios* %4)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %12
%36 = call i32 @perr(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %39
38: ; preds = %12
store i32 0, i32* %2
br label %39
39: ; preds = %38, %35, %10
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@B115200 = global i32 0
@TCIFLUSH = global i32 0
@IGNPAR = global i32 0
@CSTOPB = global i32 0
@TCSANOW = global i32 0
@.str = [28 x i8] c"setting terminal attributes\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* @IGNPAR
%15 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = or i32 %16, %14
store i32 %17, i32* %15
%18 = load i32, i32* @CSTOPB
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %0, i32 %23, %struct.termios* %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %6
%27 = call i32 @perr(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %6, %1, %26
%29 = phi i32 [ %24, %26 ], [ %4, %1 ], [ 0, %6 ]
ret i32 %29
}