#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 timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, ...);
long read(int, char *, long);
long select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *, struct timeval *);
char *strerror(scalar_t__);
int /*<<< orphan*/ usleep(int);
long int portread(int fd, char *data) {
  long int n = 0, len = 1;
  long int numport = 0;
  char com1;
  fd_set fds;
  struct timeval timeout;
  *data = '?';
  timeout.tv_sec = 0;
  timeout.tv_usec = 300000;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  numport = select(FD_SETSIZE, &fds, (fd_set *)NULL, (fd_set *)NULL, &timeout);
  usleep(500000);
  if (numport > 0) {
    n = read(fd, data, len);
  }
  if (n < 0) {
    if (errno == EAGAIN) {
      printf("SERIAL EAGAIN ERROR\n");
      return 0;
    } else {
      printf("SERIAL read error %d %s\n", errno, strerror(errno));
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<portread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x3f,(%rax)
movq   $0x0,-0x40(%rbp)
movl   $0x493e0,-0x48(%rbp)
lea    -0x38(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %rax,-0x30(%rbp)
mov    $0x7a120,%edi
call   1090 <usleep@plt>
cmpq   $0x0,-0x30(%rbp)
jle    120c <portread+0x9c>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    128b <portread+0x11b>
mov    0x2dba(%rip),%rax
mov    (%rax),%rax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%rax
jne    124c <portread+0xdc>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1293 <portread+0x123>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1293 <portread+0x123>
movq   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<portread>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movb   $0x3f,(%rsi)
movq   $0x0,0x18(%rsp)
movl   $0x493e0,0x10(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
mov    %rax,%rbx
mov    $0x7a120,%edi
call   10a0 <usleep@plt>
test   %rbx,%rbx
jle    1223 <portread+0xa3>
mov    $0x1,%ebx
mov    $0x1,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <read@plt>
test   %rax,%rax
jns    1248 <portread+0xc8>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rbp
mov    0x2dba(%rip),%rax
cmp    (%rax),%rbp
jne    122a <portread+0xaa>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1248 <portread+0xc8>
mov    $0x1,%ebx
jmp    1248 <portread+0xc8>
mov    %rbp,%rdi
call   1080 <strerror@plt>
lea    0xdc7(%rip),%rdi
xor    %ebx,%ebx
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [21 x i8] c"SERIAL EAGAIN ERROR\0A\00"
@.str.1 = [25 x i8] c"SERIAL read error %d %s\0A\00"
define i64 @portread(i32 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 0, i64* %6
store i64 1, i64* %7
store i64 0, i64* %8
%12 = load i8*, i8** %5
store i8 63, i8* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 300000, i32* %14
%15 = call i32 @FD_ZERO(i32* %10)
%16 = load i32, i32* %4
%17 = call i32 @FD_SET(i32 %16, i32* %10)
%18 = load i32, i32* @FD_SETSIZE
%19 = call i64 @select(i32 %18, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i64 %19, i64* %8
%20 = call i32 @usleep(i32 500000)
%21 = load i64, i64* %8
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load i32, i32* %4
%25 = load i8*, i8** %5
%26 = load i64, i64* %7
%27 = call i64 @read(i32 %24, i8* %25, i64 %26)
store i64 %27, i64* %6
br label %28
28: ; preds = %23, %2
%29 = load i64, i64* %6
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %28
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %3
br label %43
37: ; preds = %31
%38 = load i64, i64* @errno
%39 = load i64, i64* @errno
%40 = call i8* @strerror(i64 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %38, i8* %40)
store i64 0, i64* %3
br label %43
42: ; preds = %28
store i64 1, i64* %3
br label %43
43: ; preds = %42, %37, %35
%44 = load i64, i64* %3
ret i64 %44
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str.1 = [25 x i8] c"SERIAL read error %d %s\0A\00"
@str = [20 x i8] c"SERIAL EAGAIN ERROR\00"
define i64 @portread(i32 %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
store i8 63, i8* %1
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 300000, i32* %8
%9 = call i32 @FD_ZERO(i32* %3)
%10 = call i32 @FD_SET(i32 %0, i32* %3)
%11 = load i32, i32* @FD_SETSIZE
%12 = call i64 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%13 = call i32 @usleep(i32 500000)
%14 = icmp sgt i64 %12, 0
br i1 %14, label %15, label %27
15: ; preds = %2
%16 = call i64 @read(i32 %0, i8* %1, i64 1)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EAGAIN
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %27
24: ; preds = %18
%25 = call i8* @strerror(i64 %19)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %19, i8* %25)
br label %27
27: ; preds = %15, %2, %24, %22
%28 = phi i64 [ 0, %22 ], [ 0, %24 ], [ 1, %2 ], [ 1, %15 ]
ret i64 %28
}