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

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int valid_ipaddr(int pf, char *ipstr, int portnr) {
  struct addrinfo hints, *restmp, *res0;
  char portstr[5];
  int error;
  if (portnr != -1)
    snprintf(portstr, sizeof(portstr), "%d", portnr);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = pf;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if (portnr == -1)
    error = getaddrinfo(ipstr, NULL, &hints, &res0);
  else
    error = getaddrinfo(ipstr, portstr, &hints, &res0);
  if (error) {
    freeaddrinfo(res0);
    return (0);
  }
  for (restmp = res0; restmp; restmp = restmp->ai_next) {
    switch (restmp->ai_family) {
    case AF_INET:
      if (pf == AF_INET)
        return (1);
      break;
    case AF_INET6:
      if (pf == AF_INET6)
        return (1);
      break;
    default:
      freeaddrinfo(res0);
      return (0);
    }
  }
  freeaddrinfo(res0);
  return (0);
}


x86 assembly (clang, -O0):
<valid_ipaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1174 <valid_ipaddr+0x34>
lea    -0x45(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x5,%esi
lea    0xe91(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c9 <valid_ipaddr+0x89>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
jmp    11e1 <valid_ipaddr+0xa1>
mov    -0x10(%rbp),%rdi
lea    -0x45(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1200 <valid_ipaddr+0xc0>
mov    -0x40(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b6 <valid_ipaddr+0x176>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12a6 <valid_ipaddr+0x166>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
sub    $0x80,%eax
je     125d <valid_ipaddr+0x11d>
jmp    122c <valid_ipaddr+0xec>
mov    -0x50(%rbp),%eax
sub    $0x81,%eax
jne    127b <valid_ipaddr+0x13b>
jmp    123f <valid_ipaddr+0xff>
cmpl   $0x81,-0x8(%rbp)
jne    1258 <valid_ipaddr+0x118>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <valid_ipaddr+0x176>
jmp    1290 <valid_ipaddr+0x150>
cmpl   $0x80,-0x8(%rbp)
jne    1276 <valid_ipaddr+0x136>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <valid_ipaddr+0x176>
jmp    1290 <valid_ipaddr+0x150>
mov    -0x40(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b6 <valid_ipaddr+0x176>
jmp    1295 <valid_ipaddr+0x155>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    1208 <valid_ipaddr+0xc8>
mov    -0x40(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_ipaddr>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
cmp    $0xffffffff,%edx
je     11b0 <valid_ipaddr+0x70>
mov    %edx,%ecx
lea    0xea2(%rip),%rdx
lea    0x23(%rsp),%r15
mov    %r15,%rdi
mov    $0x5,%esi
call   1030 <snprintf@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %ebx,0x8(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
jmp    11ed <valid_ipaddr+0xad>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %ebx,0x8(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %rsp,%rcx
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1050 <getaddrinfo@plt>
mov    (%rsp),%rdi
test   %eax,%eax
je     120d <valid_ipaddr+0xcd>
call   1060 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
test   %rdi,%rdi
je     11fa <valid_ipaddr+0xba>
mov    %rdi,%rax
jmp    1231 <valid_ipaddr+0xf1>
nopw   0x0(%rax,%rax,1)
cmp    $0x80,%ebx
je     124b <valid_ipaddr+0x10b>
mov    0x8(%rax),%rax
test   %rax,%rax
je     11fa <valid_ipaddr+0xba>
mov    (%rax),%ecx
cmp    $0x80,%ecx
je     1220 <valid_ipaddr+0xe0>
cmp    $0x81,%ecx
jne    11fa <valid_ipaddr+0xba>
cmp    $0x81,%ebx
jne    1228 <valid_ipaddr+0xe8>
mov    $0x1,%eax
jmp    1201 <valid_ipaddr+0xc1>

LLVM IR (-O0):
%struct.addrinfo = type { i32, %struct.addrinfo*, i32, i32 }
@.str = [3 x i8] c"%d\00"
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
define i32 @valid_ipaddr(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca [5 x i8]
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %11, i64 0, i64 0
%17 = load i32, i32* %7
%18 = call i32 @snprintf(i8* %16, i32 5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %3
%20 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%21 = load i32, i32* %5
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @SOCK_STREAM
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %23, i32* %24
%25 = load i32, i32* @AI_PASSIVE
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i8*, i8** %6
%31 = call i32 @getaddrinfo(i8* %30, i8* null, %struct.addrinfo* %8, %struct.addrinfo** %10)
store i32 %31, i32* %12
br label %36
32: ; preds = %19
%33 = load i8*, i8** %6
%34 = getelementptr inbounds [5 x i8], [5 x i8]* %11, i64 0, i64 0
%35 = call i32 @getaddrinfo(i8* %33, i8* %34, %struct.addrinfo* %8, %struct.addrinfo** %10)
store i32 %35, i32* %12
br label %36
36: ; preds = %32, %29
%37 = load i32, i32* %12
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load %struct.addrinfo*, %struct.addrinfo** %10
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
store i32 0, i32* %4
br label %72
42: ; preds = %36
%43 = load %struct.addrinfo*, %struct.addrinfo** %10
store %struct.addrinfo* %43, %struct.addrinfo** %9
br label %44
44: ; preds = %65, %42
%45 = load %struct.addrinfo*, %struct.addrinfo** %9
%46 = icmp ne %struct.addrinfo* %45, null
br i1 %46, label %47, label %69
47: ; preds = %44
%48 = load %struct.addrinfo*, %struct.addrinfo** %9
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 0
%50 = load i32, i32* %49
switch i32 %50, label %61 [
i32 129, label %51
i32 128, label %56
]
51: ; preds = %47
%52 = load i32, i32* %5
%53 = icmp eq i32 %52, 129
br i1 %53, label %54, label %55
54: ; preds = %51
store i32 1, i32* %4
br label %72
55: ; preds = %51
br label %64
56: ; preds = %47
%57 = load i32, i32* %5
%58 = icmp eq i32 %57, 128
br i1 %58, label %59, label %60
59: ; preds = %56
store i32 1, i32* %4
br label %72
60: ; preds = %56
br label %64
61: ; preds = %47
%62 = load %struct.addrinfo*, %struct.addrinfo** %10
%63 = call i32 @freeaddrinfo(%struct.addrinfo* %62)
store i32 0, i32* %4
br label %72
64: ; preds = %60, %55
br label %65
65: ; preds = %64
%66 = load %struct.addrinfo*, %struct.addrinfo** %9
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 1
%68 = load %struct.addrinfo*, %struct.addrinfo** %67
store %struct.addrinfo* %68, %struct.addrinfo** %9
br label %44
69: ; preds = %44
%70 = load %struct.addrinfo*, %struct.addrinfo** %10
%71 = call i32 @freeaddrinfo(%struct.addrinfo* %70)
store i32 0, i32* %4
br label %72
72: ; preds = %69, %61, %59, %54, %39
%73 = load i32, i32* %4
ret i32 %73
}

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