#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ deservire_client(int);
scalar_t__ fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int s;
  struct sockaddr_in server, client;
  int c, l;
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0) {
    printf("Eroare la crearea socketului server\n");
    return 1;
  }
  memset(&server, 0, sizeof(server));
  server.sin_port = htons(2017);
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0) {
    printf("Eroare la bind\n");
    return 1;
  }
  listen(s, 5);
  l = sizeof(client);
  memset(&client, 0, sizeof(client));
  while (1) {
    c = accept(s, (struct sockaddr *)&client, &l);
    printf("S-a conectat un client.\n");
    if (fork() == 0) {
      deservire_client(c);
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11dd <main+0x4d>
lea    0xe34(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x122>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    $0x7e1,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1245 <main+0xb5>
lea    0xdf1(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x122>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x2c(%rbp)
lea    0xdb2(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    12ad <main+0x11d>
mov    -0x2c(%rbp),%edi
call   1030 <deservire_client@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <main+0x122>
jmp    1269 <main+0xd9>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e45(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     126e <main+0xde>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    $0x7e1,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1277 <main+0xe7>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
lea    0xc(%rsp),%r15
lea    0xdc2(%rip),%r12
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <accept@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    1240 <main+0xb0>
mov    %ebp,%edi
call   1040 <deservire_client@plt>
xor    %eax,%eax
jmp    1288 <main+0xf8>
lea    0xdb2(%rip),%rdi
jmp    127e <main+0xee>
lea    0xd9a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [37 x i8] c"Eroare la crearea socketului server\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [16 x i8] c"Eroare la bind\0A\00"
@.str.2 = [25 x i8] c"S-a conectat un client.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %44
14: ; preds = %0
%15 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%16 = call i32 @htons(i32 2017)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %44
29: ; preds = %14
%30 = load i32, i32* %2
%31 = call i32 @listen(i32 %30, i32 5)
store i32 12, i32* %6
%32 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
br label %33
33: ; preds = %29, %43
%34 = load i32, i32* %2
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i32 @accept(i32 %34, %struct.sockaddr* %35, i32* %6)
store i32 %36, i32* %5
%37 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%38 = call i64 (...) @fork()
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = load i32, i32* %5
%42 = call i32 @deservire_client(i32 %41)
store i32 0, i32* %1
br label %44
43: ; preds = %33
br label %33
44: ; preds = %40, %27, %12
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, 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
@str = [24 x i8] c"S-a conectat un client.\00"
@str.3 = [15 x i8] c"Eroare la bind\00"
@str.4 = [36 x i8] c"Eroare la crearea socketului server\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.4, i64 0, i64 0))
br label %37
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%15 = call i32 @htons(i32 2017)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%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 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0))
br label %37
26: ; preds = %13
%27 = call i32 @listen(i32 %9, i32 5)
store i32 12, i32* %3
%28 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %30
30: ; preds = %30, %26
%31 = call i32 @accept(i32 %9, %struct.sockaddr* %29, i32* %3)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%33 = call i64 (...) @fork()
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %30
35: ; preds = %30
%36 = call i32 @deservire_client(i32 %31)
br label %37
37: ; preds = %35, %24, %11
%38 = phi i32 [ 1, %11 ], [ 1, %24 ], [ 0, %35 ]
ret i32 %38
}