#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 sockaddr_storage {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ freeaddrinfo(struct sockaddr_storage *);
scalar_t__ getaddrinfo(char *, char *, struct sockaddr_storage *,
                       struct sockaddr_storage **);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
char *strtok_r(char *, char *, char **);
int scan_tcp_port(char *str, struct sockaddr_storage *addr) {
  char *strtok_data;
  char *ip;
  char *port;
  struct addrinfo hints, *ai;
  memset(addr, 0, sizeof(*addr));
  ip = strtok_r(str, ",", &strtok_data);
  port = strtok_r(NULL, "", &strtok_data);
  if (port == NULL) {
    port = ip;
    ip = NULL;
  }
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_INET6;
  if (getaddrinfo(ip, port, &hints, &ai))
    return -1;
  memcpy(addr, ai->ai_addr, ai->ai_addrlen);
  freeaddrinfo(ai);
  return 0;
}


x86 assembly (clang, -O0):
<scan_tcp_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe6e(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bb <scan_tcp_port+0x6b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0x0,%rax
je     120e <scan_tcp_port+0xbe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <scan_tcp_port+0xe4>
mov    -0x18(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x48(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_tcp_port>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
lea    0xe89(%rip),%rsi
lea    0x20(%rsp),%r15
mov    %rbx,%rdi
mov    %r15,%rdx
call   1040 <strtok_r@plt>
mov    %rax,%r12
lea    0xe70(%rip),%rsi
xor    %edi,%edi
mov    %r15,%rdx
call   1040 <strtok_r@plt>
test   %rax,%rax
mov    %r12,%rbx
cmove  %rax,%rbx
cmovne %rax,%r12
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1215 <scan_tcp_port+0xc5>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r14,%rdi
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32, i32, i32, i32 }
%struct.addrinfo = type { i32, i32, i32, i32 }
@.str = [2 x i8] c",\00"
@.str.1 = [1 x i8] zeroinitializer
@AI_PASSIVE = global i32 0
@AF_INET6 = global i32 0
define i32 @scan_tcp_port(i8* %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_storage*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.addrinfo
%10 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store %struct.sockaddr_storage* %1, %struct.sockaddr_storage** %5
%11 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%12 = call i32 @memset(%struct.sockaddr_storage* %11, i32 0, i32 16)
%13 = load i8*, i8** %4
%14 = call i8* @strtok_r(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %6)
store i8* %14, i8** %7
%15 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %6)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = load i8*, i8** %7
store i8* %19, i8** %8
store i8* null, i8** %7
br label %20
20: ; preds = %18, %2
%21 = bitcast %struct.addrinfo* %9 to %struct.sockaddr_storage*
%22 = call i32 @memset(%struct.sockaddr_storage* %21, i32 0, i32 16)
%23 = load i32, i32* @AI_PASSIVE
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 3
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET6
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i8*, i8** %7
%28 = load i8*, i8** %8
%29 = bitcast %struct.addrinfo* %9 to %struct.sockaddr_storage*
%30 = bitcast %struct.addrinfo** %10 to %struct.sockaddr_storage**
%31 = call i64 @getaddrinfo(i8* %27, i8* %28, %struct.sockaddr_storage* %29, %struct.sockaddr_storage** %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %20
store i32 -1, i32* %3
br label %46
34: ; preds = %20
%35 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%36 = load %struct.addrinfo*, %struct.addrinfo** %10
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %10
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @memcpy(%struct.sockaddr_storage* %35, i32 %38, i32 %41)
%43 = load %struct.addrinfo*, %struct.addrinfo** %10
%44 = bitcast %struct.addrinfo* %43 to %struct.sockaddr_storage*
%45 = call i32 @freeaddrinfo(%struct.sockaddr_storage* %44)
store i32 0, i32* %3
br label %46
46: ; preds = %34, %33
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32, i32, i32, i32 }
%struct.addrinfo = type { i32, i32, i32, i32 }
@.str = [2 x i8] c",\00"
@.str.1 = [1 x i8] zeroinitializer
@AI_PASSIVE = global i32 0
@AF_INET6 = global i32 0
define i32 @scan_tcp_port(i8* %0, %struct.sockaddr_storage* %1) {
%3 = alloca i8*
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast i8** %3 to i8*
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
%9 = tail call i32 @memset(%struct.sockaddr_storage* %1, i32 0, i32 16)
%10 = call i8* @strtok_r(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3)
%11 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %3)
%12 = icmp eq i8* %11, null
%13 = select i1 %12, i8* null, i8* %10
%14 = select i1 %12, i8* %10, i8* %11
%15 = bitcast %struct.addrinfo* %4 to %struct.sockaddr_storage*
%16 = call i32 @memset(%struct.sockaddr_storage* %15, i32 0, i32 16)
%17 = load i32, i32* @AI_PASSIVE
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET6
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = bitcast %struct.addrinfo** %5 to %struct.sockaddr_storage**
%22 = call i64 @getaddrinfo(i8* %13, i8* %14, %struct.sockaddr_storage* %15, %struct.sockaddr_storage** %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %2
%25 = load %struct.addrinfo*, %struct.addrinfo** %5
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @memcpy(%struct.sockaddr_storage* %1, i32 %27, i32 %29)
%31 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %21
%32 = call i32 @freeaddrinfo(%struct.sockaddr_storage* %31)
br label %33
33: ; preds = %2, %24
%34 = phi i32 [ 0, %24 ], [ -1, %2 ]
ret i32 %34
}