#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ abort();
int accept(int, int /*<<< orphan*/, int /*<<< orphan*/);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int count, char *strings[]) {
  int sd;
  int port = 9999;
  struct sockaddr_in addr;
  if (count != 2)
    printf("usage: %s <portnum>\n...Using default port (%d).\n", strings[1],
           port);
  else
    port = atoi(strings[1]);
  sd = socket(PF_INET, SOCK_STREAM, 0);
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    perror("bind");
    abort();
  };
  if (listen(sd, 20) != 0) {
    perror("listen");
    abort();
  };
  while (1) {
    char buffer[1024];
    int client = accept(sd, 0, 0);
    printf("connected\n");
    send(client, buffer, recv(client, buffer, sizeof(buffer), 0), 0);
    close(client);
  }
  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)
movl   $0x270f,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1218 <main+0x48>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x18(%rbp),%edx
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1228 <main+0x58>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     12a3 <main+0xd3>
lea    0xd9a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x14(%rbp),%edi
mov    $0x14,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     12cd <main+0xfd>
lea    0xd75(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    12d2 <main+0x102>
mov    -0x14(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   10d0 <accept@plt>
mov    %eax,-0x434(%rbp)
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x444(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x434(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x434(%rbp),%edi
call   1080 <close@plt>
jmp    12d2 <main+0x102>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    0x8(%rsi),%rsi
cmp    $0x2,%edi
jne    1203 <main+0x23>
mov    %rsi,%rdi
call   10f0 <atoi@plt>
mov    %eax,%r14d
jmp    121c <main+0x3c>
lea    0xdf6(%rip),%rdi
mov    $0x270f,%r14d
mov    $0x270f,%edx
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r15d
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jne    12de <main+0xfe>
mov    %r15d,%edi
mov    $0x14,%esi
call   10a0 <listen@plt>
test   %rax,%rax
jne    12e7 <main+0x107>
lea    0xda2(%rip),%r14
lea    0x10(%rsp),%rbp
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <puts@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %ebx,%edi
call   1090 <close@plt>
jmp    12a0 <main+0xc0>
lea    0xd4c(%rip),%rdi
jmp    12ee <main+0x10e>
lea    0xd48(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [49 x i8] c"usage: %s <portnum>\0A...Using default port (%d).\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [11 x i8] c"connected\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 [1024 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 9999, i32* %7
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* %7
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17)
br label %24
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %7
br label %24
24: ; preds = %19, %13
%25 = load i32, i32* @PF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %6
%28 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* %7
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @INADDR_ANY
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %24
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 (...) @abort()
unreachable
44: ; preds = %24
%45 = load i32, i32* %6
%46 = call i64 @listen(i32 %45, i32 20)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%50 = call i32 (...) @abort()
unreachable
51: ; preds = %44
br label %52
52: ; preds = %51, %52
%53 = load i32, i32* %6
%54 = call i32 @accept(i32 %53, i32 0, i32 0)
store i32 %54, i32* %10
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%56 = load i32, i32* %10
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%58 = load i32, i32* %10
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%60 = call i32 @recv(i32 %58, i8* %59, i32 1024, i32 0)
%61 = call i32 @send(i32 %56, i8* %57, i32 %60, i32 0)
%62 = load i32, i32* %10
%63 = call i32 @close(i32 %62)
br label %52
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [49 x i8] c"usage: %s <portnum>\0A...Using default port (%d).\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@str = [10 x i8] c"connected\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = icmp eq i32 %0, 2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br i1 %6, label %11, label %9
9: ; preds = %2
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %8, i32 9999)
br label %13
11: ; preds = %2
%12 = tail call i32 @atoi(i8* %8)
br label %13
13: ; preds = %11, %9
%14 = phi i32 [ 9999, %9 ], [ %12, %11 ]
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 %14)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %17, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %13
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %13
%32 = call i64 @listen(i32 %17, i32 20)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
br label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 (...) @abort()
unreachable
39: ; preds = %34, %39
%40 = call i32 @accept(i32 %17, i32 0, i32 0)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%42 = call i32 @recv(i32 %40, i8* %35, i32 1024, i32 0)
%43 = call i32 @send(i32 %40, i8* %35, i32 %42, i32 0)
%44 = call i32 @close(i32 %40)
br label %39
}