#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;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
};

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int /*<<< orphan*/ FNDELAY;
int /*<<< orphan*/ F_SETFL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_port(char *port_name) {
  int fd;
  struct termios options;
  fd = open(port_name, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    char errbuf[256];
    sprintf(errbuf, "open port (%s): ", port_name);
    perror(errbuf);
    return -1;
  }
  fcntl(fd, F_SETFL, FNDELAY);
  tcgetattr(fd, &options);
  options.c_iflag = options.c_oflag = options.c_lflag = 0;
  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 |= CS8;
  tcsetattr(fd, TCSANOW, &options);
  return (fd);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ef <open_port+0x6f>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe2e(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x140(%rbp),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <open_port+0x149>
mov    -0x14(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1050 <tcgetattr@plt>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1040 <cfsetospeed@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
mov    0x2d6f(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    0x2e55(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2df8(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1270 <open_port+0xf0>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2d98(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1080 <cfsetispeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1040 <cfsetospeed@plt>
mov    0x2d63(%rip),%rax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     (%rsp),%ecx
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    0x2d4d(%rip),%rdx
or     (%rdx),%eax
mov    0x2d84(%rip),%rdx
or     (%rdx),%eax
not    %eax
and    %ecx,%eax
mov    0x2d7f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,(%rsp)
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd89(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rdx
call   10a0 <sprintf@plt>
mov    %r15,%rdi
call   1090 <perror@plt>
jmp    1261 <open_port+0xe1>

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [17 x i8] c"open port (%s): \00"
@F_SETFL = global i32 0
@FNDELAY = 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
@CS8 = global i32 0
@TCSANOW = global i32 0
define i32 @open_port(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca [256 x i8]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = load i32, i32* @O_NDELAY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %7, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %1
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%18 = load i8*, i8** %3
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%21 = call i32 @perror(i8* %20)
store i32 -1, i32* %2
br label %65
22: ; preds = %1
%23 = load i32, i32* %4
%24 = load i32, i32* @F_SETFL
%25 = load i32, i32* @FNDELAY
%26 = call i32 @fcntl(i32 %23, i32 %24, i32 %25)
%27 = load i32, i32* %4
%28 = call i32 @tcgetattr(i32 %27, %struct.termios* %5)
%29 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 3
store i64 0, i64* %31
%32 = load i32, i32* @B115200
%33 = call i32 @cfsetispeed(%struct.termios* %5, i32 %32)
%34 = load i32, i32* @B115200
%35 = call i32 @cfsetospeed(%struct.termios* %5, i32 %34)
%36 = load i32, i32* @CLOCAL
%37 = load i32, i32* @CREAD
%38 = or i32 %36, %37
%39 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = or i32 %40, %38
store i32 %41, i32* %39
%42 = load i32, i32* @PARENB
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = load i32, i32* @CSTOPB
%48 = xor i32 %47, -1
%49 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%50 = load i32, i32* %49
%51 = and i32 %50, %48
store i32 %51, i32* %49
%52 = load i32, i32* @CSIZE
%53 = xor i32 %52, -1
%54 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%55 = load i32, i32* %54
%56 = and i32 %55, %53
store i32 %56, i32* %54
%57 = load i32, i32* @CS8
%58 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%59 = load i32, i32* %58
%60 = or i32 %59, %57
store i32 %60, i32* %58
%61 = load i32, i32* %4
%62 = load i32, i32* @TCSANOW
%63 = call i32 @tcsetattr(i32 %61, i32 %62, %struct.termios* %5)
%64 = load i32, i32* %4
store i32 %64, i32* %2
br label %65
65: ; preds = %22, %16
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [17 x i8] c"open port (%s): \00"
@F_SETFL = global i32 0
@FNDELAY = 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
@CS8 = global i32 0
@TCSANOW = global i32 0
define i32 @open_port(i8* %0) {
%2 = alloca %struct.termios
%3 = alloca [256 x i8]
%4 = bitcast %struct.termios* %2 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_NDELAY
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%14 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%15 = call i32 @perror(i8* %13)
br label %44
16: ; preds = %1
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* @FNDELAY
%19 = tail call i32 @fcntl(i32 %10, i32 %17, i32 %18)
%20 = call i32 @tcgetattr(i32 %10, %struct.termios* %2)
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%22 = bitcast i64* %21 to i8*
%23 = load i32, i32* @B115200
%24 = call i32 @cfsetispeed(%struct.termios* %2, i32 %23)
%25 = load i32, i32* @B115200
%26 = call i32 @cfsetospeed(%struct.termios* %2, i32 %25)
%27 = load i32, i32* @CLOCAL
%28 = load i32, i32* @CREAD
%29 = or i32 %28, %27
%30 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%31 = load i32, i32* %30
%32 = or i32 %29, %31
%33 = load i32, i32* @PARENB
%34 = load i32, i32* @CSTOPB
%35 = or i32 %33, %34
%36 = load i32, i32* @CSIZE
%37 = or i32 %35, %36
%38 = xor i32 %37, -1
%39 = and i32 %32, %38
%40 = load i32, i32* @CS8
%41 = or i32 %40, %39
store i32 %41, i32* %30
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %10, i32 %42, %struct.termios* %2)
br label %44
44: ; preds = %16, %12
ret i32 %10
}