#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 *, ...);
int read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(scalar_t__);
int portread(int fd, char *data) {
  int n = 0, len = 1;
  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);
  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    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
movq   $0x0,-0x30(%rbp)
movl   $0x493e0,-0x38(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11eb <portread+0x8b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1267 <portread+0x107>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jne    1229 <portread+0xc9>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126e <portread+0x10e>
mov    0x2da8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126e <portread+0x10e>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<portread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
movb   $0x3f,(%rsi)
movq   $0x0,0x10(%rsp)
movl   $0x493e0,0x8(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
mov    $0x1,%r14d
test   %eax,%eax
jle    1227 <portread+0xb7>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    1227 <portread+0xb7>
mov    0x2df0(%rip),%rax
mov    (%rax),%rbx
mov    0x2dd6(%rip),%rax
cmp    (%rax),%rbx
jne    1208 <portread+0x98>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
jmp    1227 <portread+0xb7>
mov    %rbx,%rdi
call   1080 <strerror@plt>
lea    0xde9(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
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 i32 @portread(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 1, i32* %7
store i32 0, i32* %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 i32 @select(i32 %18, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %2
%23 = load i32, i32* %4
%24 = load i8*, i8** %5
%25 = load i32, i32* %7
%26 = call i32 @read(i32 %23, i8* %24, i32 %25)
store i32 %26, i32* %6
br label %27
27: ; preds = %22, %2
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %27
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %42
36: ; preds = %30
%37 = load i64, i64* @errno
%38 = load i64, i64* @errno
%39 = call i8* @strerror(i64 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %37, i8* %39)
store i32 0, i32* %3
br label %42
41: ; preds = %27
store i32 1, i32* %3
br label %42
42: ; preds = %41, %36, %34
%43 = load i32, i32* %3
ret i32 %43
}

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 i32 @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 i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %2
%15 = call i32 @read(i32 %0, i8* %1, i32 1)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %26
23: ; preds = %17
%24 = call i8* @strerror(i64 %18)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %18, i8* %24)
br label %26
26: ; preds = %14, %2, %23, %21
%27 = phi i32 [ 0, %21 ], [ 0, %23 ], [ 1, %2 ], [ 1, %14 ]
ret i32 %27
}