#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_cflag;
  int c_lflag;
  int c_iflag;
  scalar_t__ *c_cc;
  int /*<<< orphan*/ c_oflag;
};

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int ECHO;
int ECHOE;
int /*<<< orphan*/ F_SETFL;
int ICANON;
int ISIG;
int IXANY;
int IXOFF;
int IXON;
int /*<<< orphan*/ OPOST;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ TCSANOW;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_devfd(char *fn) {
  struct termios options;
  int fd;
  fd = open(fn, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    printf("Serial open fail\n");
    exit(2);
  }
  fcntl(fd, F_SETFL, 0);
  tcgetattr(fd, &options);
  cfsetispeed(&options, B115200);
  cfsetospeed(&options, B115200);
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag &= ~CRTSCTS;
  options.c_cflag |= CS8;
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  options.c_iflag &= ~(IXON | IXOFF | IXANY);
  options.c_oflag &= ~OPOST;
  options.c_cc[VTIME] = 0;
  tcsetattr(fd, TCSANOW, &options);
  return fd;
}


x86 assembly (clang, -O0):
<open_devfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2dd8(%rip),%rax
or     (%rax),%esi
mov    0x2dbf(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11d3 <open_devfd+0x53>
lea    0xe3c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1090 <cfsetispeed@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1040 <cfsetospeed@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    0x2d7a(%rip),%rcx
or     (%rcx),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cf1(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
mov    0x2d23(%rip),%rcx
or     (%rcx),%eax
mov    0x2d12(%rip),%rcx
or     (%rcx),%eax
mov    0x2d01(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    0x2d1e(%rip),%rcx
or     (%rcx),%eax
mov    0x2cb5(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2c65(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%edi
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_devfd>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e58(%rip),%rax
mov    0x2de1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12c9 <open_devfd+0x149>
mov    %eax,%ebx
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
mov    0x2d86(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1090 <cfsetispeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1050 <cfsetospeed@plt>
mov    0x2d39(%rip),%rax
mov    0x2da2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     (%rsp),%ecx
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    0x2d23(%rip),%rdx
or     (%rdx),%eax
mov    0x2d72(%rip),%rdx
or     (%rdx),%eax
mov    0x2d19(%rip),%rdx
or     (%rdx),%eax
not    %eax
and    %ecx,%eax
mov    0x2d64(%rip),%rcx
or     (%rcx),%eax
mov    0x2d13(%rip),%rcx
mov    0x2d74(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,(%rsp)
mov    0x2d5e(%rip),%rax
or     (%rax),%edx
mov    0x2d4d(%rip),%rax
or     (%rax),%edx
not    %edx
and    %edx,0x4(%rsp)
mov    0x2d66(%rip),%rax
mov    0x2d6f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d04(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2cb9(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd30(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i64*, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [18 x i8] c"Serial open fail\0A\00"
@F_SETFL = global i32 0
@B115200 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CRTSCTS = global i32 0
@CS8 = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@OPOST = global i32 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @open_devfd(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.termios
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = load i32, i32* @O_NDELAY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %1
%18 = load i32, i32* %4
%19 = load i32, i32* @F_SETFL
%20 = call i32 @fcntl(i32 %18, i32 %19, i32 0)
%21 = load i32, i32* %4
%22 = call i32 @tcgetattr(i32 %21, %struct.termios* %3)
%23 = load i32, i32* @B115200
%24 = call i32 @cfsetispeed(%struct.termios* %3, i32 %23)
%25 = load i32, i32* @B115200
%26 = call i32 @cfsetospeed(%struct.termios* %3, i32 %25)
%27 = load i32, i32* @CLOCAL
%28 = load i32, i32* @CREAD
%29 = or i32 %27, %28
%30 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @PARENB
%34 = xor i32 %33, -1
%35 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%36 = load i32, i32* %35
%37 = and i32 %36, %34
store i32 %37, i32* %35
%38 = load i32, i32* @CSTOPB
%39 = xor i32 %38, -1
%40 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, %39
store i32 %42, i32* %40
%43 = load i32, i32* @CSIZE
%44 = xor i32 %43, -1
%45 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%46 = load i32, i32* %45
%47 = and i32 %46, %44
store i32 %47, i32* %45
%48 = load i32, i32* @CRTSCTS
%49 = xor i32 %48, -1
%50 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%51 = load i32, i32* %50
%52 = and i32 %51, %49
store i32 %52, i32* %50
%53 = load i32, i32* @CS8
%54 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%55 = load i32, i32* %54
%56 = or i32 %55, %53
store i32 %56, i32* %54
%57 = load i32, i32* @ICANON
%58 = load i32, i32* @ECHO
%59 = or i32 %57, %58
%60 = load i32, i32* @ECHOE
%61 = or i32 %59, %60
%62 = load i32, i32* @ISIG
%63 = or i32 %61, %62
%64 = xor i32 %63, -1
%65 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%66 = load i32, i32* %65
%67 = and i32 %66, %64
store i32 %67, i32* %65
%68 = load i32, i32* @IXON
%69 = load i32, i32* @IXOFF
%70 = or i32 %68, %69
%71 = load i32, i32* @IXANY
%72 = or i32 %70, %71
%73 = xor i32 %72, -1
%74 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
%75 = load i32, i32* %74
%76 = and i32 %75, %73
store i32 %76, i32* %74
%77 = load i32, i32* @OPOST
%78 = xor i32 %77, -1
%79 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 4
%80 = load i32, i32* %79
%81 = and i32 %80, %78
store i32 %81, i32* %79
%82 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 3
%83 = load i64*, i64** %82
%84 = load i64, i64* @VTIME
%85 = getelementptr inbounds i64, i64* %83, i64 %84
store i64 0, i64* %85
%86 = load i32, i32* %4
%87 = load i32, i32* @TCSANOW
%88 = call i32 @tcsetattr(i32 %86, i32 %87, %struct.termios* %3)
%89 = load i32, i32* %4
ret i32 %89
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i64*, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@F_SETFL = global i32 0
@B115200 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CRTSCTS = global i32 0
@CS8 = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@OPOST = global i32 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@str = [17 x i8] c"Serial open fail\00"
define i32 @open_devfd(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_NDELAY
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 2)
unreachable
14: ; preds = %1
%15 = load i32, i32* @F_SETFL
%16 = tail call i32 @fcntl(i32 %9, i32 %15, i32 0)
%17 = call i32 @tcgetattr(i32 %9, %struct.termios* %2)
%18 = load i32, i32* @B115200
%19 = call i32 @cfsetispeed(%struct.termios* %2, i32 %18)
%20 = load i32, i32* @B115200
%21 = call i32 @cfsetospeed(%struct.termios* %2, i32 %20)
%22 = load i32, i32* @CLOCAL
%23 = load i32, i32* @CREAD
%24 = or i32 %23, %22
%25 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%26 = load i32, i32* %25
%27 = or i32 %24, %26
%28 = load i32, i32* @PARENB
%29 = load i32, i32* @CSTOPB
%30 = or i32 %28, %29
%31 = load i32, i32* @CSIZE
%32 = or i32 %30, %31
%33 = load i32, i32* @CRTSCTS
%34 = or i32 %32, %33
%35 = xor i32 %34, -1
%36 = and i32 %27, %35
%37 = load i32, i32* @CS8
%38 = or i32 %37, %36
store i32 %38, i32* %25
%39 = load i32, i32* @ICANON
%40 = load i32, i32* @ECHO
%41 = or i32 %40, %39
%42 = load i32, i32* @ECHOE
%43 = or i32 %41, %42
%44 = load i32, i32* @ISIG
%45 = or i32 %43, %44
%46 = xor i32 %45, -1
%47 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%48 = load i32, i32* %47
%49 = and i32 %48, %46
store i32 %49, i32* %47
%50 = load i32, i32* @IXON
%51 = load i32, i32* @IXOFF
%52 = or i32 %51, %50
%53 = load i32, i32* @IXANY
%54 = or i32 %52, %53
%55 = xor i32 %54, -1
%56 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%57 = load i32, i32* %56
%58 = and i32 %57, %55
store i32 %58, i32* %56
%59 = load i32, i32* @OPOST
%60 = xor i32 %59, -1
%61 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 4
%62 = load i32, i32* %61
%63 = and i32 %62, %60
store i32 %63, i32* %61
%64 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%65 = load i64*, i64** %64
%66 = load i64, i64* @VTIME
%67 = getelementptr inbounds i64, i64* %65, i64 %66
store i64 0, i64* %67
%68 = load i32, i32* @TCSANOW
%69 = call i32 @tcsetattr(i32 %9, i32 %68, %struct.termios* %2)
ret i32 %9
}