#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_lflag;
  int *c_cc;
  int /*<<< orphan*/ c_cflag;
  int /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int BRKINT;
int /*<<< orphan*/ CS8;
int ECHO;
int /*<<< orphan*/ HUPCL;
int ICANON;
int ICRNL;
int IEXTEN;
int INPCK;
int ISIG;
int ISTRIP;
int IXON;
int /*<<< orphan*/ OPOST;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int unix_raw_mode(int fd, char **errmsg) {
  struct termios tios;
  if (tcgetattr(fd, &tios) == -1) {
    *errmsg = "could not set raw mode on serial device";
    return (-1);
  }
  tios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  tios.c_oflag &= ~(OPOST);
  tios.c_cflag |= (CS8);
  tios.c_cflag &= ~(HUPCL);
  tios.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  tios.c_cc[VMIN] = 0;
  tios.c_cc[VTIME] = 1;
  if (tcsetattr(fd, TCSAFLUSH, &tios) == -1) {
    *errmsg = "could not get attributes from serial device";
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<unix_raw_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    115e <unix_raw_mode+0x3e>
mov    -0x10(%rbp),%rax
lea    0xeb1(%rip),%rcx
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <unix_raw_mode+0x13d>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    0x2e7a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e19(%rip),%rcx
or     (%rcx),%eax
mov    0x2e08(%rip),%rcx
or     (%rcx),%eax
mov    0x2e47(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    0x2d99(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc0(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd7(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2da9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2d6c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    1256 <unix_raw_mode+0x136>
mov    -0x10(%rbp),%rax
lea    0xde1(%rip),%rcx
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <unix_raw_mode+0x13d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_raw_mode>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     1211 <unix_raw_mode+0xf1>
mov    0x2e75(%rip),%rax
mov    0x2e9e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e3b(%rip),%rax
or     (%rax),%ecx
mov    0x2e2a(%rip),%rax
or     (%rax),%ecx
mov    0x2e69(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x14(%rsp)
mov    0x2e2c(%rip),%rax
mov    0x10(%rsp),%ecx
or     (%rax),%ecx
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2e2a(%rip),%rcx
mov    0x2dcb(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,0x10(%rsp)
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    0x2e03(%rip),%rax
or     (%rax),%edx
not    %edx
and    %edx,0x4(%rsp)
mov    0x8(%rsp),%rax
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d9e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1220 <unix_raw_mode+0x100>
lea    0xe19(%rip),%rax
jmp    1218 <unix_raw_mode+0xf8>
lea    0xde8(%rip),%rax
mov    %rax,(%rbx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i32, i32 }
@.str = [40 x i8] c"could not set raw mode on serial device\00"
@BRKINT = global i32 0
@ICRNL = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@CS8 = global i32 0
@HUPCL = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@IEXTEN = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
@.str.1 = [44 x i8] c"could not get attributes from serial device\00"
define i32 @unix_raw_mode(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.termios
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i8**, i8*** %5
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8** %11
store i32 -1, i32* %3
br label %66
12: ; preds = %2
%13 = load i32, i32* @BRKINT
%14 = load i32, i32* @ICRNL
%15 = or i32 %13, %14
%16 = load i32, i32* @INPCK
%17 = or i32 %15, %16
%18 = load i32, i32* @ISTRIP
%19 = or i32 %17, %18
%20 = load i32, i32* @IXON
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%26 = load i32, i32* @OPOST
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @CS8
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%33 = load i32, i32* %32
%34 = or i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* @HUPCL
%36 = xor i32 %35, -1
%37 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%38 = load i32, i32* %37
%39 = and i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* @ECHO
%41 = load i32, i32* @ICANON
%42 = or i32 %40, %41
%43 = load i32, i32* @IEXTEN
%44 = or i32 %42, %43
%45 = load i32, i32* @ISIG
%46 = or i32 %44, %45
%47 = xor i32 %46, -1
%48 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%49 = load i32, i32* %48
%50 = and i32 %49, %47
store i32 %50, i32* %48
%51 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%52 = load i32*, i32** %51
%53 = load i64, i64* @VMIN
%54 = getelementptr inbounds i32, i32* %52, i64 %53
store i32 0, i32* %54
%55 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%56 = load i32*, i32** %55
%57 = load i64, i64* @VTIME
%58 = getelementptr inbounds i32, i32* %56, i64 %57
store i32 1, i32* %58
%59 = load i32, i32* %4
%60 = load i32, i32* @TCSAFLUSH
%61 = call i32 @tcsetattr(i32 %59, i32 %60, %struct.termios* %6)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %12
%64 = load i8**, i8*** %5
store i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8** %64
store i32 -1, i32* %3
br label %66
65: ; preds = %12
store i32 0, i32* %3
br label %66
66: ; preds = %65, %63, %10
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i32, i32 }
@.str = [40 x i8] c"could not set raw mode on serial device\00"
@BRKINT = global i32 0
@ICRNL = global i32 0
@INPCK = global i32 0
@ISTRIP = global i32 0
@IXON = global i32 0
@OPOST = global i32 0
@CS8 = global i32 0
@HUPCL = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@IEXTEN = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSAFLUSH = global i32 0
@.str.1 = [44 x i8] c"could not get attributes from serial device\00"
define i32 @unix_raw_mode(i32 %0, i8** nocapture writeonly %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %53, label %7
7: ; preds = %2
%8 = load i32, i32* @BRKINT
%9 = load i32, i32* @ICRNL
%10 = or i32 %9, %8
%11 = load i32, i32* @INPCK
%12 = or i32 %10, %11
%13 = load i32, i32* @ISTRIP
%14 = or i32 %12, %13
%15 = load i32, i32* @IXON
%16 = or i32 %14, %15
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* @OPOST
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 4
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%26 = load i32, i32* @CS8
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%28 = load i32, i32* %27
%29 = or i32 %28, %26
%30 = load i32, i32* @HUPCL
%31 = xor i32 %30, -1
%32 = and i32 %29, %31
store i32 %32, i32* %27
%33 = load i32, i32* @ECHO
%34 = load i32, i32* @ICANON
%35 = or i32 %34, %33
%36 = load i32, i32* @IEXTEN
%37 = or i32 %35, %36
%38 = load i32, i32* @ISIG
%39 = or i32 %37, %38
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%42 = load i32, i32* %41
%43 = and i32 %42, %40
store i32 %43, i32* %41
%44 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%45 = load i32*, i32** %44
%46 = load i64, i64* @VMIN
%47 = getelementptr inbounds i32, i32* %45, i64 %46
store i32 0, i32* %47
%48 = load i64, i64* @VTIME
%49 = getelementptr inbounds i32, i32* %45, i64 %48
store i32 1, i32* %49
%50 = load i32, i32* @TCSAFLUSH
%51 = call i32 @tcsetattr(i32 %0, i32 %50, %struct.termios* %3)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %7, %2
%54 = phi i8* [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), %7 ]
store i8* %54, i8** %1
br label %55
55: ; preds = %53, %7
%56 = phi i32 [ 0, %7 ], [ -1, %53 ]
ret i32 %56
}