#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*/ MSG_OOB;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int tcp_listen(char *, char *, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int listenfd, connfd, n;
  char buf[100];
  fd_set rset, xset;
  if (argc == 2)
    listenfd = tcp_listen(NULL, argv[1], NULL);
  else if (argc == 3)
    listenfd = tcp_listen(argv[1], argv[2], NULL);
  else
    fprintf(stderr, "Usage: %s [<host>] <port>\n", argv[0]);
  if (listenfd == -1)
    return -1;
  connfd = accept(listenfd, NULL, NULL);
  FD_ZERO(&rset);
  FD_ZERO(&xset);
  while (1) {
    FD_SET(connfd, &rset);
    FD_SET(connfd, &xset);
    n = select(connfd + 1, &rset, NULL, &xset, NULL);
    printf("select() return n = %d\n", n);
    if (FD_ISSET(connfd, &xset)) {
      n = recv(connfd, buf, sizeof(buf) - 1, MSG_OOB);
      buf[n] = '\0';
      printf("read %d OOB byte: %s\n", n, buf);
    }
    if (FD_ISSET(connfd, &rset)) {
      if ((n = read(connfd, buf, sizeof(buf) - 1)) == 0) {
        printf("received EOF\n");
        exit(0);
      }
      buf[n] = '\0';
      printf("read %d bytes: %s\n", n, buf);
    }
  }
  close(connfd);
  close(listenfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11ef <main+0x3f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   10a0 <tcp_listen@plt>
mov    %eax,-0x14(%rbp)
jmp    123b <main+0x8b>
cmpl   $0x3,-0x8(%rbp)
jne    121a <main+0x6a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <tcp_listen@plt>
mov    %eax,-0x14(%rbp)
jmp    1236 <main+0x86>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdcf(%rip),%rsi
call   1070 <fprintf@plt>
jmp    123b <main+0x8b>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1253 <main+0xa3>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbp
ret
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
lea    -0x84(%rbp),%rdi
call   1080 <FD_ZERO@plt>
lea    -0x88(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x18(%rbp),%edi
lea    -0x84(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
lea    -0x88(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
lea    -0x84(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x88(%rbp),%rcx
mov    %r8,%rdx
call   1090 <select@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x88(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1324 <main+0x174>
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    0x2cf1(%rip),%rax
mov    (%rax),%ecx
mov    $0x63,%edx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
mov    -0x1c(%rbp),%esi
lea    -0x80(%rbp),%rdx
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x84(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     138d <main+0x1dd>
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x63,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    136f <main+0x1bf>
lea    0xce8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
mov    -0x1c(%rbp),%esi
lea    -0x80(%rbp),%rdx
lea    0xcd1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127d <main+0xcd>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
cmp    $0x2,%edi
je     11e7 <main+0x27>
cmp    $0x3,%edi
jne    12f9 <main+0x139>
mov    0x8(%rsi),%rdi
mov    $0x2,%eax
jmp    11ee <main+0x2e>
mov    $0x1,%eax
xor    %edi,%edi
mov    (%rsi,%rax,8),%rsi
xor    %edx,%edx
call   10b0 <tcp_listen@plt>
cmp    $0xffffffff,%eax
je     1311 <main+0x151>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,%ebx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1090 <FD_ZERO@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebx,%r13d
add    $0x1,%r13d
lea    0xde4(%rip),%r12
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
call   10e0 <FD_SET@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10e0 <FD_SET@plt>
mov    %r13d,%edi
mov    %r14,%rsi
xor    %edx,%edx
mov    %r15,%rcx
xor    %r8d,%r8d
call   10a0 <select@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     12b3 <main+0xf3>
mov    0x2d5f(%rip),%rax
mov    (%rax),%ecx
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x63,%edx
call   1030 <recv@plt>
movslq %eax,%rsi
movb   $0x0,0x10(%rsp,%rsi,1)
lea    0xd8a(%rip),%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1240 <main+0x80>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x63,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1325 <main+0x165>
movslq %eax,%rcx
movb   $0x0,0x10(%rsp,%rcx,1)
lea    0xd61(%rip),%rdi
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1240 <main+0x80>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xcf4(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd30(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s [<host>] <port>\0A\00"
@.str.1 = [24 x i8] c"select() return n = %d\0A\00"
@MSG_OOB = global i32 0
@.str.2 = [22 x i8] c"read %d OOB byte: %s\0A\00"
@.str.3 = [14 x i8] c"received EOF\0A\00"
@.str.4 = [19 x i8] c"read %d bytes: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [100 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @tcp_listen(i8* null, i8* %17, i32* null)
store i32 %18, i32* %6
br label %37
19: ; preds = %2
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 3
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @tcp_listen(i8* %25, i8* %28, i32* null)
store i32 %29, i32* %6
br label %36
30: ; preds = %19
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %30, %22
br label %37
37: ; preds = %36, %14
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %37
ret i32 -1
41: ; preds = %37
%42 = load i32, i32* %6
%43 = call i32 @accept(i32 %42, i32* null, i32* null)
store i32 %43, i32* %7
%44 = call i32 @FD_ZERO(i32* %10)
%45 = call i32 @FD_ZERO(i32* %11)
br label %46
46: ; preds = %41, %89
%47 = load i32, i32* %7
%48 = call i32 @FD_SET(i32 %47, i32* %10)
%49 = load i32, i32* %7
%50 = call i32 @FD_SET(i32 %49, i32* %11)
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
%53 = call i32 @select(i32 %52, i32* %10, i32* null, i32* %11, i32* null)
store i32 %53, i32* %8
%54 = load i32, i32* %8
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %7
%57 = call i64 @FD_ISSET(i32 %56, i32* %11)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %70
59: ; preds = %46
%60 = load i32, i32* %7
%61 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%62 = load i32, i32* @MSG_OOB
%63 = call i32 @recv(i32 %60, i8* %61, i32 99, i32 %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 %65
store i8 0, i8* %66
%67 = load i32, i32* %8
%68 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %67, i8* %68)
br label %70
70: ; preds = %59, %46
%71 = load i32, i32* %7
%72 = call i64 @FD_ISSET(i32 %71, i32* %10)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %89
74: ; preds = %70
%75 = load i32, i32* %7
%76 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%77 = call i32 @read(i32 %75, i8* %76, i32 99)
store i32 %77, i32* %8
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %74
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%81 = call i32 @exit(i32 0)
unreachable
82: ; preds = %74
%83 = load i32, i32* %8
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 %84
store i8 0, i8* %85
%86 = load i32, i32* %8
%87 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %86, i8* %87)
br label %89
89: ; preds = %82, %70
br label %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s [<host>] <port>\0A\00"
@.str.1 = [24 x i8] c"select() return n = %d\0A\00"
@MSG_OOB = global i32 0
@.str.2 = [22 x i8] c"read %d OOB byte: %s\0A\00"
@.str.4 = [19 x i8] c"read %d bytes: %s\0A\00"
@str = [13 x i8] c"received EOF\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
switch i32 %0, label %12 [
i32 2, label %16
i32 3, label %9
]
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
br label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %23
16: ; preds = %2, %9
%17 = phi i64 [ 2, %9 ], [ 1, %2 ]
%18 = phi i8* [ %11, %9 ], [ null, %2 ]
%19 = getelementptr inbounds i8*, i8** %1, i64 %17
%20 = load i8*, i8** %19
%21 = tail call i32 @tcp_listen(i8* %18, i8* %20, i32* null)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %12, %16
ret i32 -1
24: ; preds = %16
%25 = tail call i32 @accept(i32 %21, i32* null, i32* null)
%26 = call i32 @FD_ZERO(i32* %4)
%27 = call i32 @FD_ZERO(i32* %5)
%28 = add nsw i32 %25, 1
br label %29
29: ; preds = %55, %24
%30 = call i32 @FD_SET(i32 %25, i32* %4)
%31 = call i32 @FD_SET(i32 %25, i32* %5)
%32 = call i32 @select(i32 %28, i32* %4, i32* null, i32* %5, i32* null)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = call i64 @FD_ISSET(i32 %25, i32* %5)
%35 = icmp eq i64 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %29
%37 = load i32, i32* @MSG_OOB
%38 = call i32 @recv(i32 %25, i8* %6, i32 99, i32 %37)
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %39
store i8 0, i8* %40
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %38, i8* %6)
br label %42
42: ; preds = %36, %29
%43 = call i64 @FD_ISSET(i32 %25, i32* %4)
%44 = icmp eq i64 %43, 0
br i1 %44, label %55, label %45
45: ; preds = %42
%46 = call i32 @read(i32 %25, i8* %6, i32 99)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %45
%52 = sext i32 %46 to i64
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %52
store i8 0, i8* %53
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %46, i8* %6)
br label %55
55: ; preds = %51, %42
br label %29
}