#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 {
  int dummy;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int EAI_SYSTEM;
int /*<<< orphan*/ ENAMETOOLONG;
int /*<<< orphan*/ errno;
int getaddrinfo(char const *, char const *, struct addrinfo const *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strchr(char const *, char);
scalar_t__ strcmp(char const *, char *);
int getaddrinfo_helper(const char *host_and_port, int default_port,
                       const struct addrinfo *hints, struct addrinfo **aip) {
  const char *colon, *host, *port;
  char buffer[256];
  colon = strchr(host_and_port, ':');
  if (colon == NULL) {
    snprintf(buffer, sizeof(buffer), "%d", default_port);
    host = host_and_port;
    port = buffer;
  } else {
    size_t len = colon - host_and_port;
    if (len >= sizeof(buffer)) {
      errno = ENAMETOOLONG;
      return EAI_SYSTEM;
    }
    memcpy(buffer, host_and_port, len);
    buffer[len] = 0;
    host = buffer;
    port = colon + 1;
  }
  if (strcmp(host, "*") == 0)
    host = "0.0.0.0";
  return getaddrinfo(host, port, hints, aip);
}


x86 assembly (clang, -O0):
<getaddrinfo_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1030 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ba <getaddrinfo_helper+0x6a>
lea    -0x140(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x100,%esi
lea    0xe63(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    123d <getaddrinfo_helper+0xed>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x148(%rbp)
cmpq   $0x100,-0x148(%rbp)
jb     1200 <getaddrinfo_helper+0xb0>
mov    0x2dec(%rip),%rax
mov    (%rax),%ecx
mov    0x2deb(%rip),%rax
mov    %ecx,(%rax)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <getaddrinfo_helper+0x12a>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x148(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x148(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1262 <getaddrinfo_helper+0x112>
lea    0xda7(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaddrinfo_helper>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x3a,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11aa <getaddrinfo_helper+0x5a>
mov    %rax,%r12
mov    %rax,%rbp
sub    %rbx,%rbp
cmp    $0x100,%rbp
jb     11c5 <getaddrinfo_helper+0x75>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    0x2e3b(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
jmp    120c <getaddrinfo_helper+0xbc>
mov    %rsp,%r12
lea    0xe4c(%rip),%rdx
mov    %r12,%rdi
mov    $0x100,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
jmp    11e1 <getaddrinfo_helper+0x91>
mov    %rsp,%r13
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rsp,%rbp,1)
add    $0x1,%r12
mov    %r13,%rbx
lea    0xe1b(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
lea    0xe0b(%rip),%rdi
cmovne %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <getaddrinfo@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32 }
@.str = [3 x i8] c"%d\00"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@EAI_SYSTEM = global i32 0
@.str.1 = [2 x i8] c"*\00"
@.str.2 = [8 x i8] c"0.0.0.0\00"
define i32 @getaddrinfo_helper(i8* %0, i32 %1, %struct.addrinfo* %2, %struct.addrinfo** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.addrinfo*
%9 = alloca %struct.addrinfo**
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca [256 x i8]
%14 = alloca i64
store i8* %0, i8** %6
store i32 %1, i32* %7
store %struct.addrinfo* %2, %struct.addrinfo** %8
store %struct.addrinfo** %3, %struct.addrinfo*** %9
%15 = load i8*, i8** %6
%16 = call i8* @strchr(i8* %15, i8 signext 58)
store i8* %16, i8** %10
%17 = load i8*, i8** %10
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %25
19: ; preds = %4
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%21 = load i32, i32* %7
%22 = call i32 @snprintf(i8* %20, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %6
store i8* %23, i8** %11
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
store i8* %24, i8** %12
br label %46
25: ; preds = %4
%26 = load i8*, i8** %10
%27 = load i8*, i8** %6
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
store i64 %30, i64* %14
%31 = load i64, i64* %14
%32 = icmp uge i64 %31, 256
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = load i32, i32* @ENAMETOOLONG
store i32 %34, i32* @errno
%35 = load i32, i32* @EAI_SYSTEM
store i32 %35, i32* %5
br label %57
36: ; preds = %25
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%38 = load i8*, i8** %6
%39 = load i64, i64* %14
%40 = call i32 @memcpy(i8* %37, i8* %38, i64 %39)
%41 = load i64, i64* %14
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 %41
store i8 0, i8* %42
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
store i8* %43, i8** %11
%44 = load i8*, i8** %10
%45 = getelementptr inbounds i8, i8* %44, i64 1
store i8* %45, i8** %12
br label %46
46: ; preds = %36, %19
%47 = load i8*, i8** %11
%48 = call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %11
br label %51
51: ; preds = %50, %46
%52 = load i8*, i8** %11
%53 = load i8*, i8** %12
%54 = load %struct.addrinfo*, %struct.addrinfo** %8
%55 = load %struct.addrinfo**, %struct.addrinfo*** %9
%56 = call i32 @getaddrinfo(i8* %52, i8* %53, %struct.addrinfo* %54, %struct.addrinfo** %55)
store i32 %56, i32* %5
br label %57
57: ; preds = %51, %33
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
%struct.addrinfo = type { i32 }
@.str = [3 x i8] c"%d\00"
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@EAI_SYSTEM = global i32 0
@.str.1 = [2 x i8] c"*\00"
@.str.2 = [8 x i8] c"0.0.0.0\00"
define i32 @getaddrinfo_helper(i8* %0, i32 %1, %struct.addrinfo* %2, %struct.addrinfo** %3) {
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 58)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %23
11: ; preds = %4
%12 = ptrtoint i8* %7 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = icmp ugt i64 %14, 255
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* @ENAMETOOLONG
store i32 %17, i32* @errno
%18 = load i32, i32* @EAI_SYSTEM
br label %30
19: ; preds = %11
%20 = call i32 @memcpy(i8* %6, i8* %0, i64 %14)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %14
store i8 0, i8* %21
%22 = getelementptr inbounds i8, i8* %7, i64 1
br label %23
23: ; preds = %19, %9
%24 = phi i8* [ %0, %9 ], [ %6, %19 ]
%25 = phi i8* [ %6, %9 ], [ %22, %19 ]
%26 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
%28 = select i1 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %24
%29 = call i32 @getaddrinfo(i8* %28, i8* %25, %struct.addrinfo* %2, %struct.addrinfo** %3)
br label %30
30: ; preds = %16, %23
%31 = phi i32 [ %29, %23 ], [ %18, %16 ]
ret i32 %31
}