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

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CS8;
int CSTOPB;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned char);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ stdout;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, const char *argv[]) {
  int serialFD;
  struct termios serialopts;
  serialFD = open("/dev/cu.usbserial", O_RDWR | O_NOCTTY);
  if (serialFD < 0) {
    perror("couldn't open serial port");
    return 1;
  }
  if (tcgetattr(serialFD, &serialopts) != 0) {
    perror("Unable to get serial port options");
    return 1;
  }
  serialopts.c_cflag = CSTOPB | CS8 | CLOCAL | CREAD;
  cfmakeraw(&serialopts);
  cfsetspeed(&serialopts, B115200);
  if (tcsetattr(serialFD, TCSANOW, &serialopts) != 0) {
    perror("Unable to initialize serial port");
    return 1;
  }
  while (1) {
    unsigned char ch;
    read(serialFD, &ch, 1);
    printf("%02x\n", ch);
    fflush(stdout);
  }
  close(serialFD);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
lea    0xe41(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e9 <main+0x59>
lea    0xe3a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <main+0x139>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
je     1217 <main+0x87>
lea    0xe26(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <main+0x139>
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    0x2da9(%rip),%rcx
or     (%rcx),%eax
mov    0x2d68(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <cfmakeraw@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1040 <cfsetspeed@plt>
mov    -0x14(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
je     1290 <main+0x100>
lea    0xdcf(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <main+0x139>
jmp    1295 <main+0x105>
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
lea    0xdc2(%rip),%rdi
movzbl -0x19(%rbp),%esi
call   1030 <printf@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1295 <main+0x105>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e47(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4d(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1276 <main+0xe6>
mov    %eax,%r14d
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
test   %rax,%rax
jne    127f <main+0xef>
mov    0x2dc1(%rip),%rax
mov    0x2dea(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2da7(%rip),%rax
or     (%rax),%ecx
mov    0x2dde(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <cfmakeraw@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <cfsetspeed@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1090 <tcsetattr@plt>
test   %rax,%rax
jne    1288 <main+0xf8>
lea    0xf(%rsp),%r15
lea    0xe2c(%rip),%rbp
mov    0x2d6e(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl 0xf(%rsp),%esi
mov    %rbp,%rdi
call   1030 <printf@plt>
mov    (%rbx),%edi
call   1060 <fflush@plt>
jmp    1250 <main+0xc0>
lea    0xd95(%rip),%rdi
jmp    128f <main+0xff>
lea    0xda6(%rip),%rdi
jmp    128f <main+0xff>
lea    0xdbf(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [18 x i8] c"/dev/cu.usbserial\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [26 x i8] c"couldn't open serial port\00"
@.str.2 = [34 x i8] c"Unable to get serial port options\00"
@CSTOPB = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str.3 = [33 x i8] c"Unable to initialize serial port\00"
@.str.4 = [6 x i8] c"%02x\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %49
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i64 @tcgetattr(i32 %18, %struct.termios* %7)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %49
23: ; preds = %17
%24 = load i32, i32* @CSTOPB
%25 = load i32, i32* @CS8
%26 = or i32 %24, %25
%27 = load i32, i32* @CLOCAL
%28 = or i32 %26, %27
%29 = load i32, i32* @CREAD
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
store i32 %30, i32* %31
%32 = call i32 @cfmakeraw(%struct.termios* %7)
%33 = load i32, i32* @B115200
%34 = call i32 @cfsetspeed(%struct.termios* %7, i32 %33)
%35 = load i32, i32* %6
%36 = load i32, i32* @TCSANOW
%37 = call i64 @tcsetattr(i32 %35, i32 %36, %struct.termios* %7)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %23
%40 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %49
41: ; preds = %23
br label %42
42: ; preds = %41, %42
%43 = load i32, i32* %6
%44 = call i32 @read(i32 %43, i8* %8, i32 1)
%45 = load i8, i8* %8
%46 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %45)
%47 = load i32, i32* @stdout
%48 = call i32 @fflush(i32 %47)
br label %42
49: ; preds = %39, %21, %15
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [18 x i8] c"/dev/cu.usbserial\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [26 x i8] c"couldn't open serial port\00"
@.str.2 = [34 x i8] c"Unable to get serial port options\00"
@CSTOPB = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str.3 = [33 x i8] c"Unable to initialize serial port\00"
@.str.4 = [6 x i8] c"%02x\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca i8
%5 = bitcast %struct.termios* %3 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %41
13: ; preds = %2
%14 = call i64 @tcgetattr(i32 %9, %struct.termios* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %41
18: ; preds = %13
%19 = load i32, i32* @CSTOPB
%20 = load i32, i32* @CS8
%21 = or i32 %20, %19
%22 = load i32, i32* @CLOCAL
%23 = or i32 %21, %22
%24 = load i32, i32* @CREAD
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = call i32 @cfmakeraw(%struct.termios* %3)
%28 = load i32, i32* @B115200
%29 = call i32 @cfsetspeed(%struct.termios* %3, i32 %28)
%30 = load i32, i32* @TCSANOW
%31 = call i64 @tcsetattr(i32 %9, i32 %30, %struct.termios* %3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %18
%34 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %41
35: ; preds = %18, %35
%36 = call i32 @read(i32 %9, i8* %4, i32 1)
%37 = load i8, i8* %4
%38 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %37)
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
br label %35
41: ; preds = %33, %16, %11
ret i32 1
}