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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int read_port(int *fd_ser, unsigned char *buffer) {
  int a;
  fd_set rfds;
  struct timeval tv;
  int retval;
  FD_ZERO(&rfds);
  FD_SET(*fd_ser, &rfds);
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  retval = select(*fd_ser + 1, &rfds, NULL, NULL, &tv);
  if (retval) {
    a = read(*fd_ser, buffer, 255);
    return a;
  } else
    return 0;
}


x86 assembly (clang, -O0):
<read_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1060 <FD_SET@plt>
movl   $0x1,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11c3 <read_port+0x83>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    $0xff,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <read_port+0x8a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_port>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    (%rbx),%edi
add    $0x1,%edi
xor    %r15d,%r15d
lea    0x8(%rsp),%r8
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
je     11a8 <read_port+0x68>
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0xff,%edx
call   1030 <read@plt>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @read_port(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%10 = call i32 @FD_ZERO(i32* %7)
%11 = load i32*, i32** %4
%12 = load i32, i32* %11
%13 = call i32 @FD_SET(i32 %12, i32* %7)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %15
%16 = load i32*, i32** %4
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
%19 = call i32 @select(i32 %18, i32* %7, i32* null, i32* null, %struct.timeval* %8)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = load i32*, i32** %4
%24 = load i32, i32* %23
%25 = load i8*, i8** %5
%26 = call i32 @read(i32 %24, i8* %25, i32 255)
store i32 %26, i32* %6
%27 = load i32, i32* %6
store i32 %27, i32* %3
br label %29
28: ; preds = %2
store i32 0, i32* %3
br label %29
29: ; preds = %28, %22
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @read_port(i32* nocapture %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 = call i32 @FD_ZERO(i32* %3)
%8 = load i32, i32* %0
%9 = call i32 @FD_SET(i32 %8, i32* %3)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 1, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* %0
%13 = add nsw i32 %12, 1
%14 = call i32 @select(i32 %13, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32, i32* %0
%18 = call i32 @read(i32 %17, i8* %1, i32 255)
br label %19
19: ; preds = %2, %16
%20 = phi i32 [ %18, %16 ], [ 0, %2 ]
ret i32 %20
}