#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd, clifd;
  struct sockaddr_in serv_addr, cli_addr;
  socklen_t cli_len;
  char msg[1024];
  int str_len;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket() failed");
    exit(EXIT_FAILURE);
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(atoi(argv[1]));
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    fprintf(stderr, "bind() failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (listen(sockfd, 5) == -1) {
    fprintf(stderr, "listen() failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  cli_len = sizeof(cli_addr);
  while (1) {
    clifd = accept(sockfd, (struct sockaddr *)&cli_addr, &cli_len);
    if (clifd == -1) {
      fprintf(stderr, "accept() failed: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    while ((str_len = read(clifd, msg, sizeof(msg))) > 0)
      write(clifd, msg, str_len);
    close(clifd);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     123d <main+0x4d>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd6(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    127c <main+0x8c>
lea    0xda9(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    131a <main+0x12a>
mov    0x2cef(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x448(%rbp)
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x448(%rbp),%edi
mov    %rax,%rdx
lea    0xd1b(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1370 <main+0x180>
mov    0x2c99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44c(%rbp)
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x44c(%rbp),%edi
mov    %rax,%rdx
lea    0xcd8(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2c4f(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    13d4 <main+0x1e4>
mov    0x2c35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x450(%rbp)
mov    0x2c16(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x450(%rbp),%edi
mov    %rax,%rdx
lea    0xc89(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2beb(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    13d9 <main+0x1e9>
mov    -0x18(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x444(%rbp)
cmp    $0x0,%eax
jle    1416 <main+0x226>
mov    -0x18(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x444(%rbp),%edx
call   1030 <write@plt>
jmp    13d9 <main+0x1e9>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    1377 <main+0x187>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x430,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1237 <main+0x47>
mov    0x2dda(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
jne    124f <main+0x5f>
lea    0xde5(%rip),%rdi
call   10c0 <perror@plt>
jmp    13a3 <main+0x1b3>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdb6(%rip),%rsi
jmp    139e <main+0x1ae>
mov    %eax,%r12d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rbx),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12cb <main+0xdb>
mov    0x2d21(%rip),%rax
mov    (%rax),%ebx
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd5c(%rip),%rsi
jmp    1399 <main+0x1a9>
mov    %r12d,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1300 <main+0x110>
mov    0x2cec(%rip),%rax
mov    (%rax),%ebx
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd3a(%rip),%rsi
jmp    1399 <main+0x1a9>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r12d,%edi
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     137b <main+0x18b>
mov    %eax,%ebp
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
jmp    134c <main+0x15c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1340 <main+0x150>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    134c <main+0x15c>
mov    0x2c4e(%rip),%rax
mov    (%rax),%ebx
mov    0x2c35(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xcb1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    0x2c0e(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@.str.2 = [19 x i8] c"bind() failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"listen() failed: %s\0A\00"
@.str.4 = [21 x i8] c"accept() failed: %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 %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %23
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 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
%33 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @atoi(i8* %42)
%44 = call i32 @htons(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%48 = call i32 @bind(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %57
50: ; preds = %32
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %32
%58 = load i32, i32* %6
%59 = call i32 @listen(i32 %58, i32 5)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %68
61: ; preds = %57
%62 = load i32, i32* @stderr
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %57
store i32 12, i32* %10
br label %69
69: ; preds = %68, %93
%70 = load i32, i32* %6
%71 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%72 = call i32 @accept(i32 %70, %struct.sockaddr* %71, i32* %10)
store i32 %72, i32* %7
%73 = load i32, i32* %7
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %82
75: ; preds = %69
%76 = load i32, i32* @stderr
%77 = load i32, i32* @errno
%78 = call i8* @strerror(i32 %77)
%79 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %78)
%80 = load i32, i32* @EXIT_FAILURE
%81 = call i32 @exit(i32 %80)
unreachable
82: ; preds = %69
br label %83
83: ; preds = %88, %82
%84 = load i32, i32* %7
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%86 = call i32 @read(i32 %84, i8* %85, i32 1024)
store i32 %86, i32* %12
%87 = icmp sgt i32 %86, 0
br i1 %87, label %88, label %93
88: ; preds = %83
%89 = load i32, i32* %7
%90 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%91 = load i32, i32* %12
%92 = call i32 @write(i32 %89, i8* %90, i32 %91)
br label %83
93: ; preds = %83
%94 = load i32, i32* %7
%95 = call i32 @close(i32 %94)
br label %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@.str.2 = [19 x i8] c"bind() failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"listen() failed: %s\0A\00"
@.str.4 = [21 x i8] c"accept() failed: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = icmp eq i32 %0, 2
br i1 %11, label %18, 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 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %18
%28 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %32, i32* %33
%34 = getelementptr inbounds i8*, i8** %1, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @bind(i32 %21, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %49
42: ; preds = %27
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %27
%50 = call i32 @listen(i32 %21, i32 5)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %59
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %49
store i32 12, i32* %5
%60 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%61 = call i32 @accept(i32 %21, %struct.sockaddr* %60, i32* %5)
%62 = icmp eq i32 %61, -1
br i1 %62, label %67, label %63
63: ; preds = %59, %79
%64 = phi i32 [ %81, %79 ], [ %61, %59 ]
%65 = call i32 @read(i32 %64, i8* %10, i32 1024)
%66 = icmp sgt i32 %65, 0
br i1 %66, label %74, label %79
67: ; preds = %79, %59
%68 = load i32, i32* @stderr
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %70)
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %63, %74
%75 = phi i32 [ %77, %74 ], [ %65, %63 ]
%76 = call i32 @write(i32 %64, i8* %10, i32 %75)
%77 = call i32 @read(i32 %64, i8* %10, i32 1024)
%78 = icmp sgt i32 %77, 0
br i1 %78, label %74, label %79
79: ; preds = %74, %63
%80 = call i32 @close(i32 %64)
%81 = call i32 @accept(i32 %21, %struct.sockaddr* %60, i32* %5)
%82 = icmp eq i32 %81, -1
br i1 %82, label %67, label %63
}