#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*/ EINVAL;
int d_find(char *, char *, char *);
int /*<<< orphan*/ errno;
int r_open(char *, int, char *, char *);
int sscanf(char *, char *, char *, int *);
scalar_t__ strcmp(char *, char *);
int d_open(char *ip_y_puerto, char *nombre, char *modo) {
  int fd;
  char buffer[255] = "";
  char ipRem[255] = "";
  int puertoRem;
  if (ip_y_puerto == NULL || nombre == NULL || modo == NULL ||
      sscanf(ip_y_puerto, "%[^:]:%d", ipRem, &puertoRem) != 2) {
    errno = EINVAL;
    return -1;
  }
  if (d_find(nombre, ip_y_puerto, buffer) == -1) {
    if (strcmp(modo, "r+") == 0 || modo[0] == 'w')
      sscanf(ip_y_puerto, "%[^:]:%d", ipRem, &puertoRem);
    else
      return -1;
  } else
    sscanf(buffer, "%[^:]:%d", ipRem, &puertoRem);
  fd = r_open(ipRem, puertoRem, nombre, modo);
  if (fd == -1) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<d_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1040 <memset@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1040 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
je     11d5 <d_open+0x85>
cmpq   $0x0,-0x18(%rbp)
je     11d5 <d_open+0x85>
cmpq   $0x0,-0x20(%rbp)
je     11d5 <d_open+0x85>
mov    -0x10(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xe40(%rip),%rsi
lea    -0x234(%rbp),%rcx
call   1070 <sscanf@plt>
cmp    $0x2,%eax
je     11f3 <d_open+0xa3>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfb(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <d_open+0x178>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x130(%rbp),%rdx
call   1060 <d_find@plt>
cmp    $0xffffffff,%eax
jne    126e <d_open+0x11e>
mov    -0x20(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     123a <d_open+0xea>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    125d <d_open+0x10d>
mov    -0x10(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xdb4(%rip),%rsi
lea    -0x234(%rbp),%rcx
call   1070 <sscanf@plt>
jmp    1269 <d_open+0x119>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <d_open+0x178>
jmp    128f <d_open+0x13f>
lea    -0x130(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xd7d(%rip),%rsi
lea    -0x234(%rbp),%rcx
call   1070 <sscanf@plt>
lea    -0x230(%rbp),%rdi
mov    -0x234(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <r_open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12c2 <d_open+0x172>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <d_open+0x178>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<d_open>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x218,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0x1ff(%rsp)
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movups %xmm0,0xff(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
test   %rdi,%rdi
je     12ba <d_open+0x17a>
mov    %rsi,%r15
test   %rsi,%rsi
je     12ba <d_open+0x17a>
mov    %rdx,%r14
test   %rdx,%rdx
je     12ba <d_open+0x17a>
mov    %rdi,%rbx
lea    0xda5(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1060 <sscanf@plt>
cmp    $0x2,%eax
jne    12ba <d_open+0x17a>
lea    0x110(%rsp),%r12
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1050 <d_find@plt>
cmp    $0xffffffff,%eax
je     12e0 <d_open+0x1a0>
lea    0xd6f(%rip),%rsi
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%rcx
mov    %r12,%rdi
mov    %rbx,%rdx
call   1060 <sscanf@plt>
mov    0xc(%rsp),%esi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <r_open@plt>
jmp    12d1 <d_open+0x191>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    0x2d16(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd22(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1304 <d_open+0x1c4>
mov    $0xffffffff,%eax
cmpb   $0x77,(%r14)
mov    %rbx,%r12
je     128a <d_open+0x14a>
jmp    12d1 <d_open+0x191>
mov    %rbx,%r12
jmp    128a <d_open+0x14a>

LLVM IR (-O0):
@.str = [9 x i8] c"%[^:]:%d\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.1 = [3 x i8] c"r+\00"
define i32 @d_open(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [255 x i8]
%10 = alloca [255 x i8]
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = bitcast [255 x i8]* %9 to i8*
%13 = bitcast [255 x i8]* %10 to i8*
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %27, label %16
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, null
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%25 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %24, i32* %11)
%26 = icmp ne i32 %25, 2
br i1 %26, label %27, label %29
27: ; preds = %22, %19, %16, %3
%28 = load i32, i32* @EINVAL
store i32 %28, i32* @errno
store i32 -1, i32* %4
br label %66
29: ; preds = %22
%30 = load i8*, i8** %6
%31 = load i8*, i8** %5
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%33 = call i32 @d_find(i8* %30, i8* %31, i8* %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %51
35: ; preds = %29
%36 = load i8*, i8** %7
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %35
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 119
br i1 %44, label %45, label %49
45: ; preds = %39, %35
%46 = load i8*, i8** %5
%47 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%48 = call i32 @sscanf(i8* %46, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %47, i32* %11)
br label %50
49: ; preds = %39
store i32 -1, i32* %4
br label %66
50: ; preds = %45
br label %55
51: ; preds = %29
%52 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%53 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%54 = call i32 @sscanf(i8* %52, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %53, i32* %11)
br label %55
55: ; preds = %51, %50
%56 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%57 = load i32, i32* %11
%58 = load i8*, i8** %6
%59 = load i8*, i8** %7
%60 = call i32 @r_open(i8* %56, i32 %57, i8* %58, i8* %59)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %64
63: ; preds = %55
store i32 -1, i32* %4
br label %66
64: ; preds = %55
%65 = load i32, i32* %8
store i32 %65, i32* %4
br label %66
66: ; preds = %64, %63, %49, %27
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@.str = [9 x i8] c"%[^:]:%d\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.1 = [3 x i8] c"r+\00"
define i32 @d_open(i8* %0, i8* %1, i8* %2) {
%4 = alloca [255 x i8]
%5 = alloca [255 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%9 = bitcast i32* %6 to i8*
%10 = icmp eq i8* %0, null
%11 = icmp eq i8* %1, null
%12 = or i1 %10, %11
%13 = icmp eq i8* %2, null
%14 = or i1 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %8, i32* %6)
%17 = icmp eq i32 %16, 2
br i1 %17, label %20, label %18
18: ; preds = %15, %3
%19 = load i32, i32* @EINVAL
store i32 %19, i32* @errno
br label %34
20: ; preds = %15
%21 = call i32 @d_find(i8* %1, i8* %0, i8* %7)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i8, i8* %2
%28 = icmp eq i8 %27, 119
br i1 %28, label %29, label %34
29: ; preds = %20, %23, %26
%30 = phi i8* [ %0, %26 ], [ %0, %23 ], [ %7, %20 ]
%31 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %8, i32* %6)
%32 = load i32, i32* %6
%33 = call i32 @r_open(i8* %8, i32 %32, i8* %1, i8* %2)
br label %34
34: ; preds = %29, %26, %18
%35 = phi i32 [ -1, %18 ], [ -1, %26 ], [ %33, %29 ]
ret i32 %35
}