#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 */
int EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(int);
int readport110(int fd, char *data110) {
  int n = 0, len = 107;
  int numbyte = 0;
  char com1;
  fd_set fds;
  struct timeval timeout;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  timeout.tv_sec = 0;
  timeout.tv_usec = 300000;
  numbyte = select(FD_SETSIZE, &fds, (fd_set *)NULL, (fd_set *)NULL, &timeout);
  printf("numbyte = %i\n", numbyte);
  if (numbyte > 0) {
    n = read(fd, data110, len);
    printf("readport110 n = %i\n", n);
    data110[109] = '\0';
    printf("data110 = %s\n", data110);
  }
  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):
<readport110>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x6b,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1080 <FD_SET@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x493e0,-0x38(%rbp)
mov    0x2e31(%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)
mov    -0x1c(%rbp),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    1220 <readport110+0xc0>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x6d(%rax)
mov    -0x10(%rbp),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jge    1296 <readport110+0x136>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%eax
jne    125c <readport110+0xfc>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    129d <readport110+0x13d>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %rax,%rdx
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    129d <readport110+0x13d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readport110>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
movl   $0x493e0,0x8(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
mov    %eax,%ebx
lea    0xe36(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%r14d
test   %ebx,%ebx
jle    125a <readport110+0xea>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x6b,%edx
call   1050 <read@plt>
mov    %eax,%ebx
lea    0xe19(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movb   $0x0,0x6d(%r15)
lea    0xe18(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jns    125a <readport110+0xea>
mov    0x2db9(%rip),%rax
mov    (%rax),%ebx
mov    0x2da0(%rip),%rax
cmp    (%rax),%ebx
jne    123d <readport110+0xcd>
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
jmp    125a <readport110+0xea>
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xde5(%rip),%rdi
xor    %r14d,%r14d
mov    %ebx,%esi
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
@.str = [14 x i8] c"numbyte = %i\0A\00"
@.str.1 = [20 x i8] c"readport110 n = %i\0A\00"
@.str.2 = [14 x i8] c"data110 = %s\0A\00"
@errno = global i32 0
@EAGAIN = global i32 0
@.str.3 = [21 x i8] c"SERIAL EAGAIN ERROR\0A\00"
@.str.4 = [25 x i8] c"SERIAL read error %d %s\0A\00"
define i32 @readport110(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 107, i32* %7
store i32 0, i32* %8
%12 = call i32 @FD_ZERO(i32* %10)
%13 = load i32, i32* %4
%14 = call i32 @FD_SET(i32 %13, i32* %10)
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 300000, i32* %16
%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 i32, i32* %8
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %8
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %2
%24 = load i32, i32* %4
%25 = load i8*, i8** %5
%26 = load i32, i32* %7
%27 = call i32 @read(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 109
store i8 0, i8* %31
%32 = load i8*, i8** %5
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %23, %2
%35 = load i32, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %48
37: ; preds = %34
%38 = load i32, i32* @errno
%39 = load i32, i32* @EAGAIN
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %49
43: ; preds = %37
%44 = load i32, i32* @errno
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %44, i8* %46)
store i32 0, i32* %3
br label %49
48: ; preds = %34
store i32 1, i32* %3
br label %49
49: ; preds = %48, %43, %41
%50 = load i32, i32* %3
ret i32 %50
}

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