#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*/ buf;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
void relay(int master_fd, int sock_fd) {
  fd_set rset, allset;
  int maxfdp1;
  int shutdown;
  char buf[1024];
  int rdsize;
  close(0);
  close(1);
  close(2);
  shutdown = 0;
  maxfdp1 = ((sock_fd > master_fd ? sock_fd : master_fd) + 1);
  FD_ZERO(&allset);
  FD_SET(master_fd, &allset);
  FD_SET(sock_fd, &allset);
  while (!shutdown) {
    rset = allset;
    select(maxfdp1, &rset, NULL, NULL, NULL);
    if (FD_ISSET(master_fd, &rset)) {
      rdsize = read(master_fd, buf, sizeof(buf));
      if (rdsize > 0) {
        write(sock_fd, buf, rdsize);
      } else {
        shutdown = 1;
      }
    }
    if (FD_ISSET(sock_fd, &rset)) {
      rdsize = read(sock_fd, buf, sizeof(buf));
      if (rdsize > 0) {
        write(master_fd, buf, rdsize);
      } else {
        shutdown = 1;
      }
    }
  }
  close(master_fd);
  close(sock_fd);
  exit(0);
}


x86 assembly (clang, -O0):
<relay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    11cd <relay+0x4d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x428(%rbp)
jmp    11d6 <relay+0x56>
mov    -0x4(%rbp),%eax
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <FD_SET@plt>
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1219 <relay+0x99>
jmp    1301 <relay+0x181>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1299 <relay+0x119>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    128d <relay+0x10d>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
jmp    1294 <relay+0x114>
movl   $0x1,-0x18(%rbp)
jmp    1299 <relay+0x119>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12fc <relay+0x17c>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    12f0 <relay+0x170>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
jmp    12f7 <relay+0x177>
movl   $0x1,-0x18(%rbp)
jmp    12fc <relay+0x17c>
jmp    1203 <relay+0x83>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<relay>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %esi,%r15d
mov    %edi,%ebp
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
cmp    %ebp,%r15d
mov    %ebp,%r14d
cmovg  %r15d,%r14d
add    $0x1,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
lea    0xc(%rsp),%r13
lea    0x10(%rsp),%r12
jmp    1200 <relay+0x80>
nopl   (%rax)
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %bl,%bl
je     127a <relay+0xfa>
mov    0x8(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   1040 <FD_ISSET@plt>
mov    $0x1,%bl
test   %rax,%rax
je     1252 <relay+0xd2>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1250 <relay+0xd0>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1252 <relay+0xd2>
nopl   (%rax)
xor    %ebx,%ebx
mov    %r15d,%edi
mov    %r13,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11fc <relay+0x7c>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11f0 <relay+0x70>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
define void @relay(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = tail call i32 @close(i32 0)
%10 = tail call i32 @close(i32 1)
%11 = tail call i32 @close(i32 2)
%12 = icmp sgt i32 %1, %0
%13 = select i1 %12, i32 %1, i32 %0
%14 = add nsw i32 %13, 1
%15 = call i32 @FD_ZERO(i32* %4)
%16 = call i32 @FD_SET(i32 %0, i32* %4)
%17 = call i32 @FD_SET(i32 %1, i32* %4)
br label %18
18: ; preds = %2, %37
%19 = load i32, i32* %4
store i32 %19, i32* %3
%20 = call i32 @select(i32 %14, i32* %3, i32* null, i32* null, i32* null)
%21 = call i64 @FD_ISSET(i32 %0, i32* %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = call i32 @read(i32 %0, i8* %8, i32 1024)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @write(i32 %1, i8* %8, i32 %24)
br label %28
28: ; preds = %23, %26, %18
%29 = phi i1 [ false, %23 ], [ true, %26 ], [ true, %18 ]
%30 = call i64 @FD_ISSET(i32 %1, i32* %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %28
%33 = call i32 @read(i32 %1, i8* %8, i32 1024)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @write(i32 %0, i8* %8, i32 %33)
br label %37
37: ; preds = %35, %28
br i1 %29, label %18, label %38
38: ; preds = %32, %37
%39 = call i32 @close(i32 %0)
%40 = call i32 @close(i32 %1)
%41 = call i32 @exit(i32 0)
unreachable
}