#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*/ on;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_OOBINLINE;
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 /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ sockatmark(int);
int /*<<< orphan*/ stderr;
int tcp_listen(char *, char *, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int listenfd, connfd, n, on = 1;
  char buf[100];
  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;
  setsockopt(listenfd, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on));
  connfd = accept(listenfd, NULL, NULL);
  sleep(5);
  while (1) {
    if (sockatmark(connfd))
      printf("at OOB mark\n");
    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)
movl   $0x1,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11d6 <main+0x46>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1080 <tcp_listen@plt>
mov    %eax,-0x14(%rbp)
jmp    1222 <main+0x92>
cmpl   $0x3,-0x8(%rbp)
jne    1201 <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   1080 <tcp_listen@plt>
mov    %eax,-0x14(%rbp)
jmp    121d <main+0x8d>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde8(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1222 <main+0x92>
cmpl   $0xffffffff,-0x14(%rbp)
jne    123a <main+0xaa>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbp
ret
mov    -0x14(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1090 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    -0x18(%rbp),%edi
call   1030 <sockatmark@plt>
cmp    $0x0,%rax
je     129a <main+0x10a>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x63,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    12cf <main+0x13f>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
mov    -0x1c(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127a <main+0xea>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x78,%rsp
movl   $0x1,0xc(%rsp)
cmp    $0x2,%edi
je     11cc <main+0x2c>
cmp    $0x3,%edi
jne    1280 <main+0xe0>
mov    0x8(%rsi),%rdi
mov    $0x2,%eax
jmp    11d3 <main+0x33>
mov    $0x1,%eax
xor    %edi,%edi
mov    (%rsi,%rax,8),%rsi
xor    %edx,%edx
call   1090 <tcp_listen@plt>
cmp    $0xffffffff,%eax
je     1298 <main+0xf8>
mov    %eax,%ebx
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <accept@plt>
mov    %eax,%ebx
mov    $0x5,%edi
call   10c0 <sleep@plt>
lea    0x10(%rsp),%r14
lea    0xdeb(%rip),%r15
lea    0xe04(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1030 <sockatmark@plt>
test   %rax,%rax
je     1254 <main+0xb4>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x63,%edx
call   1070 <read@plt>
test   %eax,%eax
je     12a9 <main+0x109>
movslq %eax,%rcx
movb   $0x0,0x10(%rsp,%rcx,1)
mov    %r15,%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1240 <main+0xa0>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd6d(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd7e(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s [<host>] <port>\0A\00"
@SOL_SOCKET = global i32 0
@SO_OOBINLINE = global i32 0
@.str.1 = [13 x i8] c"at OOB mark\0A\00"
@.str.2 = [14 x i8] c"received EOF\0A\00"
@.str.3 = [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]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %9
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @tcp_listen(i8* null, i8* %16, i32* null)
store i32 %17, i32* %6
br label %36
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 3
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @tcp_listen(i8* %24, i8* %27, i32* null)
store i32 %28, i32* %6
br label %35
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %29, %21
br label %36
36: ; preds = %35, %13
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %36
ret i32 -1
40: ; preds = %36
%41 = load i32, i32* %6
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_OOBINLINE
%44 = call i32 @setsockopt(i32 %41, i32 %42, i32 %43, i32* %9, i32 4)
%45 = load i32, i32* %6
%46 = call i32 @accept(i32 %45, i32* null, i32* null)
store i32 %46, i32* %7
%47 = call i32 @sleep(i32 5)
br label %48
48: ; preds = %40, %62
%49 = load i32, i32* %7
%50 = call i64 @sockatmark(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = load i32, i32* %7
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 99)
store i32 %57, i32* %8
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %54
%63 = load i32, i32* %8
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 %64
store i8 0, i8* %65
%66 = load i32, i32* %8
%67 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %66, i8* %67)
br label %48
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage: %s [<host>] <port>\0A\00"
@SOL_SOCKET = global i32 0
@SO_OOBINLINE = global i32 0
@.str.3 = [19 x i8] c"read %d bytes: %s\0A\00"
@str = [13 x i8] c"received EOF\00"
@str.4 = [12 x i8] c"at OOB mark\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
switch i32 %0, label %10 [
i32 2, label %14
i32 3, label %7
]
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
br label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %21
14: ; preds = %2, %7
%15 = phi i64 [ 2, %7 ], [ 1, %2 ]
%16 = phi i8* [ %9, %7 ], [ null, %2 ]
%17 = getelementptr inbounds i8*, i8** %1, i64 %15
%18 = load i8*, i8** %17
%19 = tail call i32 @tcp_listen(i8* %16, i8* %18, i32* null)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %10, %14
ret i32 -1
22: ; preds = %14
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_OOBINLINE
%25 = call i32 @setsockopt(i32 %19, i32 %23, i32 %24, i32* %3, i32 4)
%26 = call i32 @accept(i32 %19, i32* null, i32* null)
%27 = call i32 @sleep(i32 5)
br label %28
28: ; preds = %39, %22
%29 = call i64 @sockatmark(i32 %26)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = call i32 @read(i32 %26, i8* %6, i32 99)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %33
%40 = sext i32 %34 to i64
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %40
store i8 0, i8* %41
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %6)
br label %28
}