#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 */

/* Variables and functions */
int /*<<< orphan*/ B9600;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int errno;
int open(char *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ set_blocking(int, int /*<<< orphan*/);
int /*<<< orphan*/ set_interface_attribs(int, int /*<<< orphan*/,
                                         int /*<<< orphan*/);
char *strerror(int);
int /*<<< orphan*/ write(int, char *, int);
int read_port() {
  char *portname = "/dev/ttyACM0";
  int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    printf("error %d opening %s: %s", errno, portname, strerror(errno));
    return (0);
  }
  set_interface_attribs(fd, B9600, 0);
  set_blocking(fd, 0);
  write(fd, "hello!\n", 7);
  char buf[100];
  int n = read(fd, buf, sizeof buf);
  printf("data is:", fd);
}


x86 assembly (clang, -O0):
<read_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    0xe7e(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1209 <read_port+0x99>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x94(%rbp),%esi
mov    -0x90(%rbp),%rdx
mov    %rax,%rcx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1262 <read_port+0xf2>
mov    -0x14(%rbp),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <set_interface_attribs@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <set_blocking@plt>
mov    -0x14(%rbp),%edi
lea    0xdf5(%rip),%rsi
mov    $0x7,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1070 <read@plt>
mov    %eax,-0x84(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_port>:
push   %rbx
sub    $0x70,%rsp
mov    0x2e6c(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
lea    0xe69(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f1 <read_port+0x81>
mov    %eax,%ebx
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <set_interface_attribs@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <set_blocking@plt>
lea    0xe61(%rip),%rsi
mov    %ebx,%edi
mov    $0x7,%edx
call   1030 <write@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1070 <read@plt>
lea    0xe47(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    121b <read_port+0xab>
mov    0x2dc8(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xe05(%rip),%rdi
lea    0xdf1(%rip),%rdx
mov    %ebx,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.1 = [24 x i8] c"error %d opening %s: %s\00"
@errno = global i32 0
@B9600 = global i32 0
@.str.2 = [8 x i8] c"hello!\0A\00"
@.str.3 = [9 x i8] c"data is:\00"
define i32 @read_port() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i32
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_SYNC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %6, i32 %11)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = load i8*, i8** %2
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %16, i8* %17, i8* %19)
store i32 0, i32* %1
br label %34
21: ; preds = %0
%22 = load i32, i32* %3
%23 = load i32, i32* @B9600
%24 = call i32 @set_interface_attribs(i32 %22, i32 %23, i32 0)
%25 = load i32, i32* %3
%26 = call i32 @set_blocking(i32 %25, i32 0)
%27 = load i32, i32* %3
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 7)
%29 = load i32, i32* %3
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 100)
store i32 %31, i32* %5
%32 = load i32, i32* %3
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %21, %15
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.1 = [24 x i8] c"error %d opening %s: %s\00"
@errno = global i32 0
@B9600 = global i32 0
@.str.2 = [8 x i8] c"hello!\0A\00"
@.str.3 = [9 x i8] c"data is:\00"
define i32 @read_port() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_SYNC
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %21
13: ; preds = %0
%14 = load i32, i32* @B9600
%15 = tail call i32 @set_interface_attribs(i32 %7, i32 %14, i32 0)
%16 = tail call i32 @set_blocking(i32 %7, i32 0)
%17 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 7)
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%19 = call i32 @read(i32 %7, i8* %18, i32 100)
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %7)
br label %21
21: ; preds = %13, %9
ret i32 0
}