#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;
  int c_iflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
};
typedef int /*<<< orphan*/ new_tio;
typedef int /*<<< orphan*/ c;

/* Variables and functions */
int B4800;
int CLOCAL;
int CREAD;
int CS8;
int IGNPAR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main() {
  int fd;
  struct termios old_tio;
  struct termios new_tio;
  int rc;
  int n;
  char c;
  fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
  if (fd < 0)
    return -1;
  tcgetattr(fd, &old_tio);
  memset(&new_tio, 0, sizeof(new_tio));
  new_tio.c_cflag = 0 | B4800 | CS8 | CLOCAL | CREAD;
  new_tio.c_iflag = 0 | IGNPAR;
  new_tio.c_oflag = 0;
  new_tio.c_lflag = 0;
  new_tio.c_cc[VMIN] = 1;
  new_tio.c_cc[VTIME] = 0;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &new_tio);
  n = 0;
  for (;;) {
    rc = read(fd, &c, sizeof(c));
    if (rc < 0) {
      perror("read");
      return -1;
    }
    if (n % 16 == 0) {
      n = 0;
      printf("\n");
    }
    printf(" 0x%02x", (int)(c & 0xff));
    fflush(stdout);
    ++n;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d6 <main+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <main+0x195>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <tcgetattr@plt>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
or     $0x0,%eax
mov    0x2dc3(%rip),%rcx
or     (%rcx),%eax
mov    0x2d72(%rip),%rcx
or     (%rcx),%eax
mov    0x2db9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
or     $0x0,%eax
mov    %eax,-0x44(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2d7a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x40(%rbp),%rax
mov    0x2d3d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x8(%rbp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1090 <tcsetattr@plt>
movl   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    12c8 <main+0x138>
lea    0xd56(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <main+0x195>
mov    -0x50(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12f1 <main+0x161>
movl   $0x0,-0x50(%rbp)
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x51(%rbp),%esi
and    $0xff,%esi
lea    0xd12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1292 <main+0x102>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    0x2e35(%rip),%rax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3b(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
jns    11e0 <main+0x40>
mov    $0xffffffff,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
lea    0x30(%rsp),%rsi
mov    %eax,%edi
call   1090 <tcgetattr@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2dcf(%rip),%rax
mov    0x2db8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d65(%rip),%rax
or     (%rax),%ecx
mov    0x2dac(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
mov    0x18(%rsp),%rax
mov    0x2d75(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d3c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   10a0 <tcsetattr@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
jns    12af <main+0x10f>
lea    0xd68(%rip),%rdi
call   10c0 <perror@plt>
jmp    11ce <main+0x2e>
lea    0xd5c(%rip),%r14
mov    0x2ce3(%rip),%r12
lea    0xf(%rsp),%r15
xor    %ebp,%ebp
jmp    12fe <main+0x15e>
cs nopw 0x0(%rax,%rax,1)
movzbl 0xf(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%edi
call   1080 <fflush@plt>
add    $0x1,%ebp
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
js     129e <main+0xfe>
test   $0xf,%bpl
jne    12d0 <main+0x130>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %ebp,%ebp
jmp    12d0 <main+0x130>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B4800 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [8 x i8] c" 0x%02x\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 0, i32* %1
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %0
store i32 -1, i32* %1
br label %69
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @tcgetattr(i32 %16, %struct.termios* %3)
%18 = call i32 @memset(%struct.termios* %4, i32 0, i32 32)
%19 = load i32, i32* @B4800
%20 = or i32 0, %19
%21 = load i32, i32* @CS8
%22 = or i32 %20, %21
%23 = load i32, i32* @CLOCAL
%24 = or i32 %22, %23
%25 = load i32, i32* @CREAD
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @IGNPAR
%29 = or i32 0, %28
%30 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 4
store i64 0, i64* %31
%32 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 3
store i64 0, i64* %32
%33 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
%34 = load i32*, i32** %33
%35 = load i64, i64* @VMIN
%36 = getelementptr inbounds i32, i32* %34, i64 %35
store i32 1, i32* %36
%37 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
%38 = load i32*, i32** %37
%39 = load i64, i64* @VTIME
%40 = getelementptr inbounds i32, i32* %38, i64 %39
store i32 0, i32* %40
%41 = load i32, i32* %2
%42 = load i32, i32* @TCIFLUSH
%43 = call i32 @tcflush(i32 %41, i32 %42)
%44 = load i32, i32* %2
%45 = load i32, i32* @TCSANOW
%46 = call i32 @tcsetattr(i32 %44, i32 %45, %struct.termios* %4)
store i32 0, i32* %6
br label %47
47: ; preds = %60, %15
%48 = load i32, i32* %2
%49 = call i32 @read(i32 %48, i8* %7, i32 1)
store i32 %49, i32* %5
%50 = load i32, i32* %5
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %69
54: ; preds = %47
%55 = load i32, i32* %6
%56 = srem i32 %55, 16
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %54
store i32 0, i32* %6
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %60
60: ; preds = %58, %54
%61 = load i8, i8* %7
%62 = sext i8 %61 to i32
%63 = and i32 %62, 255
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %63)
%65 = load i32, i32* @stdout
%66 = call i32 @fflush(i32 %65)
%67 = load i32, i32* %6
%68 = add nsw i32 %67, 1
store i32 %68, i32* %6
br label %47
69: ; preds = %52, %14
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B4800 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [5 x i8] c"read\00"
@.str.3 = [8 x i8] c" 0x%02x\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i8
%4 = bitcast %struct.termios* %1 to i8*
%5 = bitcast %struct.termios* %2 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 ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %56, label %11
11: ; preds = %0
%12 = call i32 @tcgetattr(i32 %9, %struct.termios* %1)
%13 = call i32 @memset(%struct.termios* %2, i32 0, i32 32)
%14 = load i32, i32* @B4800
%15 = load i32, i32* @CS8
%16 = or i32 %15, %14
%17 = load i32, i32* @CLOCAL
%18 = or i32 %16, %17
%19 = load i32, i32* @CREAD
%20 = or i32 %18, %19
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @IGNPAR
%23 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%25 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%26 = bitcast i64* %24 to i8*
%27 = load i32*, i32** %25
%28 = load i64, i64* @VMIN
%29 = getelementptr inbounds i32, i32* %27, i64 %28
store i32 1, i32* %29
%30 = load i64, i64* @VTIME
%31 = getelementptr inbounds i32, i32* %27, i64 %30
store i32 0, i32* %31
%32 = load i32, i32* @TCIFLUSH
%33 = call i32 @tcflush(i32 %9, i32 %32)
%34 = load i32, i32* @TCSANOW
%35 = call i32 @tcsetattr(i32 %9, i32 %34, %struct.termios* %2)
%36 = call i32 @read(i32 %9, i8* %3, i32 1)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %46, %11
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %56
40: ; preds = %11, %46
%41 = phi i32 [ %53, %46 ], [ 0, %11 ]
%42 = and i32 %41, 15
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @putchar(i32 10)
br label %46
46: ; preds = %44, %40
%47 = phi i32 [ 0, %44 ], [ %41, %40 ]
%48 = load i8, i8* %3
%49 = zext i8 %48 to i32
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = load i32, i32* @stdout
%52 = call i32 @fflush(i32 %51)
%53 = add nsw i32 %47, 1
%54 = call i32 @read(i32 %9, i8* %3, i32 1)
%55 = icmp slt i32 %54, 0
br i1 %55, label %38, label %40
56: ; preds = %0, %38
ret i32 -1
}