#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 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);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int read_pkt(int, char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
scalar_t__ write(int, char *, int);
scalar_t__ write_pkt(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[1800];
  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)) {
        len = read_pkt(s, buffer);
        if ((write(s1, buffer, len)) < 0) {
          perror("write(net):");
          exit(1);
        }
      }
      if (FD_ISSET(s1, &rfds)) {
        if ((len = read(s1, buffer, 1800)) < 0) {
          perror("read(tun):");
          exit(1);
        }
        if ((write_pkt(s, buffer, len)) < 0)
          exit(1);
      }
    }
  }
}


x86 assembly (clang, -O0):
<async_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x750,%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,-0x744(%rbp)
jmp    11d4 <async_read+0x34>
mov    -0x8(%rbp),%eax
mov    %eax,-0x744(%rbp)
mov    -0x744(%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    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    1338 <async_read+0x198>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12bc <async_read+0x11c>
mov    -0x4(%rbp),%edi
lea    -0x740(%rbp),%rsi
call   1030 <read_pkt@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x740(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    12b7 <async_read+0x117>
lea    0xd60(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12bc <async_read+0x11c>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1333 <async_read+0x193>
mov    -0x8(%rbp),%edi
lea    -0x740(%rbp),%rsi
mov    $0x708,%edx
call   1070 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1308 <async_read+0x168>
lea    0xd1b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x740(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1060 <write_pkt@plt>
cmp    $0x0,%rax
jge    132e <async_read+0x18e>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1333 <async_read+0x193>
jmp    1338 <async_read+0x198>
jmp    133d <async_read+0x19d>
jmp    11e6 <async_read+0x46>

x86 assembly (clang, -O2):
<async_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x728,%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     1298 <async_read+0xf8>
je     11e0 <async_read+0x40>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1251 <async_read+0xb1>
mov    %ebp,%edi
mov    %r13,%rsi
call   1030 <read_pkt@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
js     12b2 <async_read+0x112>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     11e0 <async_read+0x40>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x708,%edx
call   1070 <read@plt>
test   %eax,%eax
js     12bb <async_read+0x11b>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1060 <write_pkt@plt>
test   %rax,%rax
jns    11e0 <async_read+0x40>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd61(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xd4f(%rip),%rdi
jmp    12c2 <async_read+0x122>
lea    0xd52(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%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
@.str.1 = [12 x i8] c"write(net):\00"
@.str.2 = [11 x i8] c"read(tun):\00"
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 [1800 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, %80
%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 %79
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 = load i32, i32* %3
%46 = getelementptr inbounds [1800 x i8], [1800 x i8]* %10, i64 0, i64 0
%47 = call i32 @read_pkt(i32 %45, i8* %46)
store i32 %47, i32* %8
%48 = load i32, i32* %4
%49 = getelementptr inbounds [1800 x i8], [1800 x i8]* %10, i64 0, i64 0
%50 = load i32, i32* %8
%51 = call i64 @write(i32 %48, i8* %49, i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %44
br label %57
57: ; preds = %56, %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 %78
61: ; preds = %57
%62 = load i32, i32* %4
%63 = getelementptr inbounds [1800 x i8], [1800 x i8]* %10, i64 0, i64 0
%64 = call i32 @read(i32 %62, i8* %63, i32 1800)
store i32 %64, i32* %8
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %61
%70 = load i32, i32* %3
%71 = getelementptr inbounds [1800 x i8], [1800 x i8]* %10, i64 0, i64 0
%72 = load i32, i32* %8
%73 = call i64 @write_pkt(i32 %70, i8* %71, i32 %72)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %69
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %69
br label %78
78: ; preds = %77, %57
br label %79
79: ; preds = %78, %37
br label %80
80: ; preds = %79
br label %22
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"select:\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"write(net):\00"
@.str.2 = [11 x i8] c"read(tun):\00"
define void @async_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca [1800 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 [1800 x i8], [1800 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, %46, %37
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 %37, label %30
30: ; preds = %27
%31 = call i32 @read_pkt(i32 %0, i8* %10)
%32 = call i64 @write(i32 %1, i8* %10, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30, %27
%38 = call i64 @FD_ISSET(i32 %1, i32* %3)
%39 = icmp eq i64 %38, 0
br i1 %39, label %26, label %40
40: ; preds = %37
%41 = call i32 @read(i32 %1, i8* %10, i32 1800)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %40
%47 = call i64 @write_pkt(i32 %0, i8* %10, i32 %41)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %26
49: ; preds = %46
%50 = call i32 @exit(i32 1)
unreachable
}