#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;

/* Variables and functions */
scalar_t__ EINTR;
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 STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ die(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ write_once(int, int);
void netcat(int fd) {
  fd_set rfds, backup_fds;
  FD_ZERO(&backup_fds);
  FD_SET(fd, &backup_fds);
  FD_SET(STDIN_FILENO, &backup_fds);
  for (;;) {
    rfds = backup_fds;
    if (select(((STDIN_FILENO) > (fd) ? (STDIN_FILENO) : (fd)) + 1, &rfds, NULL,
               NULL, NULL) < 0) {
      if (errno == EINTR)
        continue;
      die(EXIT_FAILURE, strerror(errno));
    }
    if (FD_ISSET(fd, &rfds))
      if (!write_once(fd, STDOUT_FILENO))
        break;
    if (FD_ISSET(STDIN_FILENO, &rfds))
      if (!write_once(STDIN_FILENO, fd))
        break;
  }
}


x86 assembly (clang, -O0):
<netcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
cmp    -0x4(%rbp),%eax
jle    11cb <netcat+0x5b>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    11d1 <netcat+0x61>
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1060 <select@plt>
cmp    $0x0,%rax
jge    1239 <netcat+0xc9>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jne    1214 <netcat+0xa4>
jmp    11a2 <netcat+0x32>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%esi
call   1070 <die@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1273 <netcat+0x103>
mov    -0x4(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
call   1040 <write_once@plt>
cmp    $0x0,%eax
jne    126e <netcat+0xfe>
jmp    12b8 <netcat+0x148>
jmp    1273 <netcat+0x103>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12b3 <netcat+0x143>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
call   1040 <write_once@plt>
cmp    $0x0,%eax
jne    12ae <netcat+0x13e>
jmp    12b8 <netcat+0x148>
jmp    12b3 <netcat+0x143>
jmp    11a2 <netcat+0x32>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netcat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <FD_SET@plt>
mov    0x2e27(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %r14,%rsi
call   1090 <FD_SET@plt>
lea    0x4(%rsp),%r14
mov    0x2e28(%rip),%r13
mov    0x2e11(%rip),%r12
mov    0x2e32(%rip),%r15
xchg   %ax,%ax
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
cmp    %ebx,%edi
cmovle %ebx,%edi
add    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
test   %rax,%rax
jns    120e <netcat+0x9e>
mov    0x0(%r13),%rdi
cmp    (%r12),%rdi
je     11c0 <netcat+0x50>
mov    0x2dd9(%rip),%rax
mov    (%rax),%ebp
call   1080 <strerror@plt>
mov    %ebp,%edi
mov    0x2db9(%rip),%rbp
mov    %eax,%esi
call   1070 <die@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     122b <netcat+0xbb>
mov    (%r15),%esi
mov    %ebx,%edi
call   1040 <write_once@plt>
test   %eax,%eax
je     124d <netcat+0xdd>
mov    0x0(%rbp),%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11c0 <netcat+0x50>
mov    0x0(%rbp),%edi
mov    %ebx,%esi
call   1040 <write_once@plt>
test   %eax,%eax
jne    11c0 <netcat+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
define void @netcat(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @FD_ZERO(i32* %4)
%6 = load i32, i32* %2
%7 = call i32 @FD_SET(i32 %6, i32* %4)
%8 = load i32, i32* @STDIN_FILENO
%9 = call i32 @FD_SET(i32 %8, i32* %4)
br label %10
10: ; preds = %56, %28, %1
%11 = load i32, i32* %4
store i32 %11, i32* %3
%12 = load i32, i32* @STDIN_FILENO
%13 = load i32, i32* %2
%14 = icmp sgt i32 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i32, i32* @STDIN_FILENO
br label %19
17: ; preds = %10
%18 = load i32, i32* %2
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %16, %15 ], [ %18, %17 ]
%21 = add nsw i32 %20, 1
%22 = call i64 @select(i32 %21, i32* %3, i32* null, i32* null, i32* null)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
br label %10
29: ; preds = %24
%30 = load i32, i32* @EXIT_FAILURE
%31 = load i64, i64* @errno
%32 = call i32 @strerror(i64 %31)
%33 = call i32 @die(i32 %30, i32 %32)
br label %34
34: ; preds = %29, %19
%35 = load i32, i32* %2
%36 = call i64 @FD_ISSET(i32 %35, i32* %3)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %34
%39 = load i32, i32* %2
%40 = load i32, i32* @STDOUT_FILENO
%41 = call i32 @write_once(i32 %39, i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %38
br label %57
44: ; preds = %38
br label %45
45: ; preds = %44, %34
%46 = load i32, i32* @STDIN_FILENO
%47 = call i64 @FD_ISSET(i32 %46, i32* %3)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = load i32, i32* @STDIN_FILENO
%51 = load i32, i32* %2
%52 = call i32 @write_once(i32 %50, i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %55, label %54
54: ; preds = %49
br label %57
55: ; preds = %49
br label %56
56: ; preds = %55, %45
br label %10
57: ; preds = %54, %43
ret void
}

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