#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;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ B9600;
int O_NOCTTY;
int O_RDONLY;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int read_port(void) {
  int fd = open("/dev/ttyACM0", O_RDONLY | O_NOCTTY);
  if (fd == -1) {
    perror("open_port: Unable to open /dev/ttyACM0 - ");
  }
  char buffer[32];
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  cfsetospeed(&tty, B9600);
  cfsetispeed(&tty, B9600);
  int n = read(fd, buffer, sizeof(buffer));
  if (n < 0)
    fputs("read failed!\n", stderr);
  return (fd);
}


x86 assembly (clang, -O0):
<read_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11af <read_port+0x3f>
lea    0xe63(%rip),%rdi
call   1090 <perror@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1216 <read_port+0xa6>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_port>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e68(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1205 <read_port+0x95>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e27(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1080 <cfsetispeed@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x20,%edx
call   1060 <read@plt>
test   %eax,%eax
jns    11f9 <read_port+0x89>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1040 <fputs@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
jmp    119e <read_port+0x2e>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [42 x i8] c"open_port: Unable to open /dev/ttyACM0 - \00"
@B9600 = global i32 0
@.str.2 = [14 x i8] c"read failed!\0A\00"
@stderr = global i32 0
define i32 @read_port() {
%1 = alloca i32
%2 = alloca [32 x i8]
%3 = alloca %struct.termios
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = call i32 @memset(%struct.termios* %3, i32 0, i32 4)
%15 = load i32, i32* @B9600
%16 = call i32 @cfsetospeed(%struct.termios* %3, i32 %15)
%17 = load i32, i32* @B9600
%18 = call i32 @cfsetispeed(%struct.termios* %3, i32 %17)
%19 = load i32, i32* %1
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 32)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = load i32, i32* @stderr
%26 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %13
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [42 x i8] c"open_port: Unable to open /dev/ttyACM0 - \00"
@B9600 = global i32 0
@.str.2 = [14 x i8] c"read failed!\0A\00"
@stderr = global i32 0
define i32 @read_port() {
%1 = alloca [32 x i8]
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%12 = bitcast %struct.termios* %2 to i8*
%13 = call i32 @memset(%struct.termios* %2, i32 0, i32 4)
%14 = load i32, i32* @B9600
%15 = call i32 @cfsetospeed(%struct.termios* %2, i32 %14)
%16 = load i32, i32* @B9600
%17 = call i32 @cfsetispeed(%struct.termios* %2, i32 %16)
%18 = call i32 @read(i32 %6, i8* %11, i32 32)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = load i32, i32* @stderr
%22 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %10
ret i32 %6
}