#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 /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
unsigned char communicate(int serialfd, unsigned char data[], int length) {
  struct timeval timeout;
  fd_set rfds;
  int i;
  FD_ZERO(&rfds);
  FD_SET(serialfd, &rfds);
  timeout.tv_sec = 100;
  timeout.tv_usec = 0;
  for (i = 0; i < length; i++) {
    do {
      select(serialfd + 1, &rfds, NULL, NULL, &timeout);
    } while (read(serialfd, &(data[i]), 1) != 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<communicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1060 <FD_SET@plt>
movl   $0x64,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11dc <communicate+0x9c>
jmp    118e <communicate+0x4e>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    118e <communicate+0x4e>
jmp    11ce <communicate+0x8e>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    117d <communicate+0x3d>
xor    %eax,%eax
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<communicate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %rsi,0x10(%rsp)
mov    %edi,%r13d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
movl   $0x64,0x18(%rsp)
movq   $0x0,0x20(%rsp)
test   %r15d,%r15d
jle    11e1 <communicate+0xa1>
lea    0x1(%r13),%ebp
mov    %r15d,%eax
mov    %rax,0x8(%rsp)
xor    %r14d,%r14d
lea    0x4(%rsp),%r15
lea    0x18(%rsp),%r12
mov    0x10(%rsp),%rax
lea    (%rax,%r14,1),%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1050 <select@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11b0 <communicate+0x70>
add    $0x1,%r14
cmp    0x8(%rsp),%r14
jne    11a0 <communicate+0x60>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define zeroext i8 @communicate(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i32 @FD_ZERO(i32* %5)
%9 = call i32 @FD_SET(i32 %0, i32* %5)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 100, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %11
%12 = icmp sgt i32 %2, 0
br i1 %12, label %13, label %26
13: ; preds = %3
%14 = add nsw i32 %0, 1
%15 = zext i32 %2 to i64
br label %16
16: ; preds = %13, %23
%17 = phi i64 [ 0, %13 ], [ %24, %23 ]
%18 = getelementptr inbounds i8, i8* %1, i64 %17
br label %19
19: ; preds = %16, %19
%20 = call i32 @select(i32 %14, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%21 = call i32 @read(i32 %0, i8* %18, i32 1)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %19
23: ; preds = %19
%24 = add nuw nsw i64 %17, 1
%25 = icmp eq i64 %24, %15
br i1 %25, label %26, label %16
26: ; preds = %23, %3
ret i8 0
}