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

/* Variables and functions */
int B115200;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int block_read(int, char *, size_t *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, char **argv) {
  int fd, ret;
  size_t size;
  struct termios oldtio, newtio;
  char buf[32] = {0};
  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd < 0) {
    perror("/dev/ttyS0");
    exit(-1);
  }
  tcgetattr(fd, &oldtio);
  memset(&newtio, 0, sizeof(newtio));
  newtio.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR;
  newtio.c_oflag = 0;
  newtio.c_lflag = 0;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &newtio);
  size = 32;
  ret = block_read(fd, buf, &size, 5 * 1000);
  printf("ret = %d, size = %ld\n", ret, size);
  if (ret >= 0)
    printf("[%s]\n", buf);
  tcsetattr(fd, TCSANOW, &oldtio);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
mov    0x2dfe(%rip),%rax
or     (%rax),%esi
lea    0xe15(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1213 <main+0x73>
lea    0xdfc(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1070 <tcgetattr@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    0x2d89(%rip),%rcx
or     (%rcx),%eax
mov    0x2d48(%rip),%rcx
or     (%rcx),%eax
mov    0x2d7f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x60(%rbp)
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x14(%rbp),%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   1080 <tcsetattr@plt>
movq   $0x20,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x1388,%ecx
call   10c0 <block_read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jl     12e9 <main+0x149>
lea    -0x80(%rbp),%rsi
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
mov    0x2e2f(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movaps %xmm0,0x10(%rsp)
mov    0x2e10(%rip),%rax
or     (%rax),%esi
lea    0xe37(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12d9 <main+0x139>
mov    %eax,%ebx
lea    0x58(%rsp),%rsi
mov    %eax,%edi
call   1070 <tcgetattr@plt>
lea    0x38(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2d99(%rip),%rax
mov    0x2db2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d6f(%rip),%rax
or     (%rax),%ecx
mov    0x2da6(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x38(%rsp)
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x50(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x40(%rsp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2d7c(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
movq   $0x20,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
mov    $0x1388,%ecx
call   10c0 <block_read@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdx
lea    0xd85(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
js     12b6 <main+0x116>
lea    0xd87(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%esi
lea    0x58(%rsp),%rdx
mov    %ebx,%edi
call   1080 <tcsetattr@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd30(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32 }
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@B115200 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [22 x i8] c"ret = %d, size = %ld\0A\00"
@.str.2 = [6 x i8] c"[%s]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca %struct.termios
%10 = alloca %struct.termios
%11 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [32 x i8]* %11 to i8*
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %2
%25 = load i32, i32* %6
%26 = call i32 @tcgetattr(i32 %25, %struct.termios* %9)
%27 = call i32 @memset(%struct.termios* %10, i32 0, i32 32)
%28 = load i32, i32* @B115200
%29 = load i32, i32* @CS8
%30 = or i32 %28, %29
%31 = load i32, i32* @CLOCAL
%32 = or i32 %30, %31
%33 = load i32, i32* @CREAD
%34 = or i32 %32, %33
%35 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* @IGNPAR
%37 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 3
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 2
store i64 0, i64* %38
%39 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 1
store i64 0, i64* %39
%40 = load i32, i32* %6
%41 = load i32, i32* @TCIFLUSH
%42 = call i32 @tcflush(i32 %40, i32 %41)
%43 = load i32, i32* %6
%44 = load i32, i32* @TCSANOW
%45 = call i32 @tcsetattr(i32 %43, i32 %44, %struct.termios* %10)
store i64 32, i64* %8
%46 = load i32, i32* %6
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%48 = call i32 @block_read(i32 %46, i8* %47, i64* %8, i32 5000)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = load i64, i64* %8
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %49, i64 %50)
%52 = load i32, i32* %7
%53 = icmp sge i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %24
%55 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %54, %24
%58 = load i32, i32* %6
%59 = load i32, i32* @TCSANOW
%60 = call i32 @tcsetattr(i32 %58, i32 %59, %struct.termios* %9)
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@B115200 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [22 x i8] c"ret = %d, size = %ld\0A\00"
@.str.2 = [6 x i8] c"[%s]\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = alloca [32 x i8]
%7 = bitcast i64* %3 to i8*
%8 = bitcast %struct.termios* %4 to i8*
%9 = bitcast %struct.termios* %5 to i8*
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %12, %11
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %13, %14
%16 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = call i32 @tcgetattr(i32 %16, %struct.termios* %4)
%23 = call i32 @memset(%struct.termios* %5, i32 0, i32 32)
%24 = load i32, i32* @B115200
%25 = load i32, i32* @CS8
%26 = or i32 %25, %24
%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* %5, i64 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* @IGNPAR
%33 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 3
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 1
%35 = bitcast i64* %34 to i8*
%36 = load i32, i32* @TCIFLUSH
%37 = call i32 @tcflush(i32 %16, i32 %36)
%38 = load i32, i32* @TCSANOW
%39 = call i32 @tcsetattr(i32 %16, i32 %38, %struct.termios* %5)
store i64 32, i64* %3
%40 = call i32 @block_read(i32 %16, i8* %10, i64* %3, i32 5000)
%41 = load i64, i64* %3
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %40, i64 %41)
%43 = icmp sgt i32 %40, -1
br i1 %43, label %44, label %46
44: ; preds = %21
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %46
46: ; preds = %44, %21
%47 = load i32, i32* @TCSANOW
%48 = call i32 @tcsetattr(i32 %16, i32 %47, %struct.termios* %4)
%49 = call i32 @close(i32 %16)
ret i32 0
}