#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_in6 {
  int sin6_port;
};
struct sockaddr_in {
  int sin_port;
};
struct addrinfo {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/ *,
                struct addrinfo **);
int ntohs(int);
int xtables_getportbyname(const char *name) {
  struct addrinfo *res = NULL, *p;
  int ret;
  ret = getaddrinfo(NULL, name, NULL, &res);
  if (ret < 0)
    return -1;
  ret = -1;
  for (p = res; p != NULL; p = p->ai_next) {
    if (p->ai_family == AF_INET6) {
      ret = ((struct sockaddr_in6 *)p->ai_addr)->sin6_port;
      break;
    } else if (p->ai_family == AF_INET) {
      ret = ((struct sockaddr_in *)p->ai_addr)->sin_port;
      break;
    }
  }
  freeaddrinfo(res);
  if (ret < 0)
    return ret;
  return ntohs(ret);
}


x86 assembly (clang, -O0):
<xtables_getportbyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
mov    %rdx,%rdi
call   1040 <getaddrinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1171 <xtables_getportbyname+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <xtables_getportbyname+0xf1>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f8 <xtables_getportbyname+0xc8>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
jne    11b4 <xtables_getportbyname+0x84>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    11f8 <xtables_getportbyname+0xc8>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <xtables_getportbyname+0xad>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    11f8 <xtables_getportbyname+0xc8>
jmp    11e2 <xtables_getportbyname+0xb2>
jmp    11e7 <xtables_getportbyname+0xb7>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <xtables_getportbyname+0x50>
mov    -0x18(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
cmpl   $0x0,-0x24(%rbp)
jge    1216 <xtables_getportbyname+0xe6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1221 <xtables_getportbyname+0xf1>
mov    -0x24(%rbp),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xtables_getportbyname>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rsi
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
xor    %edx,%edx
call   1040 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11b5 <xtables_getportbyname+0x85>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1196 <xtables_getportbyname+0x66>
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    0x2e75(%rip),%rcx
mov    (%rcx),%rdx
mov    %rdi,%rcx
nopl   0x0(%rax)
mov    (%rcx),%rsi
cmp    %rax,%rsi
je     119d <xtables_getportbyname+0x6d>
cmp    %rdx,%rsi
je     119d <xtables_getportbyname+0x6d>
mov    0x10(%rcx),%rcx
test   %rcx,%rcx
jne    1180 <xtables_getportbyname+0x50>
call   1050 <freeaddrinfo@plt>
jmp    11b5 <xtables_getportbyname+0x85>
mov    0x8(%rcx),%rax
mov    (%rax),%ebx
call   1050 <freeaddrinfo@plt>
test   %ebx,%ebx
js     11b5 <xtables_getportbyname+0x85>
mov    %ebx,%edi
call   1030 <ntohs@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i64, %struct.addrinfo* }
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET6 = global i64 0
@AF_INET = global i64 0
define i32 @xtables_getportbyname(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo*
%6 = alloca i32
store i8* %0, i8** %3
store %struct.addrinfo* null, %struct.addrinfo** %4
%7 = load i8*, i8** %3
%8 = call i32 @getaddrinfo(i32* null, i8* %7, i32* null, %struct.addrinfo** %4)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %59
12: ; preds = %1
store i32 -1, i32* %6
%13 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %13, %struct.addrinfo** %5
br label %14
14: ; preds = %45, %12
%15 = load %struct.addrinfo*, %struct.addrinfo** %5
%16 = icmp ne %struct.addrinfo* %15, null
br i1 %16, label %17, label %49
17: ; preds = %14
%18 = load %struct.addrinfo*, %struct.addrinfo** %5
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @AF_INET6
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load %struct.addrinfo*, %struct.addrinfo** %5
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to %struct.sockaddr_in6*
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %27, i32 0, i32 0
%29 = load i32, i32* %28
store i32 %29, i32* %6
br label %49
30: ; preds = %17
%31 = load %struct.addrinfo*, %struct.addrinfo** %5
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @AF_INET
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %43
36: ; preds = %30
%37 = load %struct.addrinfo*, %struct.addrinfo** %5
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 1
%39 = load i64, i64* %38
%40 = inttoptr i64 %39 to %struct.sockaddr_in*
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 0
%42 = load i32, i32* %41
store i32 %42, i32* %6
br label %49
43: ; preds = %30
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %5
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 2
%48 = load %struct.addrinfo*, %struct.addrinfo** %47
store %struct.addrinfo* %48, %struct.addrinfo** %5
br label %14
49: ; preds = %36, %23, %14
%50 = load %struct.addrinfo*, %struct.addrinfo** %4
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
%52 = load i32, i32* %6
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = load i32, i32* %6
store i32 %55, i32* %2
br label %59
56: ; preds = %49
%57 = load i32, i32* %6
%58 = call i32 @ntohs(i32 %57)
store i32 %58, i32* %2
br label %59
59: ; preds = %56, %54, %11
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, %struct.addrinfo* }
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET6 = global i64 0
@AF_INET = global i64 0
define i32 @xtables_getportbyname(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = bitcast %struct.addrinfo** %2 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %2
%4 = call i32 @getaddrinfo(i32* null, i8* %0, i32* null, %struct.addrinfo** %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %42, label %6
6: ; preds = %1
%7 = load %struct.addrinfo*, %struct.addrinfo** %2
%8 = icmp eq %struct.addrinfo* %7, null
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = load i64, i64* @AF_INET6
%11 = load i64, i64* @AF_INET
br label %14
12: ; preds = %31, %6
%13 = call i32 @freeaddrinfo(%struct.addrinfo* %7)
br label %42
14: ; preds = %9, %31
%15 = phi %struct.addrinfo* [ %7, %9 ], [ %33, %31 ]
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 0
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, %10
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 1
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to %struct.sockaddr_in6*
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %22, i64 0, i32 0
br label %35
24: ; preds = %14
%25 = icmp eq i64 %17, %11
br i1 %25, label %26, label %31
26: ; preds = %24
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 1
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to %struct.sockaddr_in*
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %29, i64 0, i32 0
br label %35
31: ; preds = %24
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 2
%33 = load %struct.addrinfo*, %struct.addrinfo** %32
%34 = icmp eq %struct.addrinfo* %33, null
br i1 %34, label %12, label %14
35: ; preds = %26, %19
%36 = phi i32* [ %23, %19 ], [ %30, %26 ]
%37 = load i32, i32* %36
%38 = call i32 @freeaddrinfo(%struct.addrinfo* %7)
%39 = icmp slt i32 %37, 0
br i1 %39, label %42, label %40
40: ; preds = %35
%41 = call i32 @ntohs(i32 %37)
br label %42
42: ; preds = %12, %35, %1, %40
%43 = phi i32 [ %41, %40 ], [ -1, %1 ], [ %37, %35 ], [ -1, %12 ]
ret i32 %43
}