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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
unsigned short atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closeWithError(char *);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char **, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ write(int, char **, int);
int main(int argc, char *argv[]) {
  int client_socktfd;
  struct sockaddr_in client_address;
  unsigned short server_port;
  char *server_address;
  char buff[32];
  int client_length;
  if ((argc < 2) || (argc > 3)) {
    fprintf(stderr, "Uso: %s <Server IP> [<Echo Port>]\n", argv[0]);
    exit(1);
  }
  server_address = argv[1];
  if (argc == 3) {
    server_port = atoi(argv[2]);
  } else {
    server_port = 5000;
  }
  if ((client_socktfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    closeWithError("socket() fallo");
  }
  memset(&client_address, 0, sizeof(client_address));
  client_address.sin_family = AF_INET;
  client_address.sin_addr.s_addr = inet_addr(server_address);
  client_address.sin_port = htons(server_port);
  client_length = sizeof(client_address);
  printf("%s\n", "Triying Connection...");
  if (connect(client_socktfd, (struct sockaddr *)&client_address,
              client_length) == -1) {
    closeWithError("Connection failed");
  }
  printf("%s\n", "Successful Connection");
  printf("%s\n", "Waiting for entry...");
  while (1) {
    printf("%s", "User:");
    fgets(buff, 32, stdin);
    write(client_socktfd, &buff, 32);
    read(client_socktfd, &buff, 32);
    printf("Server:%s", buff);
  }
  close(client_socktfd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11fa <main+0x2a>
cmpl   $0x3,-0x8(%rbp)
jle    1220 <main+0x50>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdef(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    124c <main+0x7c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %ax,-0x22(%rbp)
jmp    1252 <main+0x82>
movw   $0x1388,-0x22(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1283 <main+0xb3>
lea    0xda5(%rip),%rdi
call   1040 <closeWithError@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
movzwl -0x22(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
movl   $0xc,-0x54(%rbp)
lea    0xd6d(%rip),%rdi
lea    0xd6a(%rip),%rsi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x54(%rbp),%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12f5 <main+0x125>
lea    0xd5c(%rip),%rdi
call   1040 <closeWithError@plt>
lea    0xd36(%rip),%rdi
lea    0xd5b(%rip),%rsi
call   1060 <printf@plt>
lea    0xd23(%rip),%rdi
lea    0xd5e(%rip),%rsi
call   1060 <printf@plt>
lea    0xd74(%rip),%rdi
lea    0xd60(%rip),%rsi
call   1060 <printf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2ca7(%rip),%rax
mov    (%rax),%edx
mov    $0x20,%esi
call   1090 <fgets@plt>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x20,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x20,%edx
call   1080 <read@plt>
lea    -0x50(%rbp),%rsi
lea    0xd1d(%rip),%rdi
call   1060 <printf@plt>
jmp    131b <main+0x14b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    -0x4(%rdi),%eax
cmp    $0xfffffffd,%eax
jbe    1327 <main+0x157>
mov    0x8(%rsi),%r14
mov    $0x1388,%bp
cmp    $0x3,%edi
jne    1202 <main+0x32>
mov    0x10(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebp
mov    0x2ddf(%rip),%r12
mov    (%r12),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    122f <main+0x5f>
lea    0xdf9(%rip),%rdi
call   1040 <closeWithError@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r14,%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
movzwl %bp,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
lea    0xdcb(%rip),%rdi
lea    0xdc8(%rip),%rsi
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1293 <main+0xc3>
lea    0xdbe(%rip),%rdi
call   1040 <closeWithError@plt>
lea    0xd98(%rip),%rbp
lea    0xdbd(%rip),%rsi
mov    %rbp,%rdi
call   1060 <printf@plt>
lea    0xdc4(%rip),%rsi
mov    %rbp,%rdi
call   1060 <printf@plt>
lea    0xdd7(%rip),%r14
lea    0xdc3(%rip),%r15
mov    0x2d13(%rip),%r13
lea    0x10(%rsp),%rbp
lea    0xdb6(%rip),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
mov    0x0(%r13),%edx
mov    %rbp,%rdi
mov    $0x20,%esi
call   1090 <fgets@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x20,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x20,%edx
call   1080 <read@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1060 <printf@plt>
jmp    12e0 <main+0x110>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xcc6(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [35 x i8] c"Uso: %s <Server IP> [<Echo Port>]\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"socket() fallo\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [22 x i8] c"Triying Connection...\00"
@.str.4 = [18 x i8] c"Connection failed\00"
@.str.5 = [22 x i8] c"Successful Connection\00"
@.str.6 = [21 x i8] c"Waiting for entry...\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [6 x i8] c"User:\00"
@stdin = global i32 0
@.str.9 = [10 x i8] c"Server:%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i16
%9 = alloca i8*
%10 = alloca [32 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 3
br i1 %16, label %17, label %24
17: ; preds = %14, %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
store i8* %27, i8** %9
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 3
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call zeroext i16 @atoi(i8* %33)
store i16 %34, i16* %8
br label %36
35: ; preds = %24
store i16 5000, i16* %8
br label %36
36: ; preds = %35, %30
%37 = load i32, i32* @AF_INET
%38 = load i32, i32* @SOCK_STREAM
%39 = call i32 @socket(i32 %37, i32 %38, i32 0)
store i32 %39, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @closeWithError(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i8*, i8** %9
%48 = call i32 @inet_addr(i8* %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
store i32 %48, i32* %50
%51 = load i16, i16* %8
%52 = call i32 @htons(i16 zeroext %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %52, i32* %53
store i32 12, i32* %11
%54 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* %6
%56 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%57 = load i32, i32* %11
%58 = call i32 @connect(i32 %55, %struct.sockaddr* %56, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %43
%61 = call i32 @closeWithError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %62
62: ; preds = %60, %43
%63 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%64 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %65
65: ; preds = %62, %65
%66 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%67 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%68 = load i32, i32* @stdin
%69 = call i32 @fgets(i8* %67, i32 32, i32 %68)
%70 = load i32, i32* %6
%71 = bitcast [32 x i8]* %10 to i8**
%72 = call i32 @write(i32 %70, i8** %71, i32 32)
%73 = load i32, i32* %6
%74 = bitcast [32 x i8]* %10 to i8**
%75 = call i32 @read(i32 %73, i8** %74, i32 32)
%76 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%77 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* %76)
br label %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [35 x i8] c"Uso: %s <Server IP> [<Echo Port>]\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [15 x i8] c"socket() fallo\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [22 x i8] c"Triying Connection...\00"
@.str.4 = [18 x i8] c"Connection failed\00"
@.str.5 = [22 x i8] c"Successful Connection\00"
@.str.6 = [21 x i8] c"Waiting for entry...\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [6 x i8] c"User:\00"
@stdin = global i32 0
@.str.9 = [10 x i8] c"Server:%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [32 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = add i32 %0, -4
%8 = icmp ult i32 %7, -2
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %1
%12 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = icmp eq i32 %0, 3
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call zeroext i16 @atoi(i8* %20)
br label %22
22: ; preds = %14, %18
%23 = phi i16 [ %21, %18 ], [ 5000, %14 ]
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = tail call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i32 @closeWithError(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %22
%31 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = call i32 @inet_addr(i8* %16)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %34, i32* %35
%36 = call i32 @htons(i16 zeroext %23)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %36, i32* %37
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i32 @connect(i32 %26, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %30
%43 = call i32 @closeWithError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %30
%45 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%47 = bitcast [32 x i8]* %4 to i8**
br label %48
48: ; preds = %48, %44
%49 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%50 = load i32, i32* @stdin
%51 = call i32 @fgets(i8* %6, i32 32, i32 %50)
%52 = call i32 @write(i32 %26, i8** %47, i32 32)
%53 = call i32 @read(i32 %26, i8** %47, i32 32)
%54 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* %6)
br label %48
}