#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_sec;
  int tv_usec;
};
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 readport(int fd, char *data) {
  int n = 0, len = 1;
  int numbyte = 0;
  char com1;
  fd_set fds;
  struct timeval timeout;
  timeout.tv_sec = 3;
  timeout.tv_usec = 300000;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  numbyte = select(FD_SETSIZE, &fds, (fd_set *)NULL, (fd_set *)NULL, &timeout);
  *data = '?';
  if (numbyte > 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):
<readport>:
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)
movl   $0x3,-0x30(%rbp)
movl   $0x493e0,-0x2c(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
cmpl   $0x0,-0x1c(%rbp)
jle    11ea <readport+0x8a>
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    1266 <readport+0x106>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <readport+0xc8>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126d <readport+0x10d>
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126d <readport+0x10d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readport>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
movabs $0x493e000000003,%rax
mov    %rax,0x8(%rsp)
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
movb   $0x3f,(%r15)
mov    $0x1,%r14d
test   %eax,%eax
jle    1226 <readport+0xb6>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    1226 <readport+0xb6>
mov    0x2df1(%rip),%rax
mov    (%rax),%rbx
mov    0x2dd7(%rip),%rax
cmp    (%rax),%rbx
jne    1207 <readport+0x97>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
jmp    1226 <readport+0xb6>
mov    %rbx,%rdi
call   1080 <strerror@plt>
lea    0xdea(%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, i32 }
@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 @readport(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 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 3, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i32 300000, i32* %13
%14 = call i32 @FD_ZERO(i32* %10)
%15 = load i32, i32* %4
%16 = call i32 @FD_SET(i32 %15, i32* %10)
%17 = load i32, i32* @FD_SETSIZE
%18 = call i32 @select(i32 %17, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %18, i32* %8
%19 = load i8*, i8** %5
store i8 63, i8* %19
%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, i32 }
@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 @readport(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*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 3, i32* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
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)
store i8 63, i8* %1
%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
}