#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 */
typedef int /*<<< orphan*/ ruta;

/* Variables and functions */
int EINVAL;
int conecta(char *, int);
int envia(int, char *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int pollIn(int, int);
int recibe(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int r_open(char *ip, int port, char *ruta, char *modo) {
  int rc, r_errno, socket;
  char cadena[300] = "";
  if ((ruta == NULL) || (sizeof(ruta) >= 255)) {
    errno = EINVAL;
    fprintf(stderr, "r_open(): ruta incorrecta\n");
    return -1;
  }
  if ((ip == NULL) || (modo == NULL)) {
    errno = EINVAL;
    fprintf(stderr, "r_open():  parametros incorrectos\n");
    return -1;
  }
  if ((socket = conecta(ip, port)) == -1) {
    perror("r_open(): conecta()");
    return -1;
  }
  sprintf(cadena, "r_open %s %s\n", ruta, modo);
  if (envia(socket, cadena, strlen(cadena)) == -1) {
    perror("r_open(): envia()");
    return -1;
  }
  rc = pollIn(socket, 3000);
  if (rc == 1) {
    fprintf(stderr, "r_open(): pollIn(): timeout\n");
    return -1;
  }
  rc = 0;
  while (rc == 0) {
    rc = recibe(socket, cadena, 300);
    if (rc == -1) {
      perror("r_open(): recibe()");
      return -1;
    }
  }
  sscanf(cadena, "%d", &r_errno);
  if (r_errno != 0) {
    errno = r_errno;
    return -1;
  }
  return socket;
}


x86 assembly (clang, -O0):
<r_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x160(%rbp),%rdi
xor    %esi,%esi
mov    $0x12c,%edx
call   1060 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
jne    120b <r_open+0x6b>
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
mov    0x2df0(%rip),%rax
mov    %ecx,(%rax)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c0 <r_open+0x220>
cmpq   $0x0,-0x10(%rbp)
je     1221 <r_open+0x81>
cmpq   $0x0,-0x28(%rbp)
jne    1254 <r_open+0xb4>
mov    0x2da8(%rip),%rax
mov    (%rax),%ecx
mov    0x2da7(%rip),%rax
mov    %ecx,(%rax)
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c0 <r_open+0x220>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10c0 <conecta@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1284 <r_open+0xe4>
lea    0xdcb(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c0 <r_open+0x220>
lea    -0x160(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdb8(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x16c(%rbp)
lea    -0x160(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    -0x160(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x16c(%rbp),%edi
mov    -0x168(%rbp),%rsi
mov    %eax,%edx
call   1040 <envia@plt>
cmp    $0xffffffff,%eax
jne    12f7 <r_open+0x157>
lea    0xd7a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c0 <r_open+0x220>
mov    -0x34(%rbp),%edi
mov    $0xbb8,%esi
call   1070 <pollIn@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jne    1332 <r_open+0x192>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c0 <r_open+0x220>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1381 <r_open+0x1e1>
mov    -0x34(%rbp),%edi
lea    -0x160(%rbp),%rsi
mov    $0x12c,%edx
call   1030 <recibe@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    137c <r_open+0x1dc>
lea    0xd24(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c0 <r_open+0x220>
jmp    1339 <r_open+0x199>
lea    -0x160(%rbp),%rdi
lea    0xd13(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1090 <sscanf@plt>
cmpl   $0x0,-0x30(%rbp)
je     13ba <r_open+0x21a>
mov    -0x30(%rbp),%ecx
mov    0x2c2c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13c0 <r_open+0x220>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<r_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x148,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
mov    $0x12c,%edx
xor    %esi,%esi
call   1060 <memset@plt>
test   %r15,%r15
je     125b <r_open+0xbb>
test   %rbx,%rbx
je     1287 <r_open+0xe7>
test   %r14,%r14
je     1287 <r_open+0xe7>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10c0 <conecta@plt>
cmp    $0xffffffff,%eax
je     1322 <r_open+0x182>
mov    %eax,%ebx
lea    0xe52(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   10b0 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <envia@plt>
cmp    $0xffffffff,%eax
je     1330 <r_open+0x190>
mov    %ebx,%edi
mov    $0xbb8,%esi
call   1070 <pollIn@plt>
cmp    $0x1,%eax
jne    12b0 <r_open+0x110>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1080 <fprintf@plt>
jmp    130d <r_open+0x16d>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    0x2d6d(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
call   1080 <fprintf@plt>
jmp    130d <r_open+0x16d>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    0x2d41(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd72(%rip),%rsi
call   1080 <fprintf@plt>
jmp    130d <r_open+0x16d>
xor    %eax,%eax
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
test   %eax,%eax
jne    12e6 <r_open+0x146>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x12c,%edx
call   1030 <recibe@plt>
cmp    $0xffffffff,%eax
jne    12c0 <r_open+0x120>
lea    0xdb0(%rip),%rdi
call   10a0 <perror@plt>
jmp    130d <r_open+0x16d>
lea    0xdb5(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1090 <sscanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
je     1312 <r_open+0x172>
mov    0x2ccd(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd15(%rip),%rdi
call   10a0 <perror@plt>
jmp    130d <r_open+0x16d>
lea    0xd29(%rip),%rdi
call   10a0 <perror@plt>
jmp    130d <r_open+0x16d>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"r_open(): ruta incorrecta\0A\00"
@.str.1 = [35 x i8] c"r_open(): parametros incorrectos\0A\00"
@.str.2 = [20 x i8] c"r_open(): conecta()\00"
@.str.3 = [14 x i8] c"r_open %s %s\0A\00"
@.str.4 = [18 x i8] c"r_open(): envia()\00"
@.str.5 = [29 x i8] c"r_open(): pollIn(): timeout\0A\00"
@.str.6 = [19 x i8] c"r_open(): recibe()\00"
@.str.7 = [3 x i8] c"%d\00"
define i32 @r_open(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [300 x i8]
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%14 = bitcast [300 x i8]* %13 to i8*
%15 = load i8*, i8** %8
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %81
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i8*, i8** %9
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %31
27: ; preds = %24, %21
%28 = load i32, i32* @EINVAL
store i32 %28, i32* @errno
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %81
31: ; preds = %24
%32 = load i8*, i8** %6
%33 = load i32, i32* %7
%34 = call i32 @conecta(i8* %32, i32 %33)
store i32 %34, i32* %12
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %81
38: ; preds = %31
%39 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%40 = load i8*, i8** %8
%41 = load i8*, i8** %9
%42 = call i32 @sprintf(i8* %39, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %40, i8* %41)
%43 = load i32, i32* %12
%44 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%45 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @envia(i32 %43, i8* %44, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %38
%50 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %5
br label %81
51: ; preds = %38
%52 = load i32, i32* %12
%53 = call i32 @pollIn(i32 %52, i32 3000)
store i32 %53, i32* %10
%54 = load i32, i32* %10
%55 = icmp eq i32 %54, 1
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %5
br label %81
59: ; preds = %51
store i32 0, i32* %10
br label %60
60: ; preds = %71, %59
%61 = load i32, i32* %10
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %60
%64 = load i32, i32* %12
%65 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%66 = call i32 @recibe(i32 %64, i8* %65, i32 300)
store i32 %66, i32* %10
%67 = load i32, i32* %10
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %71
69: ; preds = %63
%70 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %5
br label %81
71: ; preds = %63
br label %60
72: ; preds = %60
%73 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%74 = call i32 @sscanf(i8* %73, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32* %11)
%75 = load i32, i32* %11
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %72
%78 = load i32, i32* %11
store i32 %78, i32* @errno
store i32 -1, i32* %5
br label %81
79: ; preds = %72
%80 = load i32, i32* %12
store i32 %80, i32* %5
br label %81
81: ; preds = %79, %77, %69, %56, %49, %36, %27, %17
%82 = load i32, i32* %5
ret i32 %82
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"r_open(): ruta incorrecta\0A\00"
@.str.1 = [35 x i8] c"r_open(): parametros incorrectos\0A\00"
@.str.2 = [20 x i8] c"r_open(): conecta()\00"
@.str.3 = [14 x i8] c"r_open %s %s\0A\00"
@.str.4 = [18 x i8] c"r_open(): envia()\00"
@.str.5 = [29 x i8] c"r_open(): pollIn(): timeout\0A\00"
@.str.6 = [19 x i8] c"r_open(): recibe()\00"
@.str.7 = [3 x i8] c"%d\00"
define i32 @r_open(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca [300 x i8]
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%9 = icmp eq i8* %2, null
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %53
14: ; preds = %4
%15 = icmp eq i8* %0, null
%16 = icmp eq i8* %3, null
%17 = or i1 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @EINVAL
store i32 %19, i32* @errno
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %53
22: ; preds = %14
%23 = tail call i32 @conecta(i8* %0, i32 %1)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %53
27: ; preds = %22
%28 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %2, i8* %3)
%29 = call i32 @strlen(i8* %8)
%30 = call i32 @envia(i32 %23, i8* %8, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %53
34: ; preds = %27
%35 = call i32 @pollIn(i32 %23, i32 3000)
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %53
40: ; preds = %34, %43
%41 = phi i32 [ %44, %43 ], [ 0, %34 ]
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = call i32 @recibe(i32 %23, i8* %8, i32 300)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %40
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %53
48: ; preds = %40
%49 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32* %5)
%50 = load i32, i32* %5
%51 = icmp eq i32 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %48
store i32 %50, i32* @errno
br label %53
53: ; preds = %48, %52, %46, %37, %32, %25, %18, %10
%54 = phi i32 [ -1, %10 ], [ -1, %18 ], [ -1, %25 ], [ -1, %32 ], [ -1, %37 ], [ -1, %46 ], [ -1, %52 ], [ %23, %48 ]
ret i32 %54
}