#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 */
typedef int /*<<< orphan*/ tty;
struct termios {
  int *c_cc;
  int /*<<< orphan*/ c_cflag;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCAL;
int /*<<< orphan*/ CRTSCTS;
int /*<<< orphan*/ HUPCL;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfmakeraw(struct termios *);
scalar_t__ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
int tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int configure_tty(char *tty_path, speed_t speed) {
  int serial_fd;
  struct termios tty;
  memset(&tty, 0, sizeof(tty));
  serial_fd = open(tty_path, O_RDWR);
  if (serial_fd == -1) {
    printf("Could not open %s\n", tty_path);
    return -1;
  }
  if (tcflush(serial_fd, TCIOFLUSH) == -1) {
    printf("Error in tcflush: %s\n", strerror(errno));
    return -2;
  }
  if (tcgetattr(serial_fd, &tty)) {
    printf("Error in tcgetattr: %s\n", strerror(errno));
    return -3;
  }
  cfmakeraw(&tty);
  tty.c_cc[VMIN] = 1;
  tty.c_cc[VTIME] = 0;
  tty.c_cflag &= ~CRTSCTS;
  tty.c_cflag &= ~HUPCL;
  tty.c_cflag |= CLOCAL;
  if (cfsetspeed(&tty, speed)) {
    printf("Error while setting terminal speed: %s\n", strerror(errno));
    return -4;
  }
  if (tcsetattr(serial_fd, TCSAFLUSH, &tty) == -1) {
    printf("Error could not set attribute to tty: %s\n", strerror(errno));
    return -5;
  }
  return serial_fd;
}


x86 assembly (clang, -O0):
<configure_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ea <configure_tty+0x5a>
mov    -0x10(%rbp),%rsi
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135e <configure_tty+0x1ce>
mov    -0x18(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
cmp    $0xffffffff,%eax
jne    122d <configure_tty+0x9d>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf7(%rip),%rdi
call   1040 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    135e <configure_tty+0x1ce>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
je     126c <configure_tty+0xdc>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    135e <configure_tty+0x1ce>
lea    -0x28(%rbp),%rdi
call   1070 <cfmakeraw@plt>
mov    -0x28(%rbp),%rax
mov    0x2d58(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2d23(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2cfa(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cf8(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cce(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   1060 <cfsetspeed@plt>
cmp    $0x0,%rax
je     1311 <configure_tty+0x181>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd41(%rip),%rdi
call   1040 <printf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    135e <configure_tty+0x1ce>
mov    -0x18(%rbp),%edi
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1358 <configure_tty+0x1c8>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd22(%rip),%rdi
call   1040 <printf@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    135e <configure_tty+0x1ce>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<configure_tty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1210 <configure_tty+0x80>
mov    %eax,%ebx
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
cmp    $0xffffffff,%eax
je     1229 <configure_tty+0x99>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1080 <tcgetattr@plt>
test   %rax,%rax
je     1250 <configure_tty+0xc0>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe2b(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xfffffffd,%ebx
jmp    1314 <configure_tty+0x184>
lea    0xde9(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1314 <configure_tty+0x184>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdd5(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xfffffffe,%ebx
jmp    1314 <configure_tty+0x184>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1070 <cfmakeraw@plt>
mov    (%rsp),%rax
mov    0x2d72(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d41(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    0x2d1f(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2cf8(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
mov    %r14d,%esi
call   1060 <cfsetspeed@plt>
test   %rax,%rax
je     12da <configure_tty+0x14a>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd76(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xfffffffc,%ebx
jmp    1314 <configure_tty+0x184>
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1090 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1314 <configure_tty+0x184>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd62(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xfffffffb,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@O_RDWR = global i32 0
@.str = [19 x i8] c"Could not open %s\0A\00"
@TCIOFLUSH = global i32 0
@.str.1 = [22 x i8] c"Error in tcflush: %s\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"Error in tcgetattr: %s\0A\00"
@VMIN = global i64 0
@VTIME = global i64 0
@CRTSCTS = global i32 0
@HUPCL = global i32 0
@CLOCAL = global i32 0
@.str.3 = [40 x i8] c"Error while setting terminal speed: %s\0A\00"
@TCSAFLUSH = global i32 0
@.str.4 = [42 x i8] c"Error could not set attribute to tty: %s\0A\00"
define i32 @configure_tty(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i32 @memset(%struct.termios* %7, i32 0, i32 16)
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %76
17: ; preds = %2
%18 = load i32, i32* %6
%19 = load i32, i32* @TCIOFLUSH
%20 = call i32 @tcflush(i32 %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 -2, i32* %3
br label %76
26: ; preds = %17
%27 = load i32, i32* %6
%28 = call i64 @tcgetattr(i32 %27, %struct.termios* %7)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 -3, i32* %3
br label %76
34: ; preds = %26
%35 = call i32 @cfmakeraw(%struct.termios* %7)
%36 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%37 = load i32*, i32** %36
%38 = load i64, i64* @VMIN
%39 = getelementptr inbounds i32, i32* %37, i64 %38
store i32 1, i32* %39
%40 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%41 = load i32*, i32** %40
%42 = load i64, i64* @VTIME
%43 = getelementptr inbounds i32, i32* %41, i64 %42
store i32 0, i32* %43
%44 = load i32, i32* @CRTSCTS
%45 = xor i32 %44, -1
%46 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%47 = load i32, i32* %46
%48 = and i32 %47, %45
store i32 %48, i32* %46
%49 = load i32, i32* @HUPCL
%50 = xor i32 %49, -1
%51 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%52 = load i32, i32* %51
%53 = and i32 %52, %50
store i32 %53, i32* %51
%54 = load i32, i32* @CLOCAL
%55 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%56 = load i32, i32* %55
%57 = or i32 %56, %54
store i32 %57, i32* %55
%58 = load i32, i32* %5
%59 = call i64 @cfsetspeed(%struct.termios* %7, i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %34
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %63)
store i32 -4, i32* %3
br label %76
65: ; preds = %34
%66 = load i32, i32* %6
%67 = load i32, i32* @TCSAFLUSH
%68 = call i32 @tcsetattr(i32 %66, i32 %67, %struct.termios* %7)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %74
70: ; preds = %65
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %72)
store i32 -5, i32* %3
br label %76
74: ; preds = %65
%75 = load i32, i32* %6
store i32 %75, i32* %3
br label %76
76: ; preds = %74, %70, %61, %30, %22, %14
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@O_RDWR = global i32 0
@.str = [19 x i8] c"Could not open %s\0A\00"
@TCIOFLUSH = global i32 0
@.str.1 = [22 x i8] c"Error in tcflush: %s\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"Error in tcgetattr: %s\0A\00"
@VMIN = global i64 0
@VTIME = global i64 0
@CRTSCTS = global i32 0
@HUPCL = global i32 0
@CLOCAL = global i32 0
@.str.3 = [40 x i8] c"Error while setting terminal speed: %s\0A\00"
@TCSAFLUSH = global i32 0
@.str.4 = [42 x i8] c"Error could not set attribute to tty: %s\0A\00"
define i32 @configure_tty(i8* %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @memset(%struct.termios* %3, i32 0, i32 16)
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %57
11: ; preds = %2
%12 = load i32, i32* @TCIOFLUSH
%13 = call i32 @tcflush(i32 %7, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %57
19: ; preds = %11
%20 = call i64 @tcgetattr(i32 %7, %struct.termios* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %57
26: ; preds = %19
%27 = call i32 @cfmakeraw(%struct.termios* %3)
%28 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%29 = load i32*, i32** %28
%30 = load i64, i64* @VMIN
%31 = getelementptr inbounds i32, i32* %29, i64 %30
store i32 1, i32* %31
%32 = load i64, i64* @VTIME
%33 = getelementptr inbounds i32, i32* %29, i64 %32
store i32 0, i32* %33
%34 = load i32, i32* @CRTSCTS
%35 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%36 = load i32, i32* %35
%37 = load i32, i32* @HUPCL
%38 = or i32 %34, %37
%39 = xor i32 %38, -1
%40 = and i32 %36, %39
%41 = load i32, i32* @CLOCAL
%42 = or i32 %41, %40
store i32 %42, i32* %35
%43 = call i64 @cfsetspeed(%struct.termios* %3, i32 %1)
%44 = icmp eq i64 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %26
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %47)
br label %57
49: ; preds = %26
%50 = load i32, i32* @TCSAFLUSH
%51 = call i32 @tcsetattr(i32 %7, i32 %50, %struct.termios* %3)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %49, %53, %45, %22, %15, %9
%58 = phi i32 [ -1, %9 ], [ -2, %15 ], [ -3, %22 ], [ -4, %45 ], [ -5, %53 ], [ %7, %49 ]
ret i32 %58
}