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

/* Variables and functions */
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ close(int);
int envia_mensaje(int, char *, int, int);
int /*<<< orphan*/ error_fatal(char *);
scalar_t__ espera_recepcion(int /*<<< orphan*/, int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int inicia_socket_cliente(char *, char *);
char *lee_mensaje(int, int, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int s_cliente;
  int continuar = 1;
  int error = 0;
  char linea[1024];
  int len = 0;
  int len_recibido = 0;
  char *respuesta = NULL;
  signal(SIGPIPE, SIG_IGN);
  if (argc != 2) {
    fprintf(stderr, "Uso: invierte maquina\n");
    error = 1;
  } else {
    s_cliente = inicia_socket_cliente(argv[1], "cpr");
    printf("Conexion creada. \n"
           "Introduzca texto y pulse intro para enviar al servidor.\n"
           "Para terminar pulse intro sin escribir nada o espere"
           " %d segundos sin escribir nada.\n\n",
           20);
    while (continuar) {
      continuar = 0;
      len = 0;
      if (espera_recepcion(STDIN_FILENO, 20) &&
          fgets(linea, 1024, stdin) != NULL) {
        len = strlen(linea);
        if (len > 0 && linea[len - 1] == '\n') {
          linea[len - 1] = '\0';
          len--;
        }
      }
      if (len > 0 && envia_mensaje(s_cliente, linea, len, 20) == 1) {
        respuesta = lee_mensaje(s_cliente, 20, &len_recibido);
        if (NULL != respuesta) {
          write(STDOUT_FILENO, respuesta, len_recibido);
          printf("\n");
          continuar = 1;
          free(respuesta);
        } else
          printf("Respuesta incorrecta\n");
      }
    }
    if (close(s_cliente) < 0)
      error_fatal("close");
    printf("*** terminado ***\n");
  }
  return error;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x424(%rbp)
movl   $0x0,-0x428(%rbp)
movq   $0x0,-0x430(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
cmpl   $0x2,-0x8(%rbp)
je     1258 <main+0x88>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
call   10c0 <fprintf@plt>
movl   $0x1,-0x1c(%rbp)
jmp    141f <main+0x24f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdb0(%rip),%rsi
call   10d0 <inicia_socket_cliente@plt>
mov    %eax,-0x14(%rbp)
lea    0xda5(%rip),%rdi
mov    $0x14,%esi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     13f3 <main+0x223>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x424(%rbp)
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
mov    $0x14,%esi
call   10b0 <espera_recepcion@plt>
cmp    $0x0,%rax
je     1340 <main+0x170>
lea    -0x420(%rbp),%rdi
mov    0x2d18(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     1340 <main+0x170>
lea    -0x420(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    133b <main+0x16b>
mov    -0x424(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    133b <main+0x16b>
mov    -0x424(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x424(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x424(%rbp)
jmp    1340 <main+0x170>
cmpl   $0x0,-0x424(%rbp)
jle    13ee <main+0x21e>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
mov    $0x14,%ecx
call   10e0 <envia_mensaje@plt>
cmp    $0x1,%eax
jne    13ee <main+0x21e>
mov    -0x14(%rbp),%edi
mov    $0x14,%esi
lea    -0x428(%rbp),%rdx
call   1040 <lee_mensaje@plt>
mov    %rax,-0x430(%rbp)
xor    %eax,%eax
cmp    -0x430(%rbp),%rax
je     13db <main+0x20b>
mov    0x2c4f(%rip),%rax
mov    (%rax),%edi
mov    -0x430(%rbp),%rsi
mov    -0x428(%rbp),%edx
call   1050 <write@plt>
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x430(%rbp),%rdi
call   1030 <free@plt>
jmp    13e9 <main+0x219>
lea    0xcd9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13ee <main+0x21e>
jmp    1282 <main+0xb2>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
cmp    $0x0,%rax
jge    1411 <main+0x241>
lea    0xcc5(%rip),%rdi
call   10f0 <error_fatal@plt>
lea    0xcbf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,0xc(%rsp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   10c0 <signal@plt>
cmp    $0x2,%ebp
jne    1340 <main+0x150>
mov    0x8(%rbx),%rdi
lea    0xdde(%rip),%rsi
call   10f0 <inicia_socket_cliente@plt>
mov    %eax,%ebp
lea    0xdd4(%rip),%rdi
mov    $0x14,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2d5e(%rip),%r12
mov    (%r12),%edi
mov    $0x14,%esi
call   10d0 <espera_recepcion@plt>
test   %rax,%rax
je     1368 <main+0x178>
mov    0x2d68(%rip),%r13
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   10b0 <fgets@plt>
test   %rax,%rax
je     1368 <main+0x178>
mov    %r14,%rdi
call   1080 <strlen@plt>
test   %eax,%eax
jle    1368 <main+0x178>
mov    %eax,%edx
add    $0xffffffff,%eax
cmpb   $0xa,0x10(%rsp,%rax,1)
jne    12d5 <main+0xe5>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %eax,%edx
test   %eax,%eax
je     1368 <main+0x178>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x14,%ecx
call   1100 <envia_mensaje@plt>
cmp    $0x1,%eax
jne    1368 <main+0x178>
mov    %ebp,%edi
mov    $0x14,%esi
mov    %r15,%rdx
call   1050 <lee_mensaje@plt>
test   %rax,%rax
je     135c <main+0x16c>
mov    %rax,%rbx
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%edx
mov    %rbx,%rsi
call   1070 <write@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%r12),%edi
mov    $0x14,%esi
call   10d0 <espera_recepcion@plt>
test   %rax,%rax
jne    1290 <main+0xa0>
jmp    1368 <main+0x178>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
lea    0xcb0(%rip),%rsi
call   10e0 <fprintf@plt>
mov    $0x1,%eax
jmp    138e <main+0x19e>
lea    0xd70(%rip),%rdi
call   1060 <puts@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
test   %rax,%rax
jns    1380 <main+0x190>
lea    0xd40(%rip),%rdi
call   1110 <error_fatal@plt>
lea    0xd3a(%rip),%rdi
call   1060 <puts@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Uso: invierte maquina\0A\00"
@.str.1 = [4 x i8] c"cpr\00"
@.str.2 = [160 x i8] c"Conexion creada. \0AIntroduzca texto y pulse intro para enviar al servidor.\0APara terminar pulse intro sin escribir nada o espere %d segundos sin escribir nada.\0A\0A\00"
@STDIN_FILENO = global i32 0
@stdin = global i32 0
@STDOUT_FILENO = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [22 x i8] c"Respuesta incorrecta\0A\00"
@.str.5 = [6 x i8] c"close\00"
@.str.6 = [19 x i8] c"*** terminado ***\0A\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 [1024 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
store i32 0, i32* %8
store i32 0, i32* %10
store i32 0, i32* %11
store i8* null, i8** %12
%13 = load i32, i32* @SIGPIPE
%14 = load i32, i32* @SIG_IGN
%15 = call i32 @signal(i32 %13, i32 %14)
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %8
br label %94
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @inicia_socket_cliente(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32 %25, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.2, i64 0, i64 0), i32 20)
br label %27
27: ; preds = %85, %21
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %86
30: ; preds = %27
store i32 0, i32* %7
store i32 0, i32* %10
%31 = load i32, i32* @STDIN_FILENO
%32 = call i64 @espera_recepcion(i32 %31, i32 20)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %60
34: ; preds = %30
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%36 = load i32, i32* @stdin
%37 = call i32* @fgets(i8* %35, i32 1024, i32 %36)
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %60
39: ; preds = %34
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %59
44: ; preds = %39
%45 = load i32, i32* %10
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 10
br i1 %51, label %52, label %59
52: ; preds = %44
%53 = load i32, i32* %10
%54 = sub nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %55
store i8 0, i8* %56
%57 = load i32, i32* %10
%58 = add nsw i32 %57, -1
store i32 %58, i32* %10
br label %59
59: ; preds = %52, %44, %39
br label %60
60: ; preds = %59, %34, %30
%61 = load i32, i32* %10
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %85
63: ; preds = %60
%64 = load i32, i32* %6
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%66 = load i32, i32* %10
%67 = call i32 @envia_mensaje(i32 %64, i8* %65, i32 %66, i32 20)
%68 = icmp eq i32 %67, 1
br i1 %68, label %69, label %85
69: ; preds = %63
%70 = load i32, i32* %6
%71 = call i8* @lee_mensaje(i32 %70, i32 20, i32* %11)
store i8* %71, i8** %12
%72 = load i8*, i8** %12
%73 = icmp ne i8* null, %72
br i1 %73, label %74, label %82
74: ; preds = %69
%75 = load i32, i32* @STDOUT_FILENO
%76 = load i8*, i8** %12
%77 = load i32, i32* %11
%78 = call i32 @write(i32 %75, i8* %76, i32 %77)
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %7
%80 = load i8*, i8** %12
%81 = call i32 @free(i8* %80)
br label %84
82: ; preds = %69
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %84
84: ; preds = %82, %74
br label %85
85: ; preds = %84, %63, %60
br label %27
86: ; preds = %27
%87 = load i32, i32* %6
%88 = call i64 @close(i32 %87)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %86
%91 = call i32 @error_fatal(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %92
92: ; preds = %90, %86
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %94
94: ; preds = %92, %18
%95 = load i32, i32* %8
ret i32 %95
}

LLVM IR (-O2):
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Uso: invierte maquina\0A\00"
@.str.1 = [4 x i8] c"cpr\00"
@.str.2 = [160 x i8] c"Conexion creada. \0AIntroduzca texto y pulse intro para enviar al servidor.\0APara terminar pulse intro sin escribir nada o espere %d segundos sin escribir nada.\0A\0A\00"
@STDIN_FILENO = global i32 0
@stdin = global i32 0
@STDOUT_FILENO = global i32 0
@.str.5 = [6 x i8] c"close\00"
@str = [18 x i8] c"*** terminado ***\00"
@str.7 = [21 x i8] c"Respuesta incorrecta\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = load i32, i32* @SIGPIPE
%8 = load i32, i32* @SIG_IGN
%9 = tail call i32 @signal(i32 %7, i32 %8)
%10 = icmp eq i32 %0, 2
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %62
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @inicia_socket_cliente(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([160 x i8], [160 x i8]* @.str.2, i64 0, i64 0), i32 20)
%19 = load i32, i32* @STDIN_FILENO
%20 = call i64 @espera_recepcion(i32 %19, i32 20)
%21 = icmp eq i64 %20, 0
br i1 %21, label %55, label %22
22: ; preds = %14, %46
%23 = load i32, i32* @stdin
%24 = call i32* @fgets(i8* %5, i32 1024, i32 %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %55, label %26
26: ; preds = %22
%27 = call i32 @strlen(i8* %5)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %55
29: ; preds = %26
%30 = add nsw i32 %27, -1
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 10
br i1 %34, label %35, label %37
35: ; preds = %29
store i8 0, i8* %32
%36 = icmp eq i32 %30, 0
br i1 %36, label %55, label %37
37: ; preds = %29, %35
%38 = phi i32 [ %30, %35 ], [ %27, %29 ]
%39 = call i32 @envia_mensaje(i32 %17, i8* %5, i32 %38, i32 20)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %55
41: ; preds = %37
%42 = call i8* @lee_mensaje(i32 %17, i32 20, i32* %4)
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0))
br label %55
46: ; preds = %41
%47 = load i32, i32* @STDOUT_FILENO
%48 = load i32, i32* %4
%49 = call i32 @write(i32 %47, i8* %42, i32 %48)
%50 = call i32 @putchar(i32 10)
%51 = call i32 @free(i8* %42)
%52 = load i32, i32* @STDIN_FILENO
%53 = call i64 @espera_recepcion(i32 %52, i32 20)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %22
55: ; preds = %35, %37, %26, %22, %46, %14, %44
%56 = call i64 @close(i32 %17)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 @error_fatal(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %62
62: ; preds = %60, %11
%63 = phi i32 [ 1, %11 ], [ 0, %60 ]
ret i32 %63
}