#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 */
typedef int uint32_t;
struct sockaddr_storage {
  int dummy;
};
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*/ msg;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int const);
int send(int, char const *, int const, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void server() {
  int server_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (server_socket_fd == 0) {
    fprintf(stderr, "Failed to create server socket");
    exit(-1);
  }
  struct sockaddr_in addr;
  addr.sin_family = AF_UNSPEC;
  addr.sin_port = htons(8080);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(server_socket_fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    fprintf(stderr, "Failed to bind socket");
    exit(-1);
  }
  const uint32_t BACKLOG = 3;
  if (listen(server_socket_fd, BACKLOG) != 0) {
    fprintf(stderr, "Failed to listen");
  }
  bool done = false;
  while (!done) {
    struct sockaddr_storage client_socket;
    uint32_t addr_size = sizeof(struct sockaddr_storage);
    int client_socket_fd =
        accept(server_socket_fd, (struct sockaddr *)&client_socket, &addr_size);
    if (client_socket_fd < 0) {
      fprintf(stderr, "Failed to accept");
    } else {
      const char *msg = "PongC server response";
      const int lng = sizeof(msg);
      const int bytes_sent = send(client_socket_fd, msg, lng, 0);
      if (bytes_sent < lng) {
        fprintf(stderr, "Did not successfully send full msg");
      }
      close(client_socket_fd);
    }
  }
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11ed <server+0x4d>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1251 <server+0xb1>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movl   $0x3,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x3,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     1284 <server+0xe4>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12a1 <server+0x101>
jmp    1333 <server+0x193>
movl   $0x4,-0x24(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12df <server+0x13f>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
call   1070 <fprintf@plt>
jmp    132e <server+0x18e>
lea    0xd71(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x8,-0x34(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x8,-0x38(%rbp)
jge    1326 <server+0x186>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
lea    0xd4c(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
jmp    128b <server+0xeb>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
je     12c2 <server+0x122>
mov    %eax,%ebx
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jne    12d4 <server+0x134>
mov    %ebx,%edi
mov    $0x3,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     123e <server+0x9e>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1070 <fprintf@plt>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
mov    0x2d79(%rip),%r12
lea    0xe01(%rip),%r13
jmp    1267 <server+0xc7>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1060 <close@plt>
movl   $0x4,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <accept@plt>
test   %eax,%eax
js     12b0 <server+0x110>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r13,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0x7,%eax
jg     1260 <server+0xc0>
mov    (%r12),%edi
lea    0xdca(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1260 <server+0xc0>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
lea    0xd8b(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1267 <server+0xc7>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
lea    0xd2e(%rip),%rsi
jmp    12e4 <server+0x144>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd3b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Failed to create server socket\00"
@AF_UNSPEC = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"Failed to bind socket\00"
@.str.2 = [17 x i8] c"Failed to listen\00"
@.str.3 = [17 x i8] c"Failed to accept\00"
@.str.4 = [22 x i8] c"PongC server response\00"
@.str.5 = [35 x i8] c"Did not successfully send full msg\00"
define void @server() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %1
%14 = load i32, i32* %1
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %0
%21 = load i32, i32* @AF_UNSPEC
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 8080)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %1
%30 = bitcast %struct.sockaddr_in* %2 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 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %20
store i32 3, i32* %3
%38 = load i32, i32* %1
%39 = call i64 @listen(i32 %38, i32 3)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %41, %37
store i32 0, i32* %4
br label %45
45: ; preds = %70, %44
%46 = load i32, i32* %4
%47 = icmp ne i32 %46, 0
%48 = xor i1 %47, true
br i1 %48, label %49, label %71
49: ; preds = %45
store i32 4, i32* %6
%50 = load i32, i32* %1
%51 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%52 = call i32 @accept(i32 %50, %struct.sockaddr* %51, i32* %6)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %70
58: ; preds = %49
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8** %8
store i32 8, i32* %9
%59 = load i32, i32* %7
%60 = load i8*, i8** %8
%61 = call i32 @send(i32 %59, i8* %60, i32 8, i32 0)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = icmp slt i32 %62, 8
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %67
67: ; preds = %64, %58
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
br label %70
70: ; preds = %67, %55
br label %45
71: ; preds = %45
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Failed to create server socket\00"
@AF_UNSPEC = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"Failed to bind socket\00"
@.str.2 = [17 x i8] c"Failed to listen\00"
@.str.3 = [17 x i8] c"Failed to accept\00"
@.str.4 = [22 x i8] c"PongC server response\00"
@.str.5 = [35 x i8] c"Did not successfully send full msg\00"
define void @server() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = bitcast %struct.sockaddr_in* %1 to i8*
%14 = load i32, i32* @AF_UNSPEC
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 8080)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i64 @bind(i32 %6, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %12
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %12
%29 = call i64 @listen(i32 %6, i32 3)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %31, %28
%35 = bitcast %struct.sockaddr_storage* %2 to i8*
%36 = bitcast i32* %3 to i8*
%37 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
br label %38
38: ; preds = %52, %34
store i32 4, i32* %3
%39 = call i32 @accept(i32 %6, %struct.sockaddr* %37, i32* %3)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %52
44: ; preds = %38
%45 = call i32 @send(i32 %39, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 8, i32 0)
%46 = icmp slt i32 %45, 8
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %47, %44
%51 = call i32 @close(i32 %39)
br label %52
52: ; preds = %50, %41
br label %38
}