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

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int STDIN_FILENO;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char **argv) {
  int sd, leido, escrito;
  char buffer[128];
  struct sockaddr_in dir = {};
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("socket");
    return -1;
  }
  dir.sin_family = AF_INET;
  dir.sin_port = htons(2500);
  if (!inet_aton(argv[1], &dir.sin_addr)) {
    printf("dirreccion invalida\n");
    return -1;
  }
  if (connect(sd, (struct sockaddr *)&dir, sizeof dir) < 0) {
    perror("connect");
    return -1;
  }
  if (fork()) {
    while ((leido = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
      buffer[leido] = 0;
      if ((escrito = write(sd, buffer, sizeof(buffer))) <= 0) {
        perror("write");
        printf("ENTRE!!!!!!");
        break;
      }
    }
    if (leido < 0)
      perror("read");
    return 0;
  }
  while ((leido = read(sd, buffer, sizeof(buffer))) > 0) {
    buffer[leido] = 0;
    printf("Server says: %s", buffer);
  }
  printf("salio");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120a <main+0x6a>
lea    0xe07(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <main+0x204>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa8(%rbp)
mov    $0x9c4,%edi
call   1040 <htons@plt>
mov    %eax,-0xac(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0xb0(%rbp),%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%eax
jne    1260 <main+0xc0>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <main+0x204>
mov    -0x14(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1296 <main+0xf6>
lea    0xd97(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <main+0x204>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
je     1344 <main+0x1a4>
jmp    12ac <main+0x10c>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1322 <main+0x182>
movslq -0x18(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jg     131d <main+0x17d>
lea    0xd1f(%rip),%rdi
call   1090 <perror@plt>
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1322 <main+0x182>
jmp    12ac <main+0x10c>
cmpl   $0x0,-0x18(%rbp)
jge    1338 <main+0x198>
lea    0xd03(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a4 <main+0x204>
jmp    1349 <main+0x1a9>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    138f <main+0x1ef>
movslq -0x18(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rsi
lea    0xcb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1349 <main+0x1a9>
lea    0xcb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %rsi,%rbx
movl   $0x0,(%rsp)
mov    0x2e2c(%rip),%r14
mov    (%r14),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12f9 <main+0x159>
mov    %eax,%ebp
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    $0x9c4,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1080 <inet_aton@plt>
test   %eax,%eax
je     1273 <main+0xd3>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     1302 <main+0x162>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     129c <main+0xfc>
mov    0x2da7(%rip),%r14
lea    0x10(%rsp),%rbx
xchg   %ax,%ax
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1284 <main+0xe4>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1040 <write@plt>
test   %eax,%eax
jg     1230 <main+0x90>
lea    0xdaa(%rip),%rdi
call   1090 <perror@plt>
lea    0xda4(%rip),%rdi
jmp    12ee <main+0x14e>
lea    0xdc2(%rip),%rdi
call   1030 <puts@plt>
jmp    130e <main+0x16e>
xor    %ebx,%ebx
test   %eax,%eax
jns    1313 <main+0x173>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
jmp    1313 <main+0x173>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x80,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    12e7 <main+0x147>
lea    0xd6e(%rip),%r14
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     12c0 <main+0x120>
lea    0xd48(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1313 <main+0x173>
lea    0xd00(%rip),%rdi
jmp    1309 <main+0x169>
lea    0xcfe(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [21 x i8] c"dirreccion invalida\0A\00"
@.str.2 = [8 x i8] c"connect\00"
@STDIN_FILENO = global i32 0
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [12 x i8] c"ENTRE!!!!!!\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [16 x i8] c"Server says: %s\00"
@.str.7 = [6 x i8] c"salio\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [128 x i8]
%10 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast %struct.sockaddr_in* %10 to i8*
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %80
19: ; preds = %2
%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 = call i32 @htons(i32 2500)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%28 = call i32 @inet_aton(i8* %26, i32* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %19
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %80
32: ; preds = %19
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%35 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %80
39: ; preds = %32
%40 = call i64 (...) @fork()
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %66
42: ; preds = %39
br label %43
43: ; preds = %59, %42
%44 = load i32, i32* @STDIN_FILENO
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 128)
store i32 %46, i32* %7
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %60
48: ; preds = %43
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 %50
store i8 0, i8* %51
%52 = load i32, i32* %6
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%54 = call i32 @write(i32 %52, i8* %53, i32 128)
store i32 %54, i32* %8
%55 = icmp sle i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %48
%57 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %60
59: ; preds = %48
br label %43
60: ; preds = %56, %43
%61 = load i32, i32* %7
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
store i32 0, i32* %3
br label %80
66: ; preds = %39
br label %67
67: ; preds = %72, %66
%68 = load i32, i32* %6
%69 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 128)
store i32 %70, i32* %7
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %78
72: ; preds = %67
%73 = load i32, i32* %7
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 %74
store i8 0, i8* %75
%76 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %76)
br label %67
78: ; preds = %67
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %80
80: ; preds = %78, %65, %37, %30, %17
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@STDIN_FILENO = global i32 0
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [12 x i8] c"ENTRE!!!!!!\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [16 x i8] c"Server says: %s\00"
@.str.7 = [6 x i8] c"salio\00"
@str = [20 x i8] c"dirreccion invalida\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [128 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 0, i32* %7
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %63
14: ; preds = %2
%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 = tail call i32 @htons(i32 2500)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%22 = call i32 @inet_aton(i8* %20, i32* %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %63
26: ; preds = %14
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @connect(i32 %10, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %63
32: ; preds = %26
%33 = call i64 (...) @fork()
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @read(i32 %10, i8* %5, i32 128)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %54, label %61
38: ; preds = %32, %42
%39 = load i32, i32* @STDIN_FILENO
%40 = call i32 @read(i32 %39, i8* %5, i32 128)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %38
%43 = zext i32 %40 to i64
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %43
store i8 0, i8* %44
%45 = call i32 @write(i32 %10, i8* %5, i32 128)
%46 = icmp slt i32 %45, 1
br i1 %46, label %47, label %38
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %63
50: ; preds = %38
%51 = icmp slt i32 %40, 0
br i1 %51, label %52, label %63
52: ; preds = %50
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %63
54: ; preds = %35, %54
%55 = phi i32 [ %59, %54 ], [ %36, %35 ]
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %56
store i8 0, i8* %57
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %5)
%59 = call i32 @read(i32 %10, i8* %5, i32 128)
%60 = icmp sgt i32 %59, 0
br i1 %60, label %54, label %61
61: ; preds = %54, %35
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %63
63: ; preds = %47, %50, %52, %61, %30, %24, %12
%64 = phi i32 [ -1, %12 ], [ -1, %30 ], [ 0, %61 ], [ -1, %24 ], [ 0, %52 ], [ 0, %50 ], [ 0, %47 ]
ret i32 %64
}