#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 */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
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;
  int just_readoob = 0;
  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);
    if (just_readoob == 0)
      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);
      just_readoob = 1;
      FD_CLR(connfd, &xset);
    }
    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);
      just_readoob = 0;
    }
  }
  close(connfd);
  close(listenfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1206 <main+0x46>
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    1252 <main+0x92>
cmpl   $0x3,-0x8(%rbp)
jne    1231 <main+0x71>
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    124d <main+0x8d>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb8(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1252 <main+0x92>
cmpl   $0xffffffff,-0x14(%rbp)
jne    126a <main+0xaa>
mov    $0xffffffff,%eax
add    $0xa0,%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    -0x94(%rbp),%rdi
call   1080 <FD_ZERO@plt>
lea    -0x98(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x18(%rbp),%edi
lea    -0x94(%rbp),%rsi
call   10e0 <FD_SET@plt>
cmpl   $0x0,-0x20(%rbp)
jne    12bc <main+0xfc>
mov    -0x18(%rbp),%edi
lea    -0x98(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
lea    -0x94(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x98(%rbp),%rcx
mov    %r8,%rdx
call   1090 <select@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xd31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x98(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1364 <main+0x1a4>
mov    -0x18(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    0x2ccd(%rip),%rax
mov    (%rax),%ecx
mov    $0x63,%edx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
mov    -0x1c(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xcec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x98(%rbp),%rsi
call   10c0 <FD_CLR@plt>
mov    -0x18(%rbp),%edi
lea    -0x94(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13dd <main+0x21d>
mov    -0x18(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x63,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    13b2 <main+0x1f2>
lea    0xca5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
mov    -0x1c(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xc88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x20(%rbp)
jmp    1294 <main+0xd4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
cmp    $0x2,%edi
je     11f7 <main+0x27>
cmp    $0x3,%edi
jne    1325 <main+0x155>
mov    0x8(%rsi),%rdi
mov    $0x2,%eax
jmp    11fe <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     133d <main+0x16d>
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    0x10(%rsp),%rbp
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
call   10f0 <FD_SET@plt>
test   %r12d,%r12d
jne    1269 <main+0x99>
mov    %ebx,%edi
mov    %r15,%rsi
call   10f0 <FD_SET@plt>
mov    %r13d,%edi
mov    %r14,%rsi
xor    %edx,%edx
mov    %r15,%rcx
xor    %r8d,%r8d
call   10a0 <select@plt>
lea    0xd98(%rip),%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     12dc <main+0x10c>
mov    0x2d46(%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    0xd71(%rip),%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10d0 <FD_CLR@plt>
mov    $0x1,%r12d
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1250 <main+0x80>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x63,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1351 <main+0x181>
movslq %eax,%rcx
movb   $0x0,0x10(%rsp,%rcx,1)
xor    %r12d,%r12d
lea    0xd35(%rip),%rdi
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1250 <main+0x80>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xcc8(%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    0xd04(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10e0 <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 i32
%10 = alloca [100 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @tcp_listen(i8* null, i8* %18, i32* null)
store i32 %19, i32* %6
br label %38
20: ; preds = %2
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 3
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @tcp_listen(i8* %26, i8* %29, i32* null)
store i32 %30, i32* %6
br label %37
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %31, %23
br label %38
38: ; preds = %37, %15
%39 = load i32, i32* %6
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %38
ret i32 -1
42: ; preds = %38
%43 = load i32, i32* %6
%44 = call i32 @accept(i32 %43, i32* null, i32* null)
store i32 %44, i32* %7
%45 = call i32 @FD_ZERO(i32* %11)
%46 = call i32 @FD_ZERO(i32* %12)
br label %47
47: ; preds = %42, %96
%48 = load i32, i32* %7
%49 = call i32 @FD_SET(i32 %48, i32* %11)
%50 = load i32, i32* %9
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i32, i32* %7
%54 = call i32 @FD_SET(i32 %53, i32* %12)
br label %55
55: ; preds = %52, %47
%56 = load i32, i32* %7
%57 = add nsw i32 %56, 1
%58 = call i32 @select(i32 %57, i32* %11, i32* null, i32* %12, i32* null)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %7
%62 = call i64 @FD_ISSET(i32 %61, i32* %12)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %77
64: ; preds = %55
%65 = load i32, i32* %7
%66 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%67 = load i32, i32* @MSG_OOB
%68 = call i32 @recv(i32 %65, i8* %66, i32 99, i32 %67)
store i32 %68, i32* %8
%69 = load i32, i32* %8
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 %70
store i8 0, i8* %71
%72 = load i32, i32* %8
%73 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %72, i8* %73)
store i32 1, i32* %9
%75 = load i32, i32* %7
%76 = call i32 @FD_CLR(i32 %75, i32* %12)
br label %77
77: ; preds = %64, %55
%78 = load i32, i32* %7
%79 = call i64 @FD_ISSET(i32 %78, i32* %11)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %96
81: ; preds = %77
%82 = load i32, i32* %7
%83 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%84 = call i32 @read(i32 %82, i8* %83, i32 99)
store i32 %84, i32* %8
%85 = icmp eq i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %81
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%88 = call i32 @exit(i32 0)
unreachable
89: ; preds = %81
%90 = load i32, i32* %8
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 %91
store i8 0, i8* %92
%93 = load i32, i32* %8
%94 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %93, i8* %94)
store i32 0, i32* %9
br label %96
96: ; preds = %89, %77
br label %47
}

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 = %61, %24
%30 = phi i32 [ 0, %24 ], [ %62, %61 ]
%31 = call i32 @FD_SET(i32 %25, i32* %4)
%32 = icmp eq i32 %30, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @FD_SET(i32 %25, i32* %5)
br label %35
35: ; preds = %33, %29
%36 = call i32 @select(i32 %28, i32* %4, i32* null, i32* %5, i32* null)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = call i64 @FD_ISSET(i32 %25, i32* %5)
%39 = icmp eq i64 %38, 0
br i1 %39, label %47, label %40
40: ; preds = %35
%41 = load i32, i32* @MSG_OOB
%42 = call i32 @recv(i32 %25, i8* %6, i32 99, i32 %41)
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %43
store i8 0, i8* %44
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %42, i8* %6)
%46 = call i32 @FD_CLR(i32 %25, i32* %5)
br label %47
47: ; preds = %40, %35
%48 = phi i32 [ 1, %40 ], [ %30, %35 ]
%49 = call i64 @FD_ISSET(i32 %25, i32* %4)
%50 = icmp eq i64 %49, 0
br i1 %50, label %61, label %51
51: ; preds = %47
%52 = call i32 @read(i32 %25, i8* %6, i32 99)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %51
%58 = sext i32 %52 to i64
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %58
store i8 0, i8* %59
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %52, i8* %6)
br label %61
61: ; preds = %57, %47
%62 = phi i32 [ 0, %57 ], [ %48, %47 ]
br label %29
}