#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 /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, int /*<<< orphan*/ *, struct addrinfo **);
scalar_t__ isdigit(char);
int /*<<< orphan*/ malloc(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
char *strncpy(int /*<<< orphan*/, char *, int);
int mlns_addrparse(struct addrinfo **res, char *str) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  int n;
  for (n = (int)strlen(str) - 1; isdigit(str[n]) && n > 0; n--)
    ;
  char *host_part;
  if (n != 0) {
    host_part = strncpy(malloc(n * sizeof(char)), str, n);
    host_part[n] = 0;
    n++;
  } else {
    host_part = "0.0.0.0";
  }
  int rc = getaddrinfo(host_part, str + n, NULL, res);
  if (rc != 0) {
    fprintf(stderr, "getaddrinfo for %s: %s\n", str, gai_strerror(rc));
  }
  return 1;
}


x86 assembly (clang, -O0):
<mlns_addrparse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isdigit@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x35(%rbp)
je     1200 <mlns_addrparse+0x80>
cmpl   $0x0,-0x24(%rbp)
setg   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    1210 <mlns_addrparse+0x90>
jmp    1223 <mlns_addrparse+0xa3>
jmp    1215 <mlns_addrparse+0x95>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11d3 <mlns_addrparse+0x53>
cmpl   $0x0,-0x24(%rbp)
je     1268 <mlns_addrparse+0xe8>
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1273 <mlns_addrparse+0xf3>
lea    0xd91(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12ce <mlns_addrparse+0x14e>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd3f(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mlns_addrparse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
shl    $0x20,%rax
movabs $0x100000000,%r13
add    %rax,%r13
movabs $0xffffffff00000000,%rbp
ff ff ff
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %rbp,%r13
lea    -0x1(%rbx),%r12
movsbl -0x1(%r15,%rbx,1),%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     1220 <mlns_addrparse+0xa0>
cmp    $0x1,%rbx
mov    %r12,%rbx
jg     1200 <mlns_addrparse+0x80>
test   %r12d,%r12d
je     1248 <mlns_addrparse+0xc8>
mov    %r12d,%edi
call   1090 <malloc@plt>
mov    %eax,%edi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
mov    %rax,%rdi
movb   $0x0,(%rax,%r12,1)
sar    $0x20,%r13
jmp    1252 <mlns_addrparse+0xd2>
lea    0xdb1(%rip),%rdi
xor    %r13d,%r13d
add    %r15,%r13
mov    %r13,%rsi
xor    %edx,%edx
mov    %r14,%rcx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     128a <mlns_addrparse+0x10a>
mov    0x2d63(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xd8b(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"getaddrinfo for %s: %s\0A\00"
define i32 @mlns_addrparse(%struct.addrinfo** %0, i8* %1) {
%3 = alloca %struct.addrinfo**
%4 = alloca i8*
%5 = alloca %struct.addrinfo
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store %struct.addrinfo** %0, %struct.addrinfo*** %3
store i8* %1, i8** %4
%9 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 12)
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i8*, i8** %4
%17 = call i64 @strlen(i8* %16)
%18 = trunc i64 %17 to i32
%19 = sub nsw i32 %18, 1
store i32 %19, i32* %6
br label %20
20: ; preds = %34, %2
%21 = load i8*, i8** %4
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = call i64 @isdigit(i8 signext %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = load i32, i32* %6
%30 = icmp sgt i32 %29, 0
br label %31
31: ; preds = %28, %20
%32 = phi i1 [ false, %20 ], [ %30, %28 ]
br i1 %32, label %33, label %37
33: ; preds = %31
br label %34
34: ; preds = %33
%35 = load i32, i32* %6
%36 = add nsw i32 %35, -1
store i32 %36, i32* %6
br label %20
37: ; preds = %31
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %55
40: ; preds = %37
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = mul i64 %42, 1
%44 = trunc i64 %43 to i32
%45 = call i32 @malloc(i32 %44)
%46 = load i8*, i8** %4
%47 = load i32, i32* %6
%48 = call i8* @strncpy(i32 %45, i8* %46, i32 %47)
store i8* %48, i8** %7
%49 = load i8*, i8** %7
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %56
55: ; preds = %37
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %7
br label %56
56: ; preds = %55, %40
%57 = load i8*, i8** %7
%58 = load i8*, i8** %4
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load %struct.addrinfo**, %struct.addrinfo*** %3
%63 = call i32 @getaddrinfo(i8* %57, i8* %61, i32* null, %struct.addrinfo** %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %56
%67 = load i32, i32* @stderr
%68 = load i8*, i8** %4
%69 = load i32, i32* %8
%70 = call i8* @gai_strerror(i32 %69)
%71 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %68, i8* %70)
br label %72
72: ; preds = %66, %56
ret i32 1
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [8 x i8] c"0.0.0.0\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"getaddrinfo for %s: %s\0A\00"
define i32 @mlns_addrparse(%struct.addrinfo** %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = bitcast %struct.addrinfo* %3 to i8*
%5 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 12)
%6 = load i32, i32* @AF_UNSPEC
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @AI_PASSIVE
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32 %10, i32* %11
%12 = call i64 @strlen(i8* dereferenceable(1) %1)
%13 = shl i64 %12, 32
%14 = ashr exact i64 %13, 32
br label %15
15: ; preds = %15, %2
%16 = phi i64 [ %17, %15 ], [ %14, %2 ]
%17 = add nsw i64 %16, -1
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = load i8, i8* %18
%20 = call i64 @isdigit(i8 signext %19)
%21 = icmp ne i64 %20, 0
%22 = icmp sgt i64 %16, 1
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %15, label %24
24: ; preds = %15
%25 = trunc i64 %17 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = call i32 @malloc(i32 %25)
%29 = call i8* @strncpy(i32 %28, i8* %1, i32 %25)
%30 = getelementptr inbounds i8, i8* %29, i64 %17
store i8 0, i8* %30
%31 = shl i64 %16, 32
%32 = ashr exact i64 %31, 32
br label %33
33: ; preds = %24, %27
%34 = phi i8* [ %29, %27 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %24 ]
%35 = phi i64 [ %32, %27 ], [ 0, %24 ]
%36 = getelementptr inbounds i8, i8* %1, i64 %35
%37 = call i32 @getaddrinfo(i8* %34, i8* %36, i32* null, %struct.addrinfo** %0)
%38 = icmp eq i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %33
%40 = load i32, i32* @stderr
%41 = call i8* @gai_strerror(i32 %37)
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %41)
br label %43
43: ; preds = %39, %33
ret i32 1
}