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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, int *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, int *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
main() {
  int Socket_Servidor;
  struct sockaddr_in Servidor;
  int contador = 0;
  struct sockaddr_in Cliente;
  socklen_t Longitud_Cliente;
  Socket_Servidor = socket(AF_INET, SOCK_DGRAM, 0);
  if (Socket_Servidor == -1) {
    printf("No se puede abrir socket servidor\n");
    exit(-1);
  }
  Servidor.sin_family = AF_INET;
  Servidor.sin_port = htons(2000);
  Servidor.sin_addr.s_addr = INADDR_ANY;
  if (bind(Socket_Servidor, (struct sockaddr *)&Servidor, sizeof(Servidor)) ==
      -1) {
    close(Socket_Servidor);
  }
  Longitud_Cliente = sizeof(Cliente);
  while (1) {
    int recibido = recvfrom(Socket_Servidor, &contador, sizeof(contador), 0,
                            (struct sockaddr *)&Cliente, &Longitud_Cliente);
    if (recibido > 0) {
      printf("Recibido %d, envio %d\n", contador, contador + 1);
      contador++;
      int enviado = sendto(Socket_Servidor, &contador, sizeof(contador), 0,
                           (struct sockaddr *)&Cliente, Longitud_Cliente);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d4 <main+0x54>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x7d0,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dec(%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    $0xffffffff,%eax
jne    121b <main+0x9b>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xc,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%r8
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
lea    -0x2c(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    1288 <main+0x108>
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    %eax,-0x34(%rbp)
jmp    1222 <main+0xa2>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
movl   $0x0,0x8(%rsp)
mov    0x2e3c(%rip),%r14
mov    (%r14),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     127c <main+0xec>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x7d0,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1209 <main+0x79>
mov    %ebx,%edi
call   1070 <close@plt>
movl   $0xc,0xc(%rsp)
lea    0x8(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xdd9(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1080 <recvfrom@plt>
test   %eax,%eax
jle    1230 <main+0xa0>
mov    0x8(%rsp),%esi
lea    0x1(%rsi),%edx
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
addl   $0x1,0x8(%rsp)
mov    0xc(%rsp),%r9d
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1060 <sendto@plt>
jmp    1230 <main+0xa0>
lea    0xd94(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

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_DGRAM = global i32 0
@.str = [35 x i8] c"No se puede abrir socket servidor\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [23 x i8] c"Recibido %d, envio %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %0
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 2000)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29, %17
store i32 12, i32* %6
br label %33
33: ; preds = %32, %50
%34 = load i32, i32* %2
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i32 @recvfrom(i32 %34, i32* %4, i32 4, i32 0, %struct.sockaddr* %35, i32* %6)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %50
39: ; preds = %33
%40 = load i32, i32* %4
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %42)
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
%46 = load i32, i32* %2
%47 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%48 = load i32, i32* %6
%49 = call i32 @sendto(i32 %46, i32* %4, i32 4, i32 0, %struct.sockaddr* %47, i32 %48)
store i32 %49, i32* %8
br label %50
50: ; preds = %39, %33
br label %33
}

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_DGRAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [23 x i8] c"Recibido %d, envio %d\0A\00"
@str = [34 x i8] c"No se puede abrir socket servidor\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 2000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i32 @bind(i32 %11, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @close(i32 %11)
br label %28
28: ; preds = %26, %16
store i32 12, i32* %4
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %30
30: ; preds = %41, %28
%31 = call i32 @recvfrom(i32 %11, i32* %2, i32 4, i32 0, %struct.sockaddr* %29, i32* %4)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = load i32, i32* %2
%35 = add nsw i32 %34, 1
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %35)
%37 = load i32, i32* %2
%38 = add nsw i32 %37, 1
store i32 %38, i32* %2
%39 = load i32, i32* %4
%40 = call i32 @sendto(i32 %11, i32* %2, i32 4, i32 0, %struct.sockaddr* %29, i32 %39)
br label %41
41: ; preds = %33, %30
br label %30
}