#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 /*<<< orphan*/ my_memfrob(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 = read(s, buffer, 1024)) <= 0)
          exit(EXIT_FAILURE);
        my_memfrob(buffer, len);
        write(s1, buffer, len);
      }
      if (FD_ISSET(s1, &rfds)) {
        memset(buffer, 0, 1024);
        if ((len = read(s1, buffer, 1024)) <= 0)
          exit(EXIT_FAILURE);
        my_memfrob(buffer, len);
        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    11cb <async_read+0x2b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x434(%rbp)
jmp    11d4 <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   1070 <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   1080 <select@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1253 <async_read+0xb3>
lea    0xdc0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    1352 <async_read+0x1b2>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12d5 <async_read+0x135>
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   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jg     12b4 <async_read+0x114>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1090 <my_memfrob@plt>
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     134d <async_read+0x1ad>
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     132c <async_read+0x18c>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1090 <my_memfrob@plt>
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
jmp    1352 <async_read+0x1b2>
jmp    1357 <async_read+0x1b7>
jmp    11e6 <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,%r14d
cmp    %esi,%edi
mov    %esi,%eax
cmovg  %edi,%eax
add    $0x1,%eax
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%r13
lea    0x10(%rsp),%rbx
nop
mov    %r13,%rdi
call   1070 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %r13,%rsi
call   10c0 <FD_SET@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   10c0 <FD_SET@plt>
movl   $0x1,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x8(%rsp),%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1080 <select@plt>
test   %eax,%eax
js     12d3 <async_read+0x133>
je     11d0 <async_read+0x30>
mov    %r14d,%edi
mov    %r13,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1273 <async_read+0xd3>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    12df <async_read+0x13f>
mov    %eax,%r12d
mov    %rbp,%rdi
mov    %eax,%esi
call   1090 <my_memfrob@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11d0 <async_read+0x30>
mov    %rbx,%r12
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    12df <async_read+0x13f>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <my_memfrob@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %r12,%rbx
jmp    11d0 <async_read+0x30>
lea    0xd26(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cfa(%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, %86
%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 %85
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 %62
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 @read(i32 %47, i8* %48, i32 1024)
store i32 %49, i32* %8
%50 = icmp sle i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %44
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%56 = load i32, i32* %8
%57 = call i32 @my_memfrob(i8* %55, i32 %56)
%58 = load i32, i32* %4
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%60 = load i32, i32* %8
%61 = call i32 @write(i32 %58, i8* %59, i32 %60)
br label %62
62: ; preds = %54, %40
%63 = load i32, i32* %4
%64 = call i64 @FD_ISSET(i32 %63, i32* %5)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %84
66: ; preds = %62
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%68 = call i32 @memset(i8* %67, i32 0, i32 1024)
%69 = load i32, i32* %4
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%71 = call i32 @read(i32 %69, i8* %70, i32 1024)
store i32 %71, i32* %8
%72 = icmp sle i32 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %66
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %66
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%78 = load i32, i32* %8
%79 = call i32 @my_memfrob(i8* %77, i32 %78)
%80 = load i32, i32* %3
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%82 = load i32, i32* %8
%83 = call i32 @write(i32 %80, i8* %81, i32 %82)
br label %84
84: ; preds = %76, %62
br label %85
85: ; preds = %84, %37
br label %86
86: ; preds = %85
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 = %26, %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 %26, label %27
26: ; preds = %24, %50, %40
br label %14
27: ; preds = %24
%28 = call i64 @FD_ISSET(i32 %0, i32* %3)
%29 = icmp eq i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %27
%31 = call i32 @memset(i8* %10, i32 0, i32 1024)
%32 = call i32 @read(i32 %0, i8* %10, i32 1024)
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %30
%38 = call i32 @my_memfrob(i8* %10, i32 %32)
%39 = call i32 @write(i32 %1, i8* %10, i32 %32)
br label %40
40: ; preds = %37, %27
%41 = call i64 @FD_ISSET(i32 %1, i32* %3)
%42 = icmp eq i64 %41, 0
br i1 %42, label %26, label %43
43: ; preds = %40
%44 = call i32 @memset(i8* %10, i32 0, i32 1024)
%45 = call i32 @read(i32 %1, i8* %10, i32 1024)
%46 = icmp slt i32 %45, 1
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %43
%51 = call i32 @my_memfrob(i8* %10, i32 %45)
%52 = call i32 @write(i32 %0, i8* %10, i32 %45)
br label %26
}