#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_zero;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dump(char *, int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char **, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sockfd, new_sockfd;
  struct sockaddr_in host_addr, client_addr;
  socklen_t sin_size;
  int recv_length = 1, yes = 1;
  char buffer[1024];
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    fatal("in socket");
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    fatal("setting socket option SO_REUSEADDR");
  host_addr.sin_family = AF_INET;
  host_addr.sin_port = htons(7890);
  host_addr.sin_addr.s_addr = INADDR_ANY;
  memset(&(host_addr.sin_zero), '\0', 8);
  if (bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) ==
      -1)
    fatal("binding to socket");
  if (listen(sockfd, 5) == -1)
    fatal("listening on socket");
  while (1) {
    sin_size = sizeof(struct sockaddr_in);
    new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
    if (new_sockfd == -1)
      fatal("accepting connection");
    printf("server: got connection from %s port %d\n",
           inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    send(new_sockfd, "Hello World!\n", 13, 0);
    recv_length = recv(new_sockfd, &buffer, 1024, 0);
    while (recv_length > 0) {
      printf("RECV: %d bytes\n", recv_length);
      dump(buffer, recv_length);
      recv_length = recv(new_sockfd, &buffer, 1024, 0);
    }
    close(new_sockfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1241 <main+0x51>
lea    0xdc4(%rip),%rdi
call   1100 <fatal@plt>
mov    -0x8(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    127a <main+0x8a>
lea    0xd95(%rip),%rdi
call   1100 <fatal@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x1ed2,%edi
call   1060 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12d9 <main+0xe9>
lea    0xd59(%rip),%rdi
call   1100 <fatal@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12fb <main+0x10b>
lea    0xd49(%rip),%rdi
call   1100 <fatal@plt>
jmp    1300 <main+0x110>
movl   $0x10,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1330 <main+0x140>
lea    0xd28(%rip),%rdi
call   1100 <fatal@plt>
mov    -0x2c(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x448(%rbp)
mov    -0x30(%rbp),%edi
call   10d0 <ntohs@plt>
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
lea    0xd28(%rip),%rsi
mov    $0xd,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0xc(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jle    13d5 <main+0x1e5>
mov    -0x38(%rbp),%esi
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x440(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   10b0 <dump@plt>
mov    -0xc(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x38(%rbp)
jmp    138d <main+0x19d>
mov    -0xc(%rbp),%edi
call   10a0 <close@plt>
jmp    1300 <main+0x110>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
movl   $0x1,0x8(%rsp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1235 <main+0x45>
lea    0xdd0(%rip),%rdi
call   1100 <fatal@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    126a <main+0x7a>
lea    0xda5(%rip),%rdi
call   1100 <fatal@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x1ed2,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12c5 <main+0xd5>
lea    0xd6d(%rip),%rdi
call   1100 <fatal@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e2 <main+0xf2>
lea    0xd62(%rip),%rdi
call   1100 <fatal@plt>
lea    0x30(%rsp),%r12
lea    0xdb0(%rip),%r14
jmp    12f8 <main+0x108>
mov    %r15d,%edi
call   10a0 <close@plt>
movl   $0x10,0xc(%rsp)
mov    %ebx,%edi
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10f0 <accept@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1325 <main+0x135>
lea    0xd33(%rip),%rdi
call   1100 <fatal@plt>
mov    0x24(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x20(%rsp),%edi
call   10d0 <ntohs@plt>
lea    0xd27(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15d,%edi
lea    0xd39(%rip),%rsi
mov    $0xd,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12f0 <main+0x100>
mov    %eax,%r13d
mov    %r14,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12,%rdi
mov    %r13d,%esi
call   10b0 <dump@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r13d
test   %eax,%eax
jg     1380 <main+0x190>
jmp    12f0 <main+0x100>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"in socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [35 x i8] c"setting socket option SO_REUSEADDR\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [18 x i8] c"binding to socket\00"
@.str.3 = [20 x i8] c"listening on socket\00"
@.str.4 = [21 x i8] c"accepting connection\00"
@.str.5 = [40 x i8] c"server: got connection from %s port %d\0A\00"
@.str.6 = [14 x i8] c"Hello World!\0A\00"
@.str.7 = [16 x i8] c"RECV: %d bytes\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
store i32 0, i32* %1
store i32 1, i32* %7
store i32 1, i32* %8
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @fatal(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32, i32* %2
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %8, i32 4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 3
store i32 %25, i32* %26
%27 = call i32 @htons(i32 7890)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%33 = call i32 @memset(i32* %32, i8 signext 0, i32 8)
%34 = load i32, i32* %2
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i32 @bind(i32 %34, %struct.sockaddr* %35, i32 4)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %24
%39 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %24
%41 = load i32, i32* %2
%42 = call i32 @listen(i32 %41, i32 5)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
br label %47
47: ; preds = %46, %81
store i32 16, i32* %6
%48 = load i32, i32* %2
%49 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%50 = call i32 @accept(i32 %48, %struct.sockaddr* %49, i32* %6)
store i32 %50, i32* %3
%51 = load i32, i32* %3
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i8* @inet_ntoa(i32 %58)
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @ntohs(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %59, i32 %62)
%64 = load i32, i32* %3
%65 = call i32 @send(i32 %64, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 13, i32 0)
%66 = load i32, i32* %3
%67 = bitcast [1024 x i8]* %9 to i8**
%68 = call i32 @recv(i32 %66, i8** %67, i32 1024, i32 0)
store i32 %68, i32* %7
br label %69
69: ; preds = %72, %55
%70 = load i32, i32* %7
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %81
72: ; preds = %69
%73 = load i32, i32* %7
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %73)
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%76 = load i32, i32* %7
%77 = call i32 @dump(i8* %75, i32 %76)
%78 = load i32, i32* %3
%79 = bitcast [1024 x i8]* %9 to i8**
%80 = call i32 @recv(i32 %78, i8** %79, i32 1024, i32 0)
store i32 %80, i32* %7
br label %69
81: ; preds = %69
%82 = load i32, i32* %3
%83 = call i32 @close(i32 %82)
br label %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"in socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [35 x i8] c"setting socket option SO_REUSEADDR\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [18 x i8] c"binding to socket\00"
@.str.3 = [20 x i8] c"listening on socket\00"
@.str.4 = [21 x i8] c"accepting connection\00"
@.str.5 = [40 x i8] c"server: got connection from %s port %d\0A\00"
@.str.6 = [14 x i8] c"Hello World!\0A\00"
@.str.7 = [16 x i8] c"RECV: %d bytes\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @fatal(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %13, i32 %18, i32 %19, i32* %4, i32 4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %25, i32* %26
%27 = call i32 @htons(i32 7890)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
%32 = call i32 @memset(i32* %31, i8 signext 0, i32 8)
%33 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%34 = call i32 @bind(i32 %13, %struct.sockaddr* %33, i32 4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %24
%37 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %24
%39 = call i32 @listen(i32 %13, i32 5)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%47 = bitcast [1024 x i8]* %5 to i8**
br label %48
48: ; preds = %68, %43
store i32 16, i32* %3
%49 = call i32 @accept(i32 %13, %struct.sockaddr* %44, i32* %3)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
%54 = load i32, i32* %45
%55 = call i8* @inet_ntoa(i32 %54)
%56 = load i32, i32* %46
%57 = call i32 @ntohs(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %55, i32 %57)
%59 = call i32 @send(i32 %49, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 13, i32 0)
%60 = call i32 @recv(i32 %49, i8** %47, i32 1024, i32 0)
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %53, %62
%63 = phi i32 [ %66, %62 ], [ %60, %53 ]
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %63)
%65 = call i32 @dump(i8* %10, i32 %63)
%66 = call i32 @recv(i32 %49, i8** %47, i32 1024, i32 0)
%67 = icmp sgt i32 %66, 0
br i1 %67, label %62, label %68
68: ; preds = %62, %53
%69 = call i32 @close(i32 %49)
br label %48
}