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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main() {
  int client;
  struct sockaddr_in addr;
  int addr_len = sizeof(addr);
  char buffer[1024] = "";
  if ((client = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    printf("Error: Socket()\n");
    return -1;
  }
  bzero(&addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9999);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (connect(client, (struct sockaddr *)&addr, sizeof addr) < 0) {
    perror("Error: Connect()\n");
    return -1;
  }
  while (strcmp(buffer, "q") != 0) {
    gets(buffer);
    send(client, buffer, strlen(buffer), 0);
    recv(client, buffer, 1024, 0);
    printf("Mensaje: %s\n", buffer);
  }
  close(client);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xc,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    124b <main+0x6b>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <main+0x17c>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0x0,%rax
jge    12b6 <main+0xd6>
lea    0xd6c(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135c <main+0x17c>
jmp    12bb <main+0xdb>
lea    -0x420(%rbp),%rdi
lea    0xd5a(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     134d <main+0x16d>
lea    -0x420(%rbp),%rdi
call   10c0 <gets@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x42c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    -0x420(%rbp),%rsi
lea    0xce4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12bb <main+0xdb>
mov    -0x8(%rbp),%edi
call   10a0 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   10a0 <memset@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
js     1307 <main+0x117>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10f0 <bzero@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x270f,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
test   %rax,%rax
js     1326 <main+0x136>
lea    0xd86(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10c0 <strcmp@plt>
test   %rax,%rax
je     12fc <main+0x10c>
lea    0x10(%rsp),%rbx
lea    0xd6d(%rip),%r14
lea    0xd64(%rip),%r15
xchg   %ax,%ax
mov    %rbx,%rdi
call   10d0 <gets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10c0 <strcmp@plt>
test   %rax,%rax
jne    12b0 <main+0xc0>
mov    %ebp,%edi
call   10b0 <close@plt>
xor    %eax,%eax
jmp    1318 <main+0x128>
lea    0xd13(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd3(%rip),%rdi
call   10e0 <perror@plt>
jmp    1313 <main+0x123>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [18 x i8] c"Error: Connect()\0A\00"
@.str.2 = [2 x i8] c"q\00"
@.str.3 = [13 x i8] c"Mensaje: %s\0A\00"
@str = [16 x i8] c"Error: Socket()\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1024 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %38
11: ; preds = %0
%12 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 9999)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = tail call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i64 @connect(i32 %7, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %11
%24 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %36, label %28
26: ; preds = %11
%27 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %38
28: ; preds = %23, %28
%29 = call i32 @gets(i8* %4)
%30 = call i32 @strlen(i8* %4)
%31 = call i32 @send(i32 %7, i8* %4, i32 %30, i32 0)
%32 = call i32 @recv(i32 %7, i8* %4, i32 1024, i32 0)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%34 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %28
36: ; preds = %28, %23
%37 = call i32 @close(i32 %7)
br label %38
38: ; preds = %36, %26, %9
%39 = phi i32 [ -1, %9 ], [ -1, %26 ], [ 0, %36 ]
ret i32 %39
}