#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 sock_addr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ compat_warnx(char *, char *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
struct sock_addr **sock_resolve_host(char *, char *);
struct sock_addr **sock_resolve_ipv4(char *, long);
struct sock_addr **sock_resolve_ipv6(char *, long);
struct sock_addr **sock_resolve_unix(char const *);
char *strdup(char const *);
int strlen(char *);
char *strrchr(char *, char);
long strtol(char *, int /*<<< orphan*/ *, int);
struct sock_addr **sock_resolve(const char *addr) {
  struct sock_addr **res;
  char *s;
  char *ports;
  char *ips;
  long p;
  if (addr[0] == '/') {
    res = sock_resolve_unix(addr);
    goto done;
  }
  if ((s = strdup(addr)) == NULL)
    goto err0;
  if ((ports = strrchr(s, ':')) == NULL) {
    do {
      ;
      compat_warnx("Address must contain port number: %s", s);
      errno = 0;
    } while (0);
    goto err1;
  }
  *ports++ = '\0';
  if (s[0] != '[') {
    res = sock_resolve_host(s, ports);
    goto done1;
  }
  if (s[strlen(s) - 1] != ']') {
    do {
      ;
      compat_warnx("Invalid [IP address]: %s", s);
      errno = 0;
    } while (0);
    goto err1;
  }
  ips = &s[1];
  ips[strlen(ips) - 1] = '\0';
  p = strtol(ports, NULL, 10);
  if ((p <= 0) || (p >= 65536)) {
    do {
      ;
      compat_warnx("Invalid port number: %s", ports);
      errno = 0;
    } while (0);
    goto err1;
  }
  if (strrchr(ips, ':') != NULL)
    res = sock_resolve_ipv6(ips, p);
  else
    res = sock_resolve_ipv4(ips, p);
done1:
  free(s);
done:
  return (res);
err1:
  free(s);
err0:
  return (NULL);
}


x86 assembly (clang, -O0):
<sock_resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11ce <sock_resolve+0x2e>
mov    -0x10(%rbp),%rdi
call   1090 <sock_resolve_unix@plt>
mov    %rax,-0x18(%rbp)
jmp    1389 <sock_resolve+0x1e9>
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ea <sock_resolve+0x4a>
jmp    139f <sock_resolve+0x1ff>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    122e <sock_resolve+0x8e>
jmp    120b <sock_resolve+0x6b>
mov    -0x20(%rbp),%rsi
lea    0xdea(%rip),%rdi
call   10a0 <compat_warnx@plt>
mov    0x2dbe(%rip),%rax
movq   $0x0,(%rax)
jmp    1396 <sock_resolve+0x1f6>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5b,%eax
je     1266 <sock_resolve+0xc6>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <sock_resolve_host@plt>
mov    %rax,-0x18(%rbp)
jmp    1380 <sock_resolve+0x1e0>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5d,%eax
je     12b8 <sock_resolve+0x118>
jmp    1295 <sock_resolve+0xf5>
mov    -0x20(%rbp),%rsi
lea    0xd85(%rip),%rdi
call   10a0 <compat_warnx@plt>
mov    0x2d34(%rip),%rax
movq   $0x0,(%rax)
jmp    1396 <sock_resolve+0x1f6>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jle    1314 <sock_resolve+0x174>
cmpq   $0x10000,-0x38(%rbp)
jl     133c <sock_resolve+0x19c>
jmp    1319 <sock_resolve+0x179>
mov    -0x28(%rbp),%rsi
lea    0xd1a(%rip),%rdi
call   10a0 <compat_warnx@plt>
mov    0x2cb0(%rip),%rax
movq   $0x0,(%rax)
jmp    1396 <sock_resolve+0x1f6>
mov    -0x30(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
cmp    $0x0,%rax
je     136a <sock_resolve+0x1ca>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10b0 <sock_resolve_ipv6@plt>
mov    %rax,-0x18(%rbp)
jmp    137b <sock_resolve+0x1db>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1060 <sock_resolve_ipv4@plt>
mov    %rax,-0x18(%rbp)
jmp    1380 <sock_resolve+0x1e0>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13a7 <sock_resolve+0x207>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_resolve>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmpb   $0x2f,(%rdi)
jne    11bd <sock_resolve+0x1d>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1090 <sock_resolve_unix@plt>
call   10c0 <strdup@plt>
test   %rax,%rax
je     128a <sock_resolve+0xea>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1244 <sock_resolve+0xa4>
mov    %rax,%rbx
add    $0x1,%rbx
movb   $0x0,(%rax)
cmpb   $0x5b,(%r14)
jne    124d <sock_resolve+0xad>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x5d,-0x1(%rax,%r14,1)
jne    1265 <sock_resolve+0xc5>
mov    %r14,%r15
add    $0x1,%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,(%rax,%r14,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,%r12
add    $0xffffffffffff0000,%rax
cmp    $0xffffffffffff0000,%rax
ja     129b <sock_resolve+0xfb>
lea    0xdff(%rip),%rdi
mov    %rbx,%rsi
jmp    126f <sock_resolve+0xcf>
lea    0xdb5(%rip),%rdi
jmp    126c <sock_resolve+0xcc>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <sock_resolve_host@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
jmp    128c <sock_resolve+0xec>
lea    0xdb9(%rip),%rdi
mov    %r14,%rsi
call   10a0 <compat_warnx@plt>
mov    0x2d65(%rip),%rax
movq   $0x0,(%rax)
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
mov    $0x3a,%esi
call   1050 <strrchr@plt>
mov    %r15,%rdi
mov    %r12,%rsi
test   %rax,%rax
je     12ba <sock_resolve+0x11a>
call   10b0 <sock_resolve_ipv6@plt>
jmp    1258 <sock_resolve+0xb8>
call   1060 <sock_resolve_ipv4@plt>
jmp    1258 <sock_resolve+0xb8>

LLVM IR (-O0):
%struct.sock_addr = type { i32 }
@.str = [37 x i8] c"Address must contain port number: %s\00"
@errno = global i64 0
@.str.1 = [25 x i8] c"Invalid [IP address]: %s\00"
@.str.2 = [24 x i8] c"Invalid port number: %s\00"
define %struct.sock_addr** @sock_resolve(i8* %0) {
%2 = alloca %struct.sock_addr**
%3 = alloca i8*
%4 = alloca %struct.sock_addr**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 47
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call %struct.sock_addr** @sock_resolve_unix(i8* %15)
store %struct.sock_addr** %16, %struct.sock_addr*** %4
br label %95
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %5
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17
br label %100
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = call i8* @strrchr(i8* %23, i8 signext 58)
store i8* %24, i8** %6
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %31
26: ; preds = %22
br label %27
27: ; preds = %26
%28 = load i8*, i8** %5
%29 = call i32 @compat_warnx(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %28)
store i64 0, i64* @errno
br label %30
30: ; preds = %27
br label %97
31: ; preds = %22
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %6
store i8 0, i8* %32
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 91
br i1 %38, label %39, label %43
39: ; preds = %31
%40 = load i8*, i8** %5
%41 = load i8*, i8** %6
%42 = call %struct.sock_addr** @sock_resolve_host(i8* %40, i8* %41)
store %struct.sock_addr** %42, %struct.sock_addr*** %4
br label %92
43: ; preds = %31
%44 = load i8*, i8** %5
%45 = load i8*, i8** %5
%46 = call i32 @strlen(i8* %45)
%47 = sub nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 93
br i1 %52, label %53, label %58
53: ; preds = %43
br label %54
54: ; preds = %53
%55 = load i8*, i8** %5
%56 = call i32 @compat_warnx(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %55)
store i64 0, i64* @errno
br label %57
57: ; preds = %54
br label %97
58: ; preds = %43
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i64 1
store i8* %60, i8** %7
%61 = load i8*, i8** %7
%62 = load i8*, i8** %7
%63 = call i32 @strlen(i8* %62)
%64 = sub nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %61, i64 %65
store i8 0, i8* %66
%67 = load i8*, i8** %6
%68 = call i64 @strtol(i8* %67, i32* null, i32 10)
store i64 %68, i64* %8
%69 = load i64, i64* %8
%70 = icmp sle i64 %69, 0
br i1 %70, label %74, label %71
71: ; preds = %58
%72 = load i64, i64* %8
%73 = icmp sge i64 %72, 65536
br i1 %73, label %74, label %79
74: ; preds = %71, %58
br label %75
75: ; preds = %74
%76 = load i8*, i8** %6
%77 = call i32 @compat_warnx(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %76)
store i64 0, i64* @errno
br label %78
78: ; preds = %75
br label %97
79: ; preds = %71
%80 = load i8*, i8** %7
%81 = call i8* @strrchr(i8* %80, i8 signext 58)
%82 = icmp ne i8* %81, null
br i1 %82, label %83, label %87
83: ; preds = %79
%84 = load i8*, i8** %7
%85 = load i64, i64* %8
%86 = call %struct.sock_addr** @sock_resolve_ipv6(i8* %84, i64 %85)
store %struct.sock_addr** %86, %struct.sock_addr*** %4
br label %91
87: ; preds = %79
%88 = load i8*, i8** %7
%89 = load i64, i64* %8
%90 = call %struct.sock_addr** @sock_resolve_ipv4(i8* %88, i64 %89)
store %struct.sock_addr** %90, %struct.sock_addr*** %4
br label %91
91: ; preds = %87, %83
br label %92
92: ; preds = %91, %39
%93 = load i8*, i8** %5
%94 = call i32 @free(i8* %93)
br label %95
95: ; preds = %92, %14
%96 = load %struct.sock_addr**, %struct.sock_addr*** %4
store %struct.sock_addr** %96, %struct.sock_addr*** %2
br label %101
97: ; preds = %78, %57, %30
%98 = load i8*, i8** %5
%99 = call i32 @free(i8* %98)
br label %100
100: ; preds = %97, %21
store %struct.sock_addr** null, %struct.sock_addr*** %2
br label %101
101: ; preds = %100, %95
%102 = load %struct.sock_addr**, %struct.sock_addr*** %2
ret %struct.sock_addr** %102
}

LLVM IR (-O2):
%struct.sock_addr = type { i32 }
@.str = [37 x i8] c"Address must contain port number: %s\00"
@errno = global i64 0
@.str.1 = [25 x i8] c"Invalid [IP address]: %s\00"
@.str.2 = [24 x i8] c"Invalid port number: %s\00"
define %struct.sock_addr** @sock_resolve(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 47
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call %struct.sock_addr** @sock_resolve_unix(i8* %0)
br label %52
6: ; preds = %1
%7 = tail call i8* @strdup(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %52, label %9
9: ; preds = %6
%10 = tail call i8* @strrchr(i8* %7, i8 signext 58)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @compat_warnx(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %50
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %10
%16 = load i8, i8* %7
%17 = icmp eq i8 %16, 91
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = tail call %struct.sock_addr** @sock_resolve_host(i8* %7, i8* %15)
br label %47
20: ; preds = %14
%21 = tail call i32 @strlen(i8* %7)
%22 = add nsw i32 %21, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %7, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 93
br i1 %26, label %29, label %27
27: ; preds = %20
%28 = tail call i32 @compat_warnx(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %50
29: ; preds = %20
%30 = getelementptr inbounds i8, i8* %7, i64 1
%31 = tail call i32 @strlen(i8* %30)
%32 = add nsw i32 %31, -1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = tail call i64 @strtol(i8* nocapture %15, i32* null, i32 10)
%36 = add i64 %35, -65536
%37 = icmp ult i64 %36, -65535
br i1 %37, label %38, label %40
38: ; preds = %29
%39 = tail call i32 @compat_warnx(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %50
40: ; preds = %29
%41 = tail call i8* @strrchr(i8* %30, i8 signext 58)
%42 = icmp eq i8* %41, null
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = tail call %struct.sock_addr** @sock_resolve_ipv6(i8* %30, i64 %35)
br label %47
45: ; preds = %40
%46 = tail call %struct.sock_addr** @sock_resolve_ipv4(i8* %30, i64 %35)
br label %47
47: ; preds = %43, %45, %18
%48 = phi %struct.sock_addr** [ %19, %18 ], [ %44, %43 ], [ %46, %45 ]
%49 = tail call i32 @free(i8* %7)
br label %52
50: ; preds = %38, %27, %12
store i64 0, i64* @errno
%51 = tail call i32 @free(i8* %7)
br label %52
52: ; preds = %50, %6, %4, %47
%53 = phi %struct.sock_addr** [ %5, %4 ], [ %48, %47 ], [ null, %6 ], [ null, %50 ]
ret %struct.sock_addr** %53
}