#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__ {
  scalar_t__ 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*/ 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*/ htons(int);
char *inet_ntoa(TYPE_1__);
int listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(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 = sizeof(struct sockaddr_in);
  int recv_length = 1, yes = 1;
  char buffer[1024];
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    perror("in socket");
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    perror("setting socket option SO_REUSEADDR");
  host_addr.sin_family = AF_INET;
  host_addr.sin_port = htons(7890);
  host_addr.sin_addr.s_addr = 0;
  memset(&(host_addr.sin_zero), '\0', sizeof(host_addr.sin_zero));
  if (bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) ==
      -1)
    perror("binding to socket");
  if (listen(sockfd, 5) == -1)
    perror("listening on socket");
  while (1) {
    new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
    if (new_sockfd == -1)
      perror("accepting connection");
    printf("server: got connection from %s port %d\n",
           inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    recv_length = recv(new_sockfd, &buffer, 50, 0);
    while (recv_length > 0) {
      printf("RECV: %d bytes\n", recv_length);
      dump(buffer, recv_length);
      recv_length = recv(new_sockfd, &buffer, 50, 0);
    }
    close(new_sockfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x18,-0x44(%rbp)
movl   $0x1,-0x48(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1238 <main+0x58>
lea    0xdcd(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
lea    -0x4c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1271 <main+0x91>
lea    0xd9e(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x1ed2,%edi
call   1060 <htons@plt>
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x10,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12cc <main+0xec>
lea    0xd66(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ee <main+0x10e>
lea    0xd56(%rip),%rdi
call   10e0 <perror@plt>
jmp    12f3 <main+0x113>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    131c <main+0x13c>
lea    0xd3c(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x38(%rbp),%rdi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x458(%rbp)
mov    -0x40(%rbp),%edi
call   10c0 <ntohs@plt>
mov    -0x458(%rbp),%rsi
mov    %eax,%edx
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x32,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jle    13ac <main+0x1cc>
mov    -0x48(%rbp),%esi
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x450(%rbp),%rdi
mov    -0x48(%rbp),%esi
call   10a0 <dump@plt>
mov    -0xc(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x32,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x48(%rbp)
jmp    1364 <main+0x184>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
jmp    12f3 <main+0x113>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
movl   $0x18,0xc(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2dd8(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
je     1376 <main+0x196>
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1387 <main+0x1a7>
mov    (%rbx),%eax
mov    %eax,0x24(%rsp)
mov    $0x1ed2,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
mov    $0x4,%edx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
je     1398 <main+0x1b8>
mov    %r13d,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
je     13a9 <main+0x1c9>
lea    0x40(%rsp),%rbp
lea    0xdd1(%rip),%r12
jmp    12d8 <main+0xf8>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
call   1090 <close@plt>
mov    %r13d,%edi
lea    0x28(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10f0 <accept@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    12fe <main+0x11e>
lea    0xd5a(%rip),%rdi
call   10e0 <perror@plt>
mov    0x30(%rsp),%rdi
call   1050 <inet_ntoa@plt>
mov    %rax,%rbx
mov    0x28(%rsp),%edi
call   10c0 <ntohs@plt>
lea    0xd4d(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x32,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12d0 <main+0xf0>
mov    %eax,%r15d
mov    %r12,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
call   10a0 <dump@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x32,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r15d
test   %eax,%eax
jg     1340 <main+0x160>
jmp    12d0 <main+0xf0>
lea    0xc83(%rip),%rdi
call   10e0 <perror@plt>
jmp    1226 <main+0x46>
lea    0xc7c(%rip),%rdi
call   10e0 <perror@plt>
jmp    1254 <main+0x74>
lea    0xc8e(%rip),%rdi
call   10e0 <perror@plt>
jmp    129d <main+0xbd>
lea    0xc8f(%rip),%rdi
call   10e0 <perror@plt>
jmp    12b3 <main+0xd3>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@AF_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"
@.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 = [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 24, i32* %6
store i32 1, i32* %7
store i32 1, i32* %8
%10 = load i32, i32* @AF_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 @perror(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 @perror(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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%32 = call i32 @memset(i32* %31, i8 signext 0, i32 4)
%33 = load i32, i32* %2
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 4)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %24
%38 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %24
%40 = load i32, i32* %2
%41 = call i32 @listen(i32 %40, i32 5)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
br label %46
46: ; preds = %45, %78
%47 = load i32, i32* %2
%48 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%49 = call i32 @accept(i32 %47, %struct.sockaddr* %48, i32* %6)
store i32 %49, i32* %3
%50 = load i32, i32* %3
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %46
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i64, i64* %56
%58 = call i8* @inet_ntoa(i64 %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @ntohs(i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %58, i32 %61)
%63 = load i32, i32* %3
%64 = bitcast [1024 x i8]* %9 to i8**
%65 = call i32 @recv(i32 %63, i8** %64, i32 50, i32 0)
store i32 %65, i32* %7
br label %66
66: ; preds = %69, %54
%67 = load i32, i32* %7
%68 = icmp sgt i32 %67, 0
br i1 %68, label %69, label %78
69: ; preds = %66
%70 = load i32, i32* %7
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %70)
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%73 = load i32, i32* %7
%74 = call i32 @dump(i8* %72, i32 %73)
%75 = load i32, i32* %3
%76 = bitcast [1024 x i8]* %9 to i8**
%77 = call i32 @recv(i32 %75, i8** %76, i32 50, i32 0)
store i32 %77, i32* %7
br label %66
78: ; preds = %66
%79 = load i32, i32* %3
%80 = call i32 @close(i32 %79)
br label %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@AF_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"
@.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 = [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*
store i32 24, i32* %3
%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* @AF_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 @perror(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 @perror(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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
%31 = call i32 @memset(i32* %30, i8 signext 0, i32 4)
%32 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%33 = call i32 @bind(i32 %13, %struct.sockaddr* %32, i32 4)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %24
%36 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %24
%38 = call i32 @listen(i32 %13, i32 5)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%46 = bitcast [1024 x i8]* %5 to i8**
br label %47
47: ; preds = %66, %42
%48 = call i32 @accept(i32 %13, %struct.sockaddr* %43, i32* %3)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %47
%53 = load i64, i64* %44
%54 = call i8* @inet_ntoa(i64 %53)
%55 = load i32, i32* %45
%56 = call i32 @ntohs(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %54, i32 %56)
%58 = call i32 @recv(i32 %48, i8** %46, i32 50, i32 0)
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %52, %60
%61 = phi i32 [ %64, %60 ], [ %58, %52 ]
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %61)
%63 = call i32 @dump(i8* %10, i32 %61)
%64 = call i32 @recv(i32 %48, i8** %46, i32 50, i32 0)
%65 = icmp sgt i32 %64, 0
br i1 %65, label %60, label %66
66: ; preds = %60, %52
%67 = call i32 @close(i32 %48)
br label %47
}