#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*/ EXIT_FAILURE;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int net_read(int, char *, int);
int /*<<< orphan*/ net_write(int, char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ write(int, char *, int);
void async_read(int s, int s1) {
  fd_set rfds;
  struct timeval tv;
  int max = s > s1 ? s : s1;
  int len, r;
  char buffer[1024];
  max++;
  while (1) {
    FD_ZERO(&rfds);
    FD_SET(s, &rfds);
    FD_SET(s1, &rfds);
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    if ((r = select(max, &rfds, NULL, NULL, &tv)) < 0) {
      perror("select:");
      exit(EXIT_FAILURE);
    } else if (r > 0) {
      if (FD_ISSET(s, &rfds)) {
        memset(buffer, 0, 1024);
        if ((len = net_read(s, buffer, 1024)) == 0)
          continue;
        write(s1, buffer, len);
      }
      if (FD_ISSET(s1, &rfds)) {
        memset(buffer, 0, 1024);
        if ((len = read(s1, buffer, 1024)) <= 0)
          exit(EXIT_FAILURE);
        net_write(s, buffer, len);
      }
    }
  }
}


x86 assembly (clang, -O0):
<async_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    11db <async_read+0x2b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x434(%rbp)
jmp    11e4 <async_read+0x34>
mov    -0x8(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x434(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
lea    -0xc(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10c0 <FD_SET@plt>
movl   $0x1,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x24(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1263 <async_read+0xb3>
lea    0xdb0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    133b <async_read+0x18b>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12cd <async_read+0x11d>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1070 <net_read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    12bb <async_read+0x10b>
jmp    11f6 <async_read+0x46>
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1336 <async_read+0x186>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jg     1324 <async_read+0x174>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   10d0 <net_write@plt>
jmp    133b <async_read+0x18b>
jmp    1340 <async_read+0x190>
jmp    11f6 <async_read+0x46>

x86 assembly (clang, -O2):
<async_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %esi,%r15d
mov    %edi,%ebp
cmp    %esi,%edi
mov    %esi,%r14d
cmovg  %edi,%r14d
add    $0x1,%r14d
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r12
lea    0x20(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
movl   $0x1,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1090 <select@plt>
test   %eax,%eax
js     12c0 <async_read+0x110>
je     11f0 <async_read+0x40>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1278 <async_read+0xc8>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1070 <net_read@plt>
test   %eax,%eax
je     11f0 <async_read+0x40>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11f0 <async_read+0x40>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    12cc <async_read+0x11c>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   10d0 <net_write@plt>
jmp    11f0 <async_read+0x40>
lea    0xd39(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"select:\00"
@EXIT_FAILURE = global i32 0
define void @async_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %3
br label %18
16: ; preds = %2
%17 = load i32, i32* %4
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %15, %14 ], [ %17, %16 ]
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 1
store i32 %21, i32* %7
br label %22
22: ; preds = %18, %51, %78
%23 = call i32 @FD_ZERO(i32* %5)
%24 = load i32, i32* %3
%25 = call i32 @FD_SET(i32 %24, i32* %5)
%26 = load i32, i32* %4
%27 = call i32 @FD_SET(i32 %26, i32* %5)
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 1, i32* %28
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %29
%30 = load i32, i32* %7
%31 = call i32 @select(i32 %30, i32* %5, i32* null, i32* null, %struct.timeval* %6)
store i32 %31, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %22
%38 = load i32, i32* %9
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %77
40: ; preds = %37
%41 = load i32, i32* %3
%42 = call i64 @FD_ISSET(i32 %41, i32* %5)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %40
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%46 = call i32 @memset(i8* %45, i32 0, i32 1024)
%47 = load i32, i32* %3
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%49 = call i32 @net_read(i32 %47, i8* %48, i32 1024)
store i32 %49, i32* %8
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %44
br label %22
52: ; preds = %44
%53 = load i32, i32* %4
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%55 = load i32, i32* %8
%56 = call i32 @write(i32 %53, i8* %54, i32 %55)
br label %57
57: ; preds = %52, %40
%58 = load i32, i32* %4
%59 = call i64 @FD_ISSET(i32 %58, i32* %5)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %76
61: ; preds = %57
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%63 = call i32 @memset(i8* %62, i32 0, i32 1024)
%64 = load i32, i32* %4
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%66 = call i32 @read(i32 %64, i8* %65, i32 1024)
store i32 %66, i32* %8
%67 = icmp sle i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %61
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %61
%72 = load i32, i32* %3
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%74 = load i32, i32* %8
%75 = call i32 @net_write(i32 %72, i8* %73, i32 %74)
br label %76
76: ; preds = %71, %57
br label %77
77: ; preds = %76, %37
br label %78
78: ; preds = %77
br label %22
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"select:\00"
@EXIT_FAILURE = global i32 0
define void @async_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca [1024 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = icmp sgt i32 %0, %1
%9 = select i1 %8, i32 %0, i32 %1
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = add nsw i32 %9, 1
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
br label %14
14: ; preds = %38, %2
%15 = call i32 @FD_ZERO(i32* %3)
%16 = call i32 @FD_SET(i32 %0, i32* %3)
%17 = call i32 @FD_SET(i32 %1, i32* %3)
store i32 1, i32* %12
store i64 0, i64* %13
%18 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = icmp eq i32 %18, 0
br i1 %25, label %38, label %26
26: ; preds = %24
%27 = call i64 @FD_ISSET(i32 %0, i32* %3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %26
%30 = call i32 @memset(i8* %10, i32 0, i32 1024)
%31 = call i32 @net_read(i32 %0, i8* %10, i32 1024)
%32 = icmp eq i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = call i32 @write(i32 %1, i8* %10, i32 %31)
br label %35
35: ; preds = %33, %26
%36 = call i64 @FD_ISSET(i32 %1, i32* %3)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35, %46, %24, %29
br label %14
39: ; preds = %35
%40 = call i32 @memset(i8* %10, i32 0, i32 1024)
%41 = call i32 @read(i32 %1, i8* %10, i32 1024)
%42 = icmp slt i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %39
%47 = call i32 @net_write(i32 %0, i8* %10, i32 %41)
br label %38
}