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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int socket_desc = -1;
  int new_socket = -1;
  int c = 0;
  struct sockaddr_in server;
  struct sockaddr_in client;
  char *message = NULL;
  int on = 1;
  int ret = -1;
  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc == -1) {
    printf("Could not create socket.\n");
  }
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(8888);
  ret = setsockopt(socket_desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  if (ret < 0) {
    perror("setsockopt SO_REUSEADDR failed.");
  }
  if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
    puts("bind failed");
    return 1;
  }
  puts("bind done");
  listen(socket_desc, 3);
  puts("Waiting for incoming connections....");
  c = sizeof(struct sockaddr_in);
  while ((new_socket = accept(socket_desc, (struct sockaddr *)&client,
                              (socklen_t *)&c))) {
    puts("Connection accepted");
    message = "Hello Client, I have received your connection, but I have to go "
              "now, bye.\n";
    char *client_ip = inet_ntoa(client.sin_addr);
    int client_port = ntohs(client.sin_port);
    printf("client: ip: %s, port: %d\n", client_ip, client_port);
    write(new_socket, message, strlen(message));
    close(new_socket);
    new_socket = -1;
  }
  if (new_socket < 0) {
    perror("accept failed");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x1,-0x44(%rbp)
movl   $0xffffffff,-0x48(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1255 <main+0x75>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x22b8,%edi
call   1080 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    12b7 <main+0xd7>
lea    0xd68(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
jge    12ea <main+0x10a>
lea    0xd61(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d7 <main+0x1f7>
lea    0xd55(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
call   10b0 <listen@plt>
lea    0xd46(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     13ae <main+0x1ce>
lea    0xd3c(%rip),%rdi
call   1030 <puts@plt>
lea    0xd44(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%edi
call   10c0 <ntohs@plt>
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x54(%rbp),%edx
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x40(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    -0x18(%rbp),%edi
call   10a0 <close@plt>
movl   $0xffffffff,-0x18(%rbp)
jmp    1316 <main+0x136>
cmpl   $0x0,-0x18(%rbp)
jge    13d0 <main+0x1f0>
lea    0xd2f(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d7 <main+0x1f7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x0,(%rsp)
movl   $0x1,0x4(%rsp)
mov    0x2ddc(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1229 <main+0x49>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x20(%rsp)
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x22b8,%edi
call   1080 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    0x2d66(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %eax,%eax
js     1382 <main+0x1a2>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10d0 <bind@plt>
test   %rax,%rax
js     1359 <main+0x179>
lea    0xd95(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x3,%esi
call   10b0 <listen@plt>
lea    0xd87(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc,(%rsp)
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
call   10f0 <accept@plt>
test   %eax,%eax
je     136d <main+0x18d>
mov    %eax,%r14d
lea    0xd93(%rip),%rbx
mov    %rsp,%r13
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xd64(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,%r12
mov    0x8(%rsp),%edi
call   10c0 <ntohs@plt>
lea    0xda2(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
mov    %ebp,%edi
lea    0x8(%rsp),%rsi
mov    %r13,%rdx
call   10f0 <accept@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    12f0 <main+0x110>
jmp    1370 <main+0x190>
lea    0xcc0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r15d
jmp    1370 <main+0x190>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc77(%rip),%rdi
call   10e0 <perror@plt>
jmp    1276 <main+0x96>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"Could not create socket.\0A\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [32 x i8] c"setsockopt SO_REUSEADDR failed.\00"
@.str.2 = [12 x i8] c"bind failed\00"
@.str.3 = [10 x i8] c"bind done\00"
@.str.4 = [37 x i8] c"Waiting for incoming connections....\00"
@.str.5 = [20 x i8] c"Connection accepted\00"
@.str.6 = [75 x i8] c"Hello Client, I have received your connection, but I have to go now, bye.\0A\00"
@.str.7 = [26 x i8] c"client: ip: %s, port: %d\0A\00"
@.str.8 = [14 x i8] c"accept failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
store i8* null, i8** %11
store i32 1, i32* %12
store i32 -1, i32* %13
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = call i32 @htons(i32 8888)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i32 @setsockopt(i32 %31, i32 %32, i32 %33, i32* %12, i32 4)
store i32 %34, i32* %13
%35 = load i32, i32* %13
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %23
%38 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %23
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%42 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %81
46: ; preds = %39
%47 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* %6
%49 = call i32 @listen(i32 %48, i32 3)
%50 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %8
br label %51
51: ; preds = %56, %46
%52 = load i32, i32* %6
%53 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%54 = call i32 @accept(i32 %52, %struct.sockaddr* %53, i32* %8)
store i32 %54, i32* %7
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %75
56: ; preds = %51
%57 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
store i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.6, i64 0, i64 0), i8** %11
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%59 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i8* @inet_ntoa(i32 %60)
store i8* %61, i8** %14
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i32 @ntohs(i32 %63)
store i32 %64, i32* %15
%65 = load i8*, i8** %14
%66 = load i32, i32* %15
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %65, i32 %66)
%68 = load i32, i32* %7
%69 = load i8*, i8** %11
%70 = load i8*, i8** %11
%71 = call i32 @strlen(i8* %70)
%72 = call i32 @write(i32 %68, i8* %69, i32 %71)
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
store i32 -1, i32* %7
br label %51
75: ; preds = %51
%76 = load i32, i32* %7
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %75
%79 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %81
80: ; preds = %75
store i32 0, i32* %3
br label %81
81: ; preds = %80, %78, %44
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [32 x i8] c"setsockopt SO_REUSEADDR failed.\00"
@.str.2 = [12 x i8] c"bind failed\00"
@.str.3 = [10 x i8] c"bind done\00"
@.str.4 = [37 x i8] c"Waiting for incoming connections....\00"
@.str.5 = [20 x i8] c"Connection accepted\00"
@.str.6 = [75 x i8] c"Hello Client, I have received your connection, but I have to go now, bye.\0A\00"
@.str.7 = [26 x i8] c"client: ip: %s, port: %d\0A\00"
@str = [25 x i8] c"Could not create socket.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%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 = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 8888)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %13, i32 %24, i32 %25, i32* %6, i32 4)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %17
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i64 @bind(i32 %13, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %59
36: ; preds = %30
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @listen(i32 %13, i32 3)
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %3
%40 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%41 = call i32 @accept(i32 %13, %struct.sockaddr* %40, i32* %3)
%42 = icmp eq i32 %41, 0
br i1 %42, label %59, label %43
43: ; preds = %36
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
br label %46
46: ; preds = %43, %46
%47 = phi i32 [ %41, %43 ], [ %57, %46 ]
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%49 = load i32, i32* %44
%50 = call i8* @inet_ntoa(i32 %49)
%51 = load i32, i32* %45
%52 = call i32 @ntohs(i32 %51)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %50, i32 %52)
%54 = call i32 @strlen(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @write(i32 %47, i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.6, i64 0, i64 0), i32 %54)
%56 = call i32 @close(i32 %47)
%57 = call i32 @accept(i32 %13, %struct.sockaddr* %40, i32* %3)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %46
59: ; preds = %46, %36, %34
%60 = phi i32 [ 1, %34 ], [ 0, %36 ], [ 0, %46 ]
ret i32 %60
}