#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 /*<<< orphan*/ self;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
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 sockfd;
  struct sockaddr_in self;
  char buffer[1024];
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Socket");
    exit(errno);
  }
  bzero(&self, sizeof(self));
  self.sin_family = AF_INET;
  self.sin_port = htons(9999);
  self.sin_addr.s_addr = INADDR_ANY;
  if (bind(sockfd, (struct sockaddr *)&self, sizeof(self)) != 0) {
    perror("socket--bind");
    exit(errno);
  }
  if (listen(sockfd, 20) != 0) {
    perror("socket--listen");
    exit(errno);
  }
  while (1) {
    int clientfd;
    struct sockaddr_in client_addr;
    int addrlen = sizeof(client_addr);
    clientfd = accept(sockfd, (struct sockaddr *)&client_addr, &addrlen);
    printf("%s:%d connected\n", inet_ntoa(client_addr.sin_addr),
           ntohs(client_addr.sin_port));
    send(clientfd, buffer, recv(clientfd, buffer, 1024, 0), 0);
    close(clientfd);
  }
  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)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1238 <main+0x58>
lea    0xddb(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
je     12a0 <main+0xc0>
lea    0xd7a(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x14,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     12d1 <main+0xf1>
lea    0xd56(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    12d6 <main+0xf6>
movl   $0xc,-0x434(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
lea    -0x434(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x424(%rbp)
mov    -0x42c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x450(%rbp)
mov    -0x430(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
lea    0xcfa(%rip),%rdi
call   1070 <printf@plt>
mov    -0x424(%rbp),%eax
mov    %eax,-0x444(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x424(%rbp),%edi
lea    -0x420(%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    -0x424(%rbp),%edi
call   1080 <close@plt>
jmp    12d6 <main+0xf6>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    0x2df0(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     12f4 <main+0x114>
mov    %eax,%r13d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jne    12fd <main+0x11d>
mov    %r13d,%edi
mov    $0x14,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    1306 <main+0x126>
lea    0xc(%rsp),%r15
lea    0xd9e(%rip),%r12
lea    0x30(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
movl   $0xc,0xc(%rsp)
mov    %r13d,%edi
lea    0x20(%rsp),%rsi
mov    %r15,%rdx
call   10e0 <accept@plt>
mov    %eax,%ebp
mov    0x24(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x20(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1290 <main+0xb0>
lea    0xd05(%rip),%rdi
jmp    130d <main+0x12d>
lea    0xd03(%rip),%rdi
jmp    130d <main+0x12d>
lea    0xd07(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2caf(%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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"Socket\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"socket--bind\00"
@.str.2 = [15 x i8] c"socket--listen\00"
@.str.3 = [17 x i8] c"%s:%d connected\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 9999)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @errno
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %20
%38 = load i32, i32* %6
%39 = call i64 @listen(i32 %38, i32 20)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @errno
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %37
br label %46
46: ; preds = %45, %46
store i32 12, i32* %11
%47 = load i32, i32* %6
%48 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%49 = call i32 @accept(i32 %47, %struct.sockaddr* %48, i32* %11)
store i32 %49, i32* %9
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i8* @inet_ntoa(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @ntohs(i32 %55)
%57 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %53, i32 %56)
%58 = load i32, i32* %9
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%60 = load i32, i32* %9
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%62 = call i32 @recv(i32 %60, i8* %61, i32 1024, i32 0)
%63 = call i32 @send(i32 %58, i8* %59, i32 %62, i32 0)
%64 = load i32, i32* %9
%65 = call i32 @close(i32 %64)
br label %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"Socket\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"socket--bind\00"
@.str.2 = [15 x i8] c"socket--listen\00"
@.str.3 = [17 x i8] c"%s:%d connected\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @errno
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%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 9999)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %11, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @errno
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %17
%33 = call i64 @listen(i32 %11, i32 20)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = bitcast %struct.sockaddr_in* %5 to i8*
%37 = bitcast i32* %6 to i8*
%38 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
br label %45
41: ; preds = %32
%42 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @errno
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %35, %45
store i32 12, i32* %6
%46 = call i32 @accept(i32 %11, %struct.sockaddr* %38, i32* %6)
%47 = load i32, i32* %39
%48 = call i8* @inet_ntoa(i32 %47)
%49 = load i32, i32* %40
%50 = call i32 @ntohs(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %48, i32 %50)
%52 = call i32 @recv(i32 %46, i8* %8, i32 1024, i32 0)
%53 = call i32 @send(i32 %46, i8* %8, i32 %52, i32 0)
%54 = call i32 @close(i32 %46)
br label %45
}