#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 */
typedef int uint16_t;
typedef int /*<<< orphan*/ suma;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  void *sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ client;
typedef int /*<<< orphan*/ b;
typedef int /*<<< orphan*/ a;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ MSG_WAITALL;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
void *htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ recv(int, int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, int *, int, int /*<<< orphan*/);
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(1234);
  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) {
    uint16_t a, b, suma;
    c = accept(s, (struct sockaddr *)&client, &l);
    printf("S-a conectat un client.\n");
    recv(c, &a, sizeof(a), MSG_WAITALL);
    recv(c, &b, sizeof(b), MSG_WAITALL);
    a = ntohs(a);
    b = ntohs(b);
    suma = a + b;
    suma = htons(suma);
    send(c, &suma, sizeof(suma), 0);
    close(c);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11fd <main+0x4d>
lea    0xe14(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132a <main+0x17a>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %rax,-0x10(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    1266 <main+0xb6>
lea    0xdd0(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132a <main+0x17a>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0x10,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x2c(%rbp)
lea    0xd91(%rip),%rdi
call   1060 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d0d(%rip),%rax
mov    (%rax),%ecx
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
call   1030 <recv@plt>
mov    -0x2c(%rbp),%edi
mov    0x2cf3(%rip),%rax
mov    (%rax),%ecx
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1030 <recv@plt>
mov    -0x34(%rbp),%edi
call   10a0 <ntohs@plt>
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%edi
call   10a0 <ntohs@plt>
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
jmp    128a <main+0xda>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e23(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1304 <main+0x154>
mov    %eax,%ebx
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    $0x4d2,%edi
call   1050 <htons@plt>
mov    %rax,0x20(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x1c(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     130d <main+0x15d>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0x10,0x14(%rsp)
lea    0x28(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2d63(%rip),%r12
lea    0x8(%rsp),%r13
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%r14
nopl   0x0(%rax)
mov    %ebx,%edi
lea    0x28(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   10c0 <accept@plt>
mov    %eax,%r15d
lea    0xd75(%rip),%rdi
call   1040 <puts@plt>
mov    (%r12),%ecx
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1030 <recv@plt>
mov    (%r12),%ecx
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <recv@plt>
mov    0x8(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %eax,0x8(%rsp)
mov    0x10(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %eax,0x10(%rsp)
add    0x8(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r15d,%edi
call   1080 <close@plt>
jmp    1270 <main+0xc0>
lea    0xd1c(%rip),%rdi
jmp    1314 <main+0x164>
lea    0xd04(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i8* }
%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"
@MSG_WAITALL = global i32 0
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
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%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 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %61
17: ; preds = %0
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 16)
%19 = call i8* @htons(i32 1234)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i8* %19, i8** %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 16)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %17
%31 = 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 %61
32: ; preds = %17
%33 = load i32, i32* %2
%34 = call i32 @listen(i32 %33, i32 5)
store i32 16, i32* %6
%35 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 16)
br label %36
36: ; preds = %32, %36
%37 = load i32, i32* %2
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %6)
store i32 %39, i32* %5
%40 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* %5
%42 = load i32, i32* @MSG_WAITALL
%43 = call i32 @recv(i32 %41, i32* %7, i32 4, i32 %42)
%44 = load i32, i32* %5
%45 = load i32, i32* @MSG_WAITALL
%46 = call i32 @recv(i32 %44, i32* %8, i32 4, i32 %45)
%47 = load i32, i32* %7
%48 = call i32 @ntohs(i32 %47)
store i32 %48, i32* %7
%49 = load i32, i32* %8
%50 = call i32 @ntohs(i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %7
%52 = load i32, i32* %8
%53 = add nsw i32 %51, %52
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = call i8* @htons(i32 %54)
%56 = ptrtoint i8* %55 to i32
store i32 %56, i32* %9
%57 = load i32, i32* %5
%58 = call i32 @send(i32 %57, i32* %9, i32 4, i32 0)
%59 = load i32, i32* %5
%60 = call i32 @close(i32 %59)
br label %36
61: ; preds = %30, %15
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@MSG_WAITALL = 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 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %1 to i8*
%8 = bitcast %struct.sockaddr_in* %2 to i8*
%9 = bitcast i32* %3 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.4, i64 0, i64 0))
br label %54
16: ; preds = %0
%17 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 16)
%18 = call i8* @htons(i32 1234)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i8* %18, i8** %19
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i64 @bind(i32 %12, %struct.sockaddr* %24, i32 16)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0))
br label %54
29: ; preds = %16
%30 = call i32 @listen(i32 %12, i32 5)
store i32 16, i32* %3
%31 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 16)
%32 = bitcast i32* %4 to i8*
%33 = bitcast i32* %5 to i8*
%34 = bitcast i32* %6 to i8*
%35 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %36
36: ; preds = %36, %29
%37 = call i32 @accept(i32 %12, %struct.sockaddr* %35, i32* %3)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%39 = load i32, i32* @MSG_WAITALL
%40 = call i32 @recv(i32 %37, i32* %4, i32 4, i32 %39)
%41 = load i32, i32* @MSG_WAITALL
%42 = call i32 @recv(i32 %37, i32* %5, i32 4, i32 %41)
%43 = load i32, i32* %4
%44 = call i32 @ntohs(i32 %43)
store i32 %44, i32* %4
%45 = load i32, i32* %5
%46 = call i32 @ntohs(i32 %45)
store i32 %46, i32* %5
%47 = load i32, i32* %4
%48 = add nsw i32 %47, %46
store i32 %48, i32* %6
%49 = call i8* @htons(i32 %48)
%50 = ptrtoint i8* %49 to i64
%51 = trunc i64 %50 to i32
store i32 %51, i32* %6
%52 = call i32 @send(i32 %37, i32* %6, i32 4, i32 0)
%53 = call i32 @close(i32 %37)
br label %36
54: ; preds = %27, %14
ret i32 1
}