#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*/ member_0;
  int /*<<< orphan*/ member_1;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ client_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int sd, ns, k;
  struct sockaddr_in server_addr = {AF_INET, htons(7777)};
  struct sockaddr_in client_addr = {AF_INET};
  int client_len = sizeof(client_addr);
  char buf[1024], *host;
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("server: socket failed");
    exit(1);
  }
  if (bind(sd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
    perror("server: bind failed");
    exit(1);
  }
  if (listen(sd, 1) == -1) {
    perror("server: listen failed");
    exit(1);
  }
  printf("SERVER is listening for clients to establish a connection\n");
  if ((ns = accept(sd, (struct sockaddr *)&client_addr, &client_len)) == -1) {
    perror("server: accept failed");
    exit(1);
  }
  printf(
      "accept() successful.. a client has connected! waiting for a message\n");
  while ((k = read(ns, buf, sizeof(buf))) != 0) {
    printf("SERVER RECEIVED: %s\n", buf);
    write(ns, buf, k);
  }
  close(ns);
  close(sd);
  unlink(server_addr.sin_addr);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x1e61,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0xc,-0x34(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    124e <main+0x8e>
lea    0xdc1(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    127e <main+0xbe>
lea    0xda7(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12aa <main+0xea>
lea    0xd8f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    12ea <main+0x12a>
lea    0xda0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     1344 <main+0x184>
lea    -0x440(%rbp),%rsi
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <write@plt>
jmp    12f8 <main+0x138>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x430,%rsp
mov    0x2e02(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    $0x1e61,%edi
call   1060 <htons@plt>
mov    %eax,0x14(%rsp)
movl   $0x0,0x18(%rsp)
mov    (%rbx),%edi
mov    %edi,0x20(%rsp)
movq   $0x0,0x24(%rsp)
movl   $0xc,0xc(%rsp)
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     131b <main+0x14b>
mov    %eax,%r14d
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     1324 <main+0x154>
mov    %r14d,%edi
mov    $0x1,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
je     132d <main+0x15d>
lea    0xdff(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     1336 <main+0x166>
mov    %eax,%r12d
lea    0xe0f(%rip),%rdi
call   1040 <puts@plt>
lea    0x30(%rsp),%rsi
mov    %r12d,%edi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
je     12f0 <main+0x120>
mov    %eax,%ebp
lea    0xd9c(%rip),%r15
lea    0x30(%rsp),%rbx
nop
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    12c0 <main+0xf0>
mov    %r12d,%edi
call   1080 <close@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    0x10(%rsp),%edi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcde(%rip),%rdi
jmp    133d <main+0x16d>
lea    0xceb(%rip),%rdi
jmp    133d <main+0x16d>
lea    0xcf6(%rip),%rdi
jmp    133d <main+0x16d>
lea    0xd03(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"server: socket failed\00"
@.str.1 = [20 x i8] c"server: bind failed\00"
@.str.2 = [22 x i8] c"server: listen failed\00"
@.str.3 = [59 x i8] c"SERVER is listening for clients to establish a connection\0A\00"
@.str.4 = [22 x i8] c"server: accept failed\00"
@.str.5 = [69 x i8] c"accept() successful.. a client has connected! waiting for a message\0A\00"
@.str.6 = [21 x i8] c"SERVER RECEIVED: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i8*
store i32 0, i32* %1
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%11 = load i32, i32* @AF_INET
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%13 = call i32 @htons(i32 7777)
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%16 = load i32, i32* @AF_INET
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 0, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 0, i32* %18
store i32 12, i32* %7
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %2
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %0
%24 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %0
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i32, i32* %2
%36 = call i32 @listen(i32 %35, i32 1)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* %2
%44 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %7)
store i32 %45, i32* %3
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %41
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0))
br label %52
52: ; preds = %57, %50
%53 = load i32, i32* %3
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 1024)
store i32 %55, i32* %4
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %52
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %58)
%60 = load i32, i32* %3
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%62 = load i32, i32* %4
%63 = call i32 @write(i32 %60, i8* %61, i32 %62)
br label %52
64: ; preds = %52
%65 = load i32, i32* %3
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %2
%68 = call i32 @close(i32 %67)
%69 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%70 = load i32, i32* %69
%71 = call i32 @unlink(i32 %70)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"server: socket failed\00"
@.str.1 = [20 x i8] c"server: bind failed\00"
@.str.2 = [22 x i8] c"server: listen failed\00"
@.str.4 = [22 x i8] c"server: accept failed\00"
@.str.6 = [21 x i8] c"SERVER RECEIVED: %s\0A\00"
@str = [58 x i8] c"SERVER is listening for clients to establish a connection\00"
@str.7 = [68 x i8] c"accept() successful.. a client has connected! waiting for a message\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%7 = load i32, i32* @AF_INET
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%9 = tail call i32 @htons(i32 7777)
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 0, i32* %10
%11 = bitcast %struct.sockaddr_in* %2 to i8*
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%13 = load i32, i32* @AF_INET
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 0, i32* %15
%16 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %13, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %0
%22 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %0
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i32 @bind(i32 %19, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = call i32 @listen(i32 %19, i32 1)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %31
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str, i64 0, i64 0))
%39 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%40 = call i32 @accept(i32 %19, %struct.sockaddr* %39, i32* %3)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %37
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @str.7, i64 0, i64 0))
%47 = call i32 @read(i32 %40, i8* %17, i32 1024)
%48 = icmp eq i32 %47, 0
br i1 %48, label %55, label %49
49: ; preds = %45, %49
%50 = phi i32 [ %53, %49 ], [ %47, %45 ]
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %17)
%52 = call i32 @write(i32 %40, i8* %17, i32 %50)
%53 = call i32 @read(i32 %40, i8* %17, i32 1024)
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %49
55: ; preds = %49, %45
%56 = call i32 @close(i32 %40)
%57 = call i32 @close(i32 %19)
%58 = load i32, i32* %6
%59 = call i32 @unlink(i32 %58)
ret i32 0
}