#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ B1000000;
int O_NDELAY;
int O_NOCTTY;
int O_RDONLY;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int port_open(const char *device) {
  int handle;
  struct termios options;
  if ((handle = open(device, O_RDONLY | O_NOCTTY | O_NDELAY)) == -1) {
    fprintf(stderr, "error: failed to open serial port (%s)\n", device);
    exit(10);
  }
  tcgetattr(handle, &options);
  cfmakeraw(&options);
  cfsetspeed(&options, B1000000);
  tcsetattr(handle, TCSANOW, &options);
  tcflush(handle, TCIFLUSH);
  return handle;
}


x86 assembly (clang, -O0):
<port_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11df <port_open+0x5f>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xa,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1040 <cfsetspeed@plt>
mov    -0xc(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0xc(%rbp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<port_open>:
push   %r14
push   %rbx
push   %rax
mov    0x2e5d(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2e10(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1201 <port_open+0x81>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <tcgetattr@plt>
mov    %r14,%rdi
call   1060 <cfmakeraw@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <cfsetspeed@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0xa,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"error: failed to open serial port (%s)\0A\00"
@B1000000 = global i32 0
@TCSANOW = global i32 0
@TCIFLUSH = global i32 0
define i32 @port_open(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.termios
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_RDONLY
%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* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 10)
unreachable
18: ; preds = %1
%19 = load i32, i32* %3
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %4)
%21 = call i32 @cfmakeraw(%struct.termios* %4)
%22 = load i32, i32* @B1000000
%23 = call i32 @cfsetspeed(%struct.termios* %4, i32 %22)
%24 = load i32, i32* %3
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %4)
%27 = load i32, i32* %3
%28 = load i32, i32* @TCIFLUSH
%29 = call i32 @tcflush(i32 %27, i32 %28)
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"error: failed to open serial port (%s)\0A\00"
@B1000000 = global i32 0
@TCSANOW = global i32 0
@TCIFLUSH = global i32 0
define i32 @port_open(i8* %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%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 %15
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = tail call i32 @exit(i32 10)
unreachable
15: ; preds = %1
%16 = call i32 @tcgetattr(i32 %9, %struct.termios* %2)
%17 = call i32 @cfmakeraw(%struct.termios* %2)
%18 = load i32, i32* @B1000000
%19 = call i32 @cfsetspeed(%struct.termios* %2, i32 %18)
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 %9, i32 %20, %struct.termios* %2)
%22 = load i32, i32* @TCIFLUSH
%23 = call i32 @tcflush(i32 %9, i32 %22)
ret i32 %9
}