#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 addrinfo {
  scalar_t__ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
scalar_t__ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
char *strchr(char *, char);
char *strdup(char const *);
struct addrinfo *_nxweb_resolve_host(const char *host_and_port, int passive) {
  char *host = strdup(host_and_port);
  char *port = strchr(host, ':');
  if (port)
    *port++ = '\0';
  else
    port = "80";
  struct addrinfo hints, *res, *res_first, *res_last;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = passive ? AI_PASSIVE : 0;
  if (getaddrinfo((!*host || *host == '*') ? 0 : host, port, &hints,
                  &res_first))
    goto ERR1;
  res_last = 0;
  for (res = res_first; res; res = res->ai_next) {
    if (res->ai_family == AF_INET)
      break;
    res_last = res;
  }
  if (!res)
    goto ERR2;
  if (res != res_first) {
    res_last->ai_next = res->ai_next;
    freeaddrinfo(res_first);
    res->ai_next = 0;
  }
  free(host);
  return res;
ERR2:
  freeaddrinfo(res_first);
ERR1:
  free(host);
  return 0;
}


x86 assembly (clang, -O0):
<_nxweb_resolve_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b0 <_nxweb_resolve_host+0x50>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
jmp    11bb <_nxweb_resolve_host+0x5b>
lea    0xe49(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1200 <_nxweb_resolve_host+0xa0>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
jmp    120a <_nxweb_resolve_host+0xaa>
xor    %eax,%eax
mov    %eax,-0x5c(%rbp)
jmp    120a <_nxweb_resolve_host+0xaa>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     122d <_nxweb_resolve_host+0xcd>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
jne    1238 <_nxweb_resolve_host+0xd8>
xor    %eax,%eax
mov    %rax,-0x68(%rbp)
jmp    1240 <_nxweb_resolve_host+0xe0>
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1264 <_nxweb_resolve_host+0x104>
jmp    1316 <_nxweb_resolve_host+0x1b6>
movq   $0x0,-0x58(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12b4 <_nxweb_resolve_host+0x154>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
jne    129b <_nxweb_resolve_host+0x13b>
jmp    12b4 <_nxweb_resolve_host+0x154>
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1274 <_nxweb_resolve_host+0x114>
cmpq   $0x0,-0x48(%rbp)
jne    12c4 <_nxweb_resolve_host+0x164>
jmp    130d <_nxweb_resolve_host+0x1ad>
mov    -0x48(%rbp),%rax
cmp    -0x50(%rbp),%rax
je     12f7 <_nxweb_resolve_host+0x197>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x58(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x50(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x48(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1327 <_nxweb_resolve_host+0x1c7>
mov    -0x50(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nxweb_resolve_host>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r15d
call   1070 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1195 <_nxweb_resolve_host+0x35>
mov    %rax,%rbp
add    $0x1,%rbp
movb   $0x0,(%rax)
jmp    119c <_nxweb_resolve_host+0x3c>
lea    0xe64(%rip),%rbp
lea    0x8(%rsp),%r12
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
test   %r15d,%r15d
je     11dd <_nxweb_resolve_host+0x7d>
mov    0x2e06(%rip),%rax
mov    (%rax),%r15d
mov    (%r14),%al
cmp    $0x2a,%al
mov    %r14,%rdi
cmove  %rbx,%rdi
mov    %r15d,0x18(%rsp)
test   %al,%al
cmove  %rbx,%rdi
mov    %rsp,%rcx
mov    %rbp,%rsi
mov    %r12,%rdx
call   1060 <getaddrinfo@plt>
test   %rax,%rax
jne    1264 <_nxweb_resolve_host+0x104>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     125d <_nxweb_resolve_host+0xfd>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rcx
cmp    %rcx,(%rdi)
jne    1226 <_nxweb_resolve_host+0xc6>
xor    %eax,%eax
mov    %rdi,%rbx
jmp    1241 <_nxweb_resolve_host+0xe1>
mov    %rdi,%rbx
nopl   0x0(%rax)
mov    %rbx,%rax
mov    0x8(%rbx),%rbx
test   %rbx,%rbx
je     125d <_nxweb_resolve_host+0xfd>
cmp    %rcx,(%rbx)
jne    1230 <_nxweb_resolve_host+0xd0>
cmp    %rdi,%rbx
je     1264 <_nxweb_resolve_host+0x104>
mov    0x8(%rbx),%rcx
mov    %rcx,0x8(%rax)
call   1080 <freeaddrinfo@plt>
movq   $0x0,0x8(%rbx)
jmp    1264 <_nxweb_resolve_host+0x104>
call   1080 <freeaddrinfo@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, %struct.addrinfo*, i32, i32 }
@.str = [3 x i8] c"80\00"
@PF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AF_INET = global i64 0
define %struct.addrinfo* @_nxweb_resolve_host(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i8* @strchr(i8* dereferenceable(1) %5, i8 signext 58)
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = getelementptr inbounds i8, i8* %6, i64 1
store i8 0, i8* %6
br label %10
10: ; preds = %2, %8
%11 = phi i8* [ %9, %8 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %2 ]
%12 = bitcast %struct.addrinfo* %3 to i8*
%13 = bitcast %struct.addrinfo** %4 to i8*
%14 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%15 = load i64, i64* @PF_UNSPEC
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i64 %15, i64* %16
%17 = load i32, i32* @SOCK_STREAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %17, i32* %18
%19 = icmp eq i32 %1, 0
%20 = load i32, i32* @AI_PASSIVE
%21 = select i1 %19, i32 0, i32 %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = load i8, i8* %5
%24 = icmp eq i8 %23, 0
%25 = icmp eq i8 %23, 42
%26 = or i1 %24, %25
%27 = select i1 %26, i8* null, i8* %5
%28 = call i64 @getaddrinfo(i8* %27, i8* %11, %struct.addrinfo* %3, %struct.addrinfo** %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %58
30: ; preds = %10
%31 = load %struct.addrinfo*, %struct.addrinfo** %4
%32 = icmp eq %struct.addrinfo* %31, null
br i1 %32, label %56, label %33
33: ; preds = %30
%34 = load i64, i64* @AF_INET
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 0
%36 = load i64, i64* %35
%37 = icmp eq i64 %36, %34
br i1 %37, label %47, label %42
38: ; preds = %42
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i64 0, i32 0
%40 = load i64, i64* %39
%41 = icmp eq i64 %40, %34
br i1 %41, label %47, label %42
42: ; preds = %33, %38
%43 = phi %struct.addrinfo* [ %45, %38 ], [ %31, %33 ]
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 1
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %56, label %38
47: ; preds = %38, %33
%48 = phi %struct.addrinfo* [ %31, %33 ], [ %45, %38 ]
%49 = phi %struct.addrinfo* [ null, %33 ], [ %43, %38 ]
%50 = icmp eq %struct.addrinfo* %48, %31
br i1 %50, label %58, label %51
51: ; preds = %47
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i64 0, i32 1
%53 = load %struct.addrinfo*, %struct.addrinfo** %52
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i64 0, i32 1
store %struct.addrinfo* %53, %struct.addrinfo** %54
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %31)
store %struct.addrinfo* null, %struct.addrinfo** %52
br label %58
56: ; preds = %42, %30
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %31)
br label %58
58: ; preds = %56, %10, %47, %51
%59 = phi %struct.addrinfo* [ %48, %51 ], [ %48, %47 ], [ null, %10 ], [ null, %56 ]
%60 = call i32 @free(i8* %5)
ret %struct.addrinfo* %59
}