#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*/ B9600;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, char const *, int /*<<< orphan*/, ...);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial(const char *dev) {
  int fd, rc;
  struct termios tio;
  fd = open(dev, O_RDONLY);
  if (fd == -1)
    fatal("Unable to open %s: %s (%d)\n", dev, strerror(errno), errno);
  rc = tcgetattr(fd, &tio);
  if (rc != 0)
    fatal("Failed tcgetattr(): %s (%d)\n", strerror(errno), errno);
  cfmakeraw(&tio);
  rc = cfsetispeed(&tio, B9600);
  if (rc != 0)
    fatal("Failed cfsetispeed(): %s (%d)\n", strerror(errno), errno);
  rc = tcsetattr(fd, TCSAFLUSH, &tio);
  if (rc != 0)
    fatal("Failed tcsetattr(): %s (%d)\n", strerror(errno), errno);
  return fd;
}


x86 assembly (clang, -O0):
<open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11ce <open_serial+0x5e>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1090 <fatal@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     120f <open_serial+0x9f>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
movslq %eax,%rsi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1090 <fatal@plt>
lea    -0x18(%rbp),%rdi
call   1030 <cfmakeraw@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     125f <open_serial+0xef>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
movslq %eax,%rsi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1090 <fatal@plt>
mov    -0xc(%rbp),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     12a9 <open_serial+0x139>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
movslq %eax,%rsi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1090 <fatal@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11b5 <open_serial+0x45>
mov    0x2e31(%rip),%r14
mov    (%r14),%edi
call   1080 <strerror@plt>
mov    (%r14),%ecx
lea    0xe57(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fatal@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1040 <tcgetattr@plt>
test   %eax,%eax
je     11e6 <open_serial+0x76>
mov    0x2dfc(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
movslq %eax,%rsi
mov    (%rbx),%edx
lea    0xe3d(%rip),%rdi
xor    %eax,%eax
call   1090 <fatal@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <cfmakeraw@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <cfsetispeed@plt>
test   %eax,%eax
je     1229 <open_serial+0xb9>
mov    0x2db9(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
movslq %eax,%rsi
mov    (%rbx),%edx
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1090 <fatal@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <tcsetattr@plt>
test   %eax,%eax
je     1263 <open_serial+0xf3>
mov    0x2d7f(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
movslq %eax,%rsi
mov    (%rbx),%edx
lea    0xdfc(%rip),%rdi
xor    %eax,%eax
call   1090 <fatal@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDONLY = global i32 0
@.str = [28 x i8] c"Unable to open %s: %s (%d)\0A\00"
@errno = global i32 0
@.str.1 = [29 x i8] c"Failed tcgetattr(): %s (%d)\0A\00"
@B9600 = global i32 0
@.str.2 = [31 x i8] c"Failed cfsetispeed(): %s (%d)\0A\00"
@TCSAFLUSH = global i32 0
@.str.3 = [29 x i8] c"Failed tcsetattr(): %s (%d)\0A\00"
define i32 @open_serial(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = load i32, i32* @errno
%16 = call i32 (i8*, i8*, i32, ...) @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %14, i32 %15)
br label %17
17: ; preds = %11, %1
%18 = load i32, i32* %3
%19 = call i32 @tcgetattr(i32 %18, %struct.termios* %5)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = sext i32 %24 to i64
%26 = inttoptr i64 %25 to i8*
%27 = load i32, i32* @errno
%28 = call i32 (i8*, i8*, i32, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %27)
br label %29
29: ; preds = %22, %17
%30 = call i32 @cfmakeraw(%struct.termios* %5)
%31 = load i32, i32* @B9600
%32 = call i32 @cfsetispeed(%struct.termios* %5, i32 %31)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %29
%36 = load i32, i32* @errno
%37 = call i32 @strerror(i32 %36)
%38 = sext i32 %37 to i64
%39 = inttoptr i64 %38 to i8*
%40 = load i32, i32* @errno
%41 = call i32 (i8*, i8*, i32, ...) @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %39, i32 %40)
br label %42
42: ; preds = %35, %29
%43 = load i32, i32* %3
%44 = load i32, i32* @TCSAFLUSH
%45 = call i32 @tcsetattr(i32 %43, i32 %44, %struct.termios* %5)
store i32 %45, i32* %4
%46 = load i32, i32* %4
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %42
%49 = load i32, i32* @errno
%50 = call i32 @strerror(i32 %49)
%51 = sext i32 %50 to i64
%52 = inttoptr i64 %51 to i8*
%53 = load i32, i32* @errno
%54 = call i32 (i8*, i8*, i32, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %52, i32 %53)
br label %55
55: ; preds = %48, %42
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDONLY = global i32 0
@.str = [28 x i8] c"Unable to open %s: %s (%d)\0A\00"
@errno = global i32 0
@.str.1 = [29 x i8] c"Failed tcgetattr(): %s (%d)\0A\00"
@B9600 = global i32 0
@.str.2 = [31 x i8] c"Failed cfsetispeed(): %s (%d)\0A\00"
@TCSAFLUSH = global i32 0
@.str.3 = [29 x i8] c"Failed tcsetattr(): %s (%d)\0A\00"
define i32 @open_serial(i8* %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = load i32, i32* @errno
%11 = tail call i32 (i8*, i8*, i32, ...) @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %9, i32 %10)
br label %12
12: ; preds = %7, %1
%13 = call i32 @tcgetattr(i32 %5, %struct.termios* %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i8*
%20 = load i32, i32* @errno
%21 = call i32 (i8*, i8*, i32, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %20)
br label %22
22: ; preds = %15, %12
%23 = call i32 @cfmakeraw(%struct.termios* %2)
%24 = load i32, i32* @B9600
%25 = call i32 @cfsetispeed(%struct.termios* %2, i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = load i32, i32* @errno
%33 = call i32 (i8*, i8*, i32, ...) @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %31, i32 %32)
br label %34
34: ; preds = %27, %22
%35 = load i32, i32* @TCSAFLUSH
%36 = call i32 @tcsetattr(i32 %5, i32 %35, %struct.termios* %2)
%37 = icmp eq i32 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = sext i32 %40 to i64
%42 = inttoptr i64 %41 to i8*
%43 = load i32, i32* @errno
%44 = call i32 (i8*, i8*, i32, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %42, i32 %43)
br label %45
45: ; preds = %38, %34
ret i32 %5
}