#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ incoming_socket_addr;
typedef int /*<<< orphan*/ client_socket_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int const, struct sockaddr *, int *);
int bind(int const, struct sockaddr *, int);
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ listen(int const, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  struct sockaddr_in incoming_socket_addr = {0};
  incoming_socket_addr.sin_family = AF_INET;
  incoming_socket_addr.sin_port = htons(5001);
  if (inet_aton("127.0.0.1", &incoming_socket_addr.sin_addr) == 0) {
    perror("inet_aton");
    exit(1);
  }
  const int incoming_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  const int rc =
      bind(incoming_socket_fd, (struct sockaddr *)&incoming_socket_addr,
           sizeof(incoming_socket_addr));
  if (rc == -1) {
    perror("bind");
    close(incoming_socket_fd);
    exit(1);
  }
  if (listen(incoming_socket_fd, 1024) != 0) {
    perror("listen");
    exit(1);
  }
  char buf[1024];
  while (1) {
    struct sockaddr_in client_socket_addr;
    socklen_t addrlen = sizeof(client_socket_addr);
    int acceptrc = accept(incoming_socket_fd,
                          (struct sockaddr *)&client_socket_addr, &addrlen);
    if (acceptrc == -1) {
      perror("accept");
      close(incoming_socket_fd);
      exit(1);
    }
    printf("Connected client: %s\n", inet_ntoa(client_socket_addr.sin_addr));
    read(acceptrc, buf, sizeof(buf));
    printf("Data: %s\n", buf);
    close(acceptrc);
  }
  close(incoming_socket_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1389,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xdea(%rip),%rdi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    123b <main+0x6b>
lea    0xdde(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1293 <main+0xc3>
lea    0xd98(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    $0x400,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
je     12c0 <main+0xf0>
lea    0xd68(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12c5 <main+0xf5>
movl   $0x10,-0x434(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x430(%rbp),%rsi
lea    -0x434(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x438(%rbp)
cmpl   $0xffffffff,-0x438(%rbp)
jne    1316 <main+0x146>
lea    0xd21(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x430(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xcfc(%rip),%rdi
call   1050 <printf@plt>
mov    -0x438(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
lea    -0x420(%rbp),%rsi
lea    0xce8(%rip),%rdi
call   1050 <printf@plt>
mov    -0x438(%rbp),%edi
call   1070 <close@plt>
jmp    12c5 <main+0xf5>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e08(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x1389,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xe05(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <inet_aton@plt>
test   %rax,%rax
jne    1220 <main+0x60>
lea    0xdf9(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    (%rbx),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1256 <main+0x96>
lea    0xdc3(%rip),%rdi
jmp    130f <main+0x14f>
mov    %ebp,%edi
mov    $0x400,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    1325 <main+0x165>
movl   $0x10,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     1308 <main+0x148>
mov    %eax,%r14d
lea    0x30(%rsp),%rbx
lea    0xda5(%rip),%r15
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x20(%rsp),%edi
call   1030 <inet_ntoa@plt>
lea    0xd67(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
movl   $0x10,0xc(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10c0 <accept@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    12b0 <main+0xf0>
lea    0xd11(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xced(%rip),%rdi
jmp    1211 <main+0x51>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [10 x i8] c"inet_aton\00"
@SOCK_STREAM = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [22 x i8] c"Connected client: %s\0A\00"
@.str.6 = [10 x i8] c"Data: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = bitcast %struct.sockaddr_in* %2 to i8*
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @htons(i32 5001)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%15 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 16)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
%35 = load i32, i32* %3
%36 = call i64 @listen(i32 %35, i32 1024)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
br label %42
42: ; preds = %41, %53
store i32 16, i32* %7
%43 = load i32, i32* %3
%44 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %7)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* %3
%51 = call i32 @close(i32 %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %42
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i8* @inet_ntoa(i32 %55)
%57 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %56)
%58 = load i32, i32* %8
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%60 = call i32 @read(i32 %58, i8* %59, i32 1024)
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%62 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %61)
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
br label %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [10 x i8] c"inet_aton\00"
@SOCK_STREAM = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [22 x i8] c"Connected client: %s\0A\00"
@.str.6 = [10 x i8] c"Data: %s\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1024 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 5001)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%11 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i32 @bind(i32 %19, %struct.sockaddr* %20, i32 16)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @close(i32 %19)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %16
%28 = call i64 @listen(i32 %19, i32 1024)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%35 = bitcast %struct.sockaddr_in* %3 to i8*
%36 = bitcast i32* %4 to i8*
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
store i32 16, i32* %4
%38 = call i32 @accept(i32 %19, %struct.sockaddr* %37, i32* %4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %42, label %40
40: ; preds = %33
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %46
42: ; preds = %46, %33
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @close(i32 %19)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %40, %46
%47 = phi i32 [ %38, %40 ], [ %54, %46 ]
%48 = load i32, i32* %41
%49 = call i8* @inet_ntoa(i32 %48)
%50 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = call i32 @read(i32 %47, i8* %34, i32 1024)
%52 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %34)
%53 = call i32 @close(i32 %47)
store i32 16, i32* %4
%54 = call i32 @accept(i32 %19, %struct.sockaddr* %37, i32* %4)
%55 = icmp eq i32 %54, -1
br i1 %55, label %42, label %46
}