#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__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  void *sin_port;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ Servidor;
typedef int /*<<< orphan*/ Datos;
typedef int /*<<< orphan*/ Cliente;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
void *htons(int);
void *inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
main() {
  int Socket_Cliente;
  struct sockaddr_in Cliente;
  int Datos;
  struct sockaddr_in Servidor;
  socklen_t Longitud_Servidor;
  Socket_Cliente = socket(AF_INET, SOCK_DGRAM, 0);
  if (Socket_Cliente == -1) {
    printf("No se puede abrir el socket cliente\n");
    exit(-1);
  }
  Cliente.sin_family = AF_INET;
  Cliente.sin_port = htons(1999);
  Cliente.sin_addr.s_addr = inet_addr("127.0.0.1");
  if (bind(Socket_Cliente, (struct sockaddr *)&Cliente, sizeof(Cliente)) ==
      -1) {
    close(Socket_Cliente);
  }
  Servidor.sin_family = AF_INET;
  Servidor.sin_port = htons(2000);
  Servidor.sin_addr.s_addr = inet_addr("127.0.0.1");
  Longitud_Servidor = sizeof(Servidor);
  srand(time(NULL));
  Datos = rand() % 20;
  printf("Envio %d\n", Datos);
  int enviado = sendto(Socket_Cliente, (char *)&Datos, sizeof(Datos), 0,
                       (struct sockaddr *)&Servidor, Longitud_Servidor);
  if (enviado < 0) {
    printf("Error al solicitar el servicio\n");
  }
  int recibido = recvfrom(Socket_Cliente, (char *)&Datos, sizeof(Datos), 0,
                          (struct sockaddr *)&Servidor, &Longitud_Servidor);
  if (recibido > 0)
    printf("Leido %d\n", Datos);
  else
    printf("Error al leer del servidor\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    120e <main+0x4e>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    $0x7cf,%edi
call   1030 <htons@plt>
mov    %rax,-0x18(%rbp)
lea    0xdf4(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    125c <main+0x9c>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    $0x7d0,%edi
call   1030 <htons@plt>
mov    %rax,-0x38(%rbp)
lea    0xda6(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %rax,-0x40(%rbp)
movl   $0x18,-0x44(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    $0x0,%al
call   10d0 <rand@plt>
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %edx,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
lea    -0x40(%rbp),%r8
mov    -0x44(%rbp),%r9d
mov    $0x4,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    12f8 <main+0x138>
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
lea    -0x40(%rbp),%r8
mov    $0x4,%edx
xor    %ecx,%ecx
lea    -0x44(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jle    1336 <main+0x176>
mov    -0x24(%rbp),%esi
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1344 <main+0x184>
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e0a(%rip),%r14
mov    (%r14),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     132a <main+0x15a>
mov    %eax,%ebx
mov    (%r14),%rax
mov    %rax,0x30(%rsp)
mov    $0x7cf,%edi
call   1040 <htons@plt>
mov    %rax,0x28(%rsp)
lea    0xde6(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %rax,0x20(%rsp)
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1241 <main+0x71>
mov    %ebx,%edi
call   1070 <close@plt>
mov    (%r14),%rax
mov    %rax,0x18(%rsp)
mov    $0x7d0,%edi
call   1040 <htons@plt>
mov    %rax,0x10(%rsp)
lea    0xda1(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %rax,0x8(%rsp)
movl   $0x18,0x4(%rsp)
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
xor    %eax,%eax
call   10e0 <rand@plt>
movslq %eax,%rsi
imul   $0x66666667,%rsi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
shl    $0x2,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%esi
mov    %esi,(%rsp)
lea    0xd5b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rsi
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1060 <sendto@plt>
test   %eax,%eax
jns    12e2 <main+0x112>
lea    0xd5c(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %ebx,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   10b0 <recvfrom@plt>
test   %eax,%eax
jle    1314 <main+0x144>
mov    (%rsp),%esi
lea    0xd09(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1320 <main+0x150>
lea    0xd03(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd27(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@.str = [37 x i8] c"No se puede abrir el socket cliente\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [10 x i8] c"Envio %d\0A\00"
@.str.3 = [32 x i8] c"Error al solicitar el servicio\0A\00"
@.str.4 = [10 x i8] c"Leido %d\0A\00"
@.str.5 = [28 x i8] c"Error al leer del servidor\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
%9 = load i8*, i8** @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i8* %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 ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %0
%18 = load i8*, i8** @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i8* %18, i8** %19
%20 = call i8* @htons(i32 1999)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i8* %20, i8** %21
%22 = call i8* @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%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 i8* %22, i8** %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 24)
%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
%33 = load i8*, i8** @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i8* %33, i8** %34
%35 = call i8* @htons(i32 2000)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i8* %35, i8** %36
%37 = call i8* @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i8* %37, i8** %39
store i32 24, i32* %6
%40 = call i32 @time(i32* null)
%41 = call i32 @srand(i32 %40)
%42 = call i32 (...) @rand()
%43 = srem i32 %42, 20
store i32 %43, i32* %4
%44 = load i32, i32* %4
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %2
%47 = bitcast i32* %4 to i8*
%48 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%49 = load i32, i32* %6
%50 = call i32 @sendto(i32 %46, i8* %47, i32 4, i32 0, %struct.sockaddr* %48, i32 %49)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %32
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %32
%56 = load i32, i32* %2
%57 = bitcast i32* %4 to i8*
%58 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%59 = call i32 @recvfrom(i32 %56, i8* %57, i32 4, i32 0, %struct.sockaddr* %58, i32* %6)
store i32 %59, i32* %8
%60 = load i32, i32* %8
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %55
%63 = load i32, i32* %4
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %63)
br label %67
65: ; preds = %55
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %67
67: ; preds = %65, %62
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [10 x i8] c"Envio %d\0A\00"
@.str.4 = [10 x i8] c"Leido %d\0A\00"
@str = [27 x i8] c"Error al leer del servidor\00"
@str.6 = [31 x i8] c"Error al solicitar el servicio\00"
@str.7 = [36 x i8] c"No se puede abrir el socket cliente\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*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = load i8*, i8** @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i8* %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 ([36 x i8], [36 x i8]* @str.7, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = load i8*, i8** @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i8* %17, i8** %18
%19 = tail call i8* @htons(i32 1999)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i8* %19, i8** %20
%21 = tail call i8* @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i8* %21, i8** %22
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i32 @bind(i32 %11, %struct.sockaddr* %23, i32 24)
%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
%29 = load i8*, i8** @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i8* %29, i8** %30
%31 = call i8* @htons(i32 2000)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i8* %31, i8** %32
%33 = call i8* @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i8* %33, i8** %34
store i32 24, i32* %4
%35 = call i32 @time(i32* null)
%36 = call i32 @srand(i32 %35)
%37 = call i32 (...) @rand()
%38 = srem i32 %37, 20
store i32 %38, i32* %2
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%41 = call i32 @sendto(i32 %11, i8* %6, i32 4, i32 0, %struct.sockaddr* %40, i32 24)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %28
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.6, i64 0, i64 0))
br label %45
45: ; preds = %43, %28
%46 = call i32 @recvfrom(i32 %11, i8* %6, i32 4, i32 0, %struct.sockaddr* %40, i32* %4)
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %2
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %49)
br label %53
51: ; preds = %45
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
ret i32 0
}