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

/* Variables and functions */
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int errno;
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int transfer_data(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *);
int io_loop(FILE *client, FILE *server, FILE *protocol) {
  fd_set active_fd_set, read_fd_set;
  int ret;
  int err;
  FD_ZERO(&active_fd_set);
  FD_SET(fileno(client), &active_fd_set);
  FD_SET(fileno(server), &active_fd_set);
  err = 0;
  while (1) {
    read_fd_set = active_fd_set;
    ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
    if (ret < 0) {
      err = errno;
      break;
    }
    if (FD_ISSET(fileno(client), &read_fd_set)) {
      if (feof(client))
        break;
      err = transfer_data(client, server, protocol);
    } else if (FD_ISSET(fileno(server), &read_fd_set)) {
      if (feof(server))
        break;
      err = transfer_data(server, client, protocol);
    }
    if (err)
      break;
  }
  return err;
}


x86 assembly (clang, -O0):
<io_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11fd <io_loop+0x8d>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    12af <io_loop+0x13f>
mov    -0x8(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     124c <io_loop+0xdc>
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1233 <io_loop+0xc3>
jmp    12af <io_loop+0x13f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <transfer_data@plt>
mov    %eax,-0x28(%rbp)
jmp    129b <io_loop+0x12b>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1296 <io_loop+0x126>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1282 <io_loop+0x112>
jmp    12af <io_loop+0x13f>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <transfer_data@plt>
mov    %eax,-0x28(%rbp)
jmp    129b <io_loop+0x12b>
cmpl   $0x0,-0x28(%rbp)
je     12aa <io_loop+0x13a>
jmp    12af <io_loop+0x13f>
jmp    11bc <io_loop+0x4c>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<io_loop>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xc(%rsp),%r12
mov    %r12,%rdi
call   1070 <FD_ZERO@plt>
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1090 <FD_SET@plt>
mov    %r15,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1090 <FD_SET@plt>
mov    0x2e22(%rip),%r13
lea    0x8(%rsp),%r12
jmp    11f3 <io_loop+0x83>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    1268 <io_loop+0xf8>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <transfer_data@plt>
test   %eax,%eax
jne    126a <io_loop+0xfa>
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x0(%r13),%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %eax,%eax
js     125d <io_loop+0xed>
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    11d0 <io_loop+0x60>
mov    %r15,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1255 <io_loop+0xe5>
mov    %r15,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    1268 <io_loop+0xf8>
mov    %r15,%rdi
mov    %rbx,%rsi
jmp    11e7 <io_loop+0x77>
xor    %eax,%eax
test   %eax,%eax
je     11f3 <io_loop+0x83>
jmp    126a <io_loop+0xfa>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
jmp    126a <io_loop+0xfa>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@FD_SETSIZE = global i32 0
@errno = global i32 0
define i32 @io_loop(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%11 = call i32 @FD_ZERO(i32* %7)
%12 = load i32*, i32** %4
%13 = call i32 @fileno(i32* %12)
%14 = call i32 @FD_SET(i32 %13, i32* %7)
%15 = load i32*, i32** %5
%16 = call i32 @fileno(i32* %15)
%17 = call i32 @FD_SET(i32 %16, i32* %7)
store i32 0, i32* %10
br label %18
18: ; preds = %3, %61
%19 = load i32, i32* %7
store i32 %19, i32* %8
%20 = load i32, i32* @FD_SETSIZE
%21 = call i32 @select(i32 %20, i32* %8, i32* null, i32* null, i32* null)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i32, i32* @errno
store i32 %25, i32* %10
br label %62
26: ; preds = %18
%27 = load i32*, i32** %4
%28 = call i32 @fileno(i32* %27)
%29 = call i64 @FD_ISSET(i32 %28, i32* %8)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %26
%32 = load i32*, i32** %4
%33 = call i64 @feof(i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
br label %62
36: ; preds = %31
%37 = load i32*, i32** %4
%38 = load i32*, i32** %5
%39 = load i32*, i32** %6
%40 = call i32 @transfer_data(i32* %37, i32* %38, i32* %39)
store i32 %40, i32* %10
br label %57
41: ; preds = %26
%42 = load i32*, i32** %5
%43 = call i32 @fileno(i32* %42)
%44 = call i64 @FD_ISSET(i32 %43, i32* %8)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %41
%47 = load i32*, i32** %5
%48 = call i64 @feof(i32* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46
br label %62
51: ; preds = %46
%52 = load i32*, i32** %5
%53 = load i32*, i32** %4
%54 = load i32*, i32** %6
%55 = call i32 @transfer_data(i32* %52, i32* %53, i32* %54)
store i32 %55, i32* %10
br label %56
56: ; preds = %51, %41
br label %57
57: ; preds = %56, %36
%58 = load i32, i32* %10
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %57
br label %62
61: ; preds = %57
br label %18
62: ; preds = %60, %50, %35, %24
%63 = load i32, i32* %10
ret i32 %63
}

LLVM IR (-O2):
@FD_SETSIZE = global i32 0
@errno = global i32 0
define i32 @io_loop(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i32 @FD_ZERO(i32* %4)
%9 = call i32 @fileno(i32* %0)
%10 = call i32 @FD_SET(i32 %9, i32* %4)
%11 = call i32 @fileno(i32* %1)
%12 = call i32 @FD_SET(i32 %11, i32* %4)
br label %13
13: ; preds = %38, %3
%14 = load i32, i32* %4
store i32 %14, i32* %5
%15 = load i32, i32* @FD_SETSIZE
%16 = call i32 @select(i32 %15, i32* %5, i32* null, i32* null, i32* null)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i32, i32* @errno
br label %41
20: ; preds = %13
%21 = call i32 @fileno(i32* %0)
%22 = call i64 @FD_ISSET(i32 %21, i32* %5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = call i64 @feof(i32* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %41
27: ; preds = %24
%28 = call i32 @transfer_data(i32* %0, i32* %1, i32* %2)
br label %38
29: ; preds = %20
%30 = call i32 @fileno(i32* %1)
%31 = call i64 @FD_ISSET(i32 %30, i32* %5)
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = call i64 @feof(i32* %1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = call i32 @transfer_data(i32* %1, i32* %0, i32* %2)
br label %38
38: ; preds = %29, %36, %27
%39 = phi i32 [ %28, %27 ], [ %37, %36 ], [ 0, %29 ]
%40 = icmp eq i32 %39, 0
br i1 %40, label %13, label %41
41: ; preds = %38, %33, %24, %18
%42 = phi i32 [ %19, %18 ], [ 0, %33 ], [ %39, %38 ], [ 0, %24 ]
ret i32 %42
}