#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*/ dest;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char buffer[500 + 1];
  int len, client, ret;
  struct sockaddr_in dest;
  client = socket(AF_INET, SOCK_STREAM, 0);
  if (client < 0) {
    printf("Error: Socket()\n");
    return -1;
  }
  memset(&dest, 0, sizeof(dest));
  dest.sin_family = AF_INET;
  dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  dest.sin_port = htons(9999);
  ret = connect(client, (struct sockaddr *)&dest, sizeof(struct sockaddr));
  if (ret < 0) {
    printf("Error: Connect()\n");
    return -1;
  }
  len = recv(client, buffer, 500, 0);
  if (len < 0) {
    printf("Error: Recv()\n");
    return -1;
  }
  buffer[len] = '\0';
  printf("Mensaje %s : %d bytes\n", buffer, len);
  close(client);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x218(%rbp)
cmpl   $0x0,-0x218(%rbp)
jge    11df <main+0x5f>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <main+0x170>
lea    -0x228(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x220(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x224(%rbp)
mov    $0x270f,%edi
call   1040 <htons@plt>
mov    %eax,-0x228(%rbp)
mov    -0x218(%rbp),%edi
lea    -0x228(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
jge    1269 <main+0xe9>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <main+0x170>
mov    -0x218(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x1f4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jge    12af <main+0x12f>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <main+0x170>
movslq -0x214(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
lea    -0x210(%rbp),%rsi
mov    -0x214(%rbp),%edx
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x218(%rbp),%edi
call   1080 <close@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    0x2e45(%rip),%r15
mov    (%r15),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1250 <main+0xc0>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     1259 <main+0xc9>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1f4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1262 <main+0xd2>
mov    %eax,%ecx
movb   $0x0,0x10(%rsp,%rcx,1)
lea    0xdd0(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
jmp    1273 <main+0xe3>
lea    0xddf(%rip),%rdi
jmp    1269 <main+0xd9>
lea    0xdc5(%rip),%rdi
jmp    1269 <main+0xd9>
lea    0xdae(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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 = [17 x i8] c"Error: Socket()\0A\00"
@INADDR_LOOPBACK = global i32 0
@.str.1 = [18 x i8] c"Error: Connect()\0A\00"
@.str.2 = [15 x i8] c"Error: Recv()\0A\00"
@.str.3 = [23 x i8] c"Mensaje %s : %d bytes\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [501 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
18: ; preds = %2
%19 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_LOOPBACK
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = call i32 @htons(i32 9999)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %8
%29 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%30 = call i32 @connect(i32 %28, %struct.sockaddr* %29, i32 4)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %18
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
35: ; preds = %18
%36 = load i32, i32* %8
%37 = getelementptr inbounds [501 x i8], [501 x i8]* %6, i64 0, i64 0
%38 = call i32 @recv(i32 %36, i8* %37, i32 500, i32 0)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
43: ; preds = %35
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [501 x i8], [501 x i8]* %6, i64 0, i64 %45
store i8 0, i8* %46
%47 = getelementptr inbounds [501 x i8], [501 x i8]* %6, i64 0, i64 0
%48 = load i32, i32* %7
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %47, i32 %48)
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @EXIT_SUCCESS
store i32 %52, i32* %3
br label %53
53: ; preds = %43, %41, %33, %16
%54 = load i32, i32* %3
ret i32 %54
}

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_LOOPBACK = global i32 0
@.str.3 = [23 x i8] c"Mensaje %s : %d bytes\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [14 x i8] c"Error: Recv()\00"
@str.4 = [17 x i8] c"Error: Connect()\00"
@str.5 = [16 x i8] c"Error: Socket()\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [501 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [501 x i8], [501 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 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 = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
br label %38
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_LOOPBACK
%18 = call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = call i32 @htons(i32 9999)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%23 = call i32 @connect(i32 %9, %struct.sockaddr* %22, i32 4)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.4, i64 0, i64 0))
br label %38
27: ; preds = %13
%28 = call i32 @recv(i32 %9, i8* %5, i32 500, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %38
32: ; preds = %27
%33 = zext i32 %28 to i64
%34 = getelementptr inbounds [501 x i8], [501 x i8]* %3, i64 0, i64 %33
store i8 0, i8* %34
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %5, i32 %28)
%36 = call i32 @close(i32 %9)
%37 = load i32, i32* @EXIT_SUCCESS
br label %38
38: ; preds = %32, %30, %25, %11
%39 = phi i32 [ -1, %11 ], [ -1, %25 ], [ -1, %30 ], [ %37, %32 ]
ret i32 %39
}