#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int Accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ Close(int);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int Open_listenfd(int);
int STDIN_FILENO;
int /*<<< orphan*/ Select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int atoi(char *);
int /*<<< orphan*/ command();
int /*<<< orphan*/ echo(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int listenfd, connfd, port;
  socklen_t clientlen = sizeof(struct sockaddr_in);
  struct sockaddr_in clientaddr;
  fd_set read_set, ready_set;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <port>\n", argv[0]);
    exit(0);
  }
  port = atoi(argv[1]);
  listenfd = Open_listenfd(port);
  FD_ZERO(&read_set);
  FD_SET(STDIN_FILENO, &read_set);
  FD_SET(listenfd, &read_set);
  while (1) {
    ready_set = read_set;
    Select(listenfd + 1, &ready_set, NULL, NULL, NULL);
    if (FD_ISSET(STDIN_FILENO, &ready_set))
      command();
    if (FD_ISSET(listenfd, &ready_set)) {
      connfd = Accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen);
      echo(connfd);
      Close(connfd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120a <main+0x4a>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe02(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <Open_listenfd@plt>
mov    %eax,-0x14(%rbp)
lea    -0x2c(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10a0 <Select@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     128f <main+0xcf>
mov    $0x0,%al
call   1060 <command@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12c8 <main+0x108>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <Accept@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <echo@plt>
mov    -0x18(%rbp),%edi
call   10b0 <Close@plt>
jmp    124c <main+0x8c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x4,0xc(%rsp)
cmp    $0x2,%edi
jne    1293 <main+0xd3>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1070 <Open_listenfd@plt>
mov    %eax,%r13d
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <FD_ZERO@plt>
mov    0x2dd2(%rip),%r15
mov    (%r15),%edi
mov    %rbp,%rsi
call   10e0 <FD_SET@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
call   10e0 <FD_SET@plt>
mov    %r13d,%r14d
add    $0x1,%r14d
lea    0x4(%rsp),%rbp
lea    0xc(%rsp),%r12
nopl   (%rax)
mov    0x8(%rsp),%eax
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <Select@plt>
mov    (%r15),%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1261 <main+0xa1>
xor    %eax,%eax
call   1060 <command@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1230 <main+0x70>
mov    %r13d,%edi
lea    0x10(%rsp),%rsi
mov    %r12,%rdx
call   1050 <Accept@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <echo@plt>
mov    %ebx,%edi
call   10b0 <Close@plt>
jmp    1230 <main+0x70>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd5a(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s <port>\0A\00"
@STDIN_FILENO = global i32 0
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 %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4, i32* %9
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = call i32 @Open_listenfd(i32 %27)
store i32 %28, i32* %6
%29 = call i32 @FD_ZERO(i32* %11)
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @FD_SET(i32 %30, i32* %11)
%32 = load i32, i32* %6
%33 = call i32 @FD_SET(i32 %32, i32* %11)
br label %34
34: ; preds = %22, %56
%35 = load i32, i32* %11
store i32 %35, i32* %12
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
%38 = call i32 @Select(i32 %37, i32* %12, i32* null, i32* null, i32* null)
%39 = load i32, i32* @STDIN_FILENO
%40 = call i64 @FD_ISSET(i32 %39, i32* %12)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %34
%43 = call i32 (...) @command()
br label %44
44: ; preds = %42, %34
%45 = load i32, i32* %6
%46 = call i64 @FD_ISSET(i32 %45, i32* %12)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %44
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%51 = call i32 @Accept(i32 %49, %struct.sockaddr* %50, i32* %9)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = call i32 @echo(i32 %52)
%54 = load i32, i32* %7
%55 = call i32 @Close(i32 %54)
br label %56
56: ; preds = %48, %44
br label %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s <port>\0A\00"
@STDIN_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = icmp eq i32 %0, 2
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = tail call i32 @Open_listenfd(i32 %20)
%22 = call i32 @FD_ZERO(i32* %5)
%23 = load i32, i32* @STDIN_FILENO
%24 = call i32 @FD_SET(i32 %23, i32* %5)
%25 = call i32 @FD_SET(i32 %21, i32* %5)
%26 = add nsw i32 %21, 1
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %28
28: ; preds = %43, %17
%29 = load i32, i32* %5
store i32 %29, i32* %6
%30 = call i32 @Select(i32 %26, i32* %6, i32* null, i32* null, i32* null)
%31 = load i32, i32* @STDIN_FILENO
%32 = call i64 @FD_ISSET(i32 %31, i32* %6)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %28
%35 = call i32 (...) @command()
br label %36
36: ; preds = %34, %28
%37 = call i64 @FD_ISSET(i32 %21, i32* %6)
%38 = icmp eq i64 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %36
%40 = call i32 @Accept(i32 %21, %struct.sockaddr* %27, i32* %3)
%41 = call i32 @echo(i32 %40)
%42 = call i32 @Close(i32 %40)
br label %43
43: ; preds = %39, %36
br label %28
}