#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct addrinfo {
  scalar_t__ ai_family;
  int ai_flags;
  int ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ AF_UNSPEC;
int AI_ADDRCONFIG;
int AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
uint16_t parse_port(const char *value) {
  int ret;
  uint16_t port = 0;
  struct addrinfo *resolved;
  struct addrinfo hints = {.ai_family = AF_UNSPEC,
                           .ai_socktype = SOCK_DGRAM,
                           .ai_protocol = IPPROTO_UDP,
                           .ai_flags = AI_ADDRCONFIG | AI_PASSIVE};
  if (!strlen(value)) {
    fprintf(stderr, "Unable to parse empty port\n");
    return 0;
  }
  ret = getaddrinfo(NULL, value, &hints, &resolved);
  if (ret != 0) {
    fprintf(stderr, "%s: `%s`\n", gai_strerror(ret), value);
    return 0;
  }
  if (resolved->ai_family == AF_INET &&
      resolved->ai_addrlen == sizeof(struct sockaddr_in))
    port = ntohs(((struct sockaddr_in *)resolved->ai_addr)->sin_port);
  else if (resolved->ai_family == AF_INET6 &&
           resolved->ai_addrlen == sizeof(struct sockaddr_in6))
    port = ntohs(((struct sockaddr_in6 *)resolved->ai_addr)->sin6_port);
  else
    fprintf(stderr, "Neither IPv4 nor IPv6 address found: `%s`\n", value);
  freeaddrinfo(resolved);
  return port;
}


x86 assembly (clang, -O0):
<parse_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2e57(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    11f2 <parse_port+0x92>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fb <parse_port+0x19b>
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x40(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     124c <parse_port+0xec>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fb <parse_port+0x19b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jne    128c <parse_port+0x12c>
mov    -0x20(%rbp),%rax
movslq 0xc(%rax),%rax
cmp    $0x4,%rax
jne    128c <parse_port+0x12c>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x18(%rbp)
jmp    12ec <parse_port+0x18c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2d2e(%rip),%rcx
cmp    (%rcx),%rax
jne    12cc <parse_port+0x16c>
mov    -0x20(%rbp),%rax
movslq 0xc(%rax),%rax
cmp    $0x4,%rax
jne    12cc <parse_port+0x16c>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x18(%rbp)
jmp    12e7 <parse_port+0x187>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd46(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12ec <parse_port+0x18c>
mov    -0x20(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_port>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    0x2e1f(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x18(%rsp)
movl   $0x0,0x1c(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
call   1030 <strlen@plt>
test   %eax,%eax
je     1207 <parse_port+0xa7>
xor    %r14d,%r14d
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     1223 <parse_port+0xc3>
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%ebp
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe26(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1286 <parse_port+0x126>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1286 <parse_port+0x126>
mov    0x8(%rsp),%rax
mov    (%rax),%rcx
mov    0x2dae(%rip),%rdx
cmp    (%rdx),%rcx
jne    123d <parse_port+0xdd>
cmpl   $0x4,0xc(%rax)
je     124f <parse_port+0xef>
mov    0x2d84(%rip),%rdx
cmp    (%rdx),%rcx
jne    125f <parse_port+0xff>
cmpl   $0x4,0xc(%rax)
jne    125f <parse_port+0xff>
mov    0x10(%rax),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,%r14d
jmp    127c <parse_port+0x11c>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i64, i32, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_UNSPEC = global i64 0
@AI_ADDRCONFIG = global i32 0
@AI_PASSIVE = global i32 0
@IPPROTO_UDP = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Unable to parse empty port\0A\00"
@.str.1 = [10 x i8] c"%s: `%s`\0A\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str.2 = [43 x i8] c"Neither IPv4 nor IPv6 address found: `%s`\0A\00"
define i32 @parse_port(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo
store i8* %0, i8** %3
store i32 0, i32* %5
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 0
%9 = load i64, i64* @AF_UNSPEC
store i64 %9, i64* %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 1
%11 = load i32, i32* @AI_ADDRCONFIG
%12 = load i32, i32* @AI_PASSIVE
%13 = or i32 %11, %12
store i32 %13, i32* %10
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
%17 = load i32, i32* @IPPROTO_UDP
store i32 %17, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 5
%19 = load i32, i32* @SOCK_DGRAM
store i32 %19, i32* %18
%20 = load i8*, i8** %3
%21 = call i32 @strlen(i8* %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %1
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %86
26: ; preds = %1
%27 = load i8*, i8** %3
%28 = call i32 @getaddrinfo(i32* null, i8* %27, %struct.addrinfo* %7, %struct.addrinfo** %6)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = load i32, i32* %4
%34 = call i8* @gai_strerror(i32 %33)
%35 = load i8*, i8** %3
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %35)
store i32 0, i32* %2
br label %86
37: ; preds = %26
%38 = load %struct.addrinfo*, %struct.addrinfo** %6
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @AF_INET
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %57
43: ; preds = %37
%44 = load %struct.addrinfo*, %struct.addrinfo** %6
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 2
%46 = load i32, i32* %45
%47 = sext i32 %46 to i64
%48 = icmp eq i64 %47, 4
br i1 %48, label %49, label %57
49: ; preds = %43
%50 = load %struct.addrinfo*, %struct.addrinfo** %6
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 3
%52 = load i64, i64* %51
%53 = inttoptr i64 %52 to %struct.sockaddr_in*
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @ntohs(i32 %55)
store i32 %56, i32* %5
br label %82
57: ; preds = %43, %37
%58 = load %struct.addrinfo*, %struct.addrinfo** %6
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 0
%60 = load i64, i64* %59
%61 = load i64, i64* @AF_INET6
%62 = icmp eq i64 %60, %61
br i1 %62, label %63, label %77
63: ; preds = %57
%64 = load %struct.addrinfo*, %struct.addrinfo** %6
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 2
%66 = load i32, i32* %65
%67 = sext i32 %66 to i64
%68 = icmp eq i64 %67, 4
br i1 %68, label %69, label %77
69: ; preds = %63
%70 = load %struct.addrinfo*, %struct.addrinfo** %6
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 3
%72 = load i64, i64* %71
%73 = inttoptr i64 %72 to %struct.sockaddr_in6*
%74 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %73, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i32 @ntohs(i32 %75)
store i32 %76, i32* %5
br label %81
77: ; preds = %63, %57
%78 = load i32, i32* @stderr
%79 = load i8*, i8** %3
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %77, %69
br label %82
82: ; preds = %81, %49
%83 = load %struct.addrinfo*, %struct.addrinfo** %6
%84 = call i32 @freeaddrinfo(%struct.addrinfo* %83)
%85 = load i32, i32* %5
store i32 %85, i32* %2
br label %86
86: ; preds = %82, %31, %23
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i64, i32, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_UNSPEC = global i64 0
@AI_ADDRCONFIG = global i32 0
@AI_PASSIVE = global i32 0
@IPPROTO_UDP = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Unable to parse empty port\0A\00"
@.str.1 = [10 x i8] c"%s: `%s`\0A\00"
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str.2 = [43 x i8] c"Neither IPv4 nor IPv6 address found: `%s`\0A\00"
define i32 @parse_port(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
%7 = load i64, i64* @AF_UNSPEC
store i64 %7, i64* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
%9 = load i32, i32* @AI_ADDRCONFIG
%10 = load i32, i32* @AI_PASSIVE
%11 = or i32 %10, %9
store i32 %11, i32* %8
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
%15 = load i32, i32* @IPPROTO_UDP
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
%17 = load i32, i32* @SOCK_DGRAM
store i32 %17, i32* %16
%18 = tail call i32 @strlen(i8* %0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %68
23: ; preds = %1
%24 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %3, %struct.addrinfo** %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i8* @gai_strerror(i32 %24)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %0)
br label %68
30: ; preds = %23
%31 = load %struct.addrinfo*, %struct.addrinfo** %2
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 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 %47
36: ; preds = %30
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 2
%38 = load i32, i32* %37
%39 = icmp eq i32 %38, 4
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 3
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to %struct.sockaddr_in*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %43, i64 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @ntohs(i32 %45)
br label %64
47: ; preds = %36, %30
%48 = load i64, i64* @AF_INET6
%49 = icmp eq i64 %33, %48
br i1 %49, label %50, label %61
50: ; preds = %47
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 2
%52 = load i32, i32* %51
%53 = icmp eq i32 %52, 4
br i1 %53, label %54, label %61
54: ; preds = %50
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 3
%56 = load i64, i64* %55
%57 = inttoptr i64 %56 to %struct.sockaddr_in6*
%58 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %57, i64 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @ntohs(i32 %59)
br label %64
61: ; preds = %50, %47
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %64
64: ; preds = %54, %61, %40
%65 = phi i32 [ %46, %40 ], [ %60, %54 ], [ 0, %61 ]
%66 = load %struct.addrinfo*, %struct.addrinfo** %2
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
br label %68
68: ; preds = %64, %26, %20
%69 = phi i32 [ 0, %26 ], [ %65, %64 ], [ 0, %20 ]
ret i32 %69
}