#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_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  scalar_t__ ai_protocol;
  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*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
char *as_numeric(char *address) {
  if ('0' <= address[0] && address[0] <= '9') {
    return address;
  }
  struct addrinfo hints;
  struct addrinfo *result;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  hints.ai_protocol = 0;
  int err = getaddrinfo(address, NULL, &hints, &result);
  if (err < 0) {
    printf("getaddrinfo: %s\n", gai_strerror(err));
    error(EXIT_FAILURE, 0, "Error converting domain name to IP address\n");
  }
  struct sockaddr_in *saddr = (struct sockaddr_in *)result->ai_addr;
  char *ip_address = inet_ntoa(saddr->sin_addr);
  freeaddrinfo(result);
  return ip_address;
}


x86 assembly (clang, -O0):
<as_numeric>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x30,%eax
cmp    %ecx,%eax
jg     11ad <as_numeric+0x3d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     11ad <as_numeric+0x3d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1264 <as_numeric+0xf4>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1239 <as_numeric+0xc9>
mov    -0x3c(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xde3(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    0xddd(%rip),%rdx
call   1070 <error@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<as_numeric>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    (%rdi),%al
add    $0xd0,%al
cmp    $0xa,%al
jb     1225 <as_numeric+0xb5>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
jns    120b <as_numeric+0x9b>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xe14(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rdx
xor    %esi,%esi
call   1070 <error@plt>
mov    (%rsp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i64, i32, i32, i32 }
%struct.sockaddr_in = type { i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [44 x i8] c"Error converting domain name to IP address\0A\00"
define i8* @as_numeric(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = alloca i8*
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp sle i32 48, %12
br i1 %13, label %14, label %22
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp sle i32 %18, 57
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i8*, i8** %3
store i8* %21, i8** %2
br label %53
22: ; preds = %14, %1
%23 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%24 = load i32, i32* @AF_UNSPEC
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 4
store i32 %24, i32* %25
%26 = load i32, i32* @SOCK_STREAM
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 3
store i32 %26, i32* %27
%28 = load i32, i32* @AI_PASSIVE
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 2
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 1
store i64 0, i64* %30
%31 = load i8*, i8** %3
%32 = call i32 @getaddrinfo(i8* %31, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %5)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %22
%36 = load i32, i32* %6
%37 = call i8* @gai_strerror(i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @error(i32 %39, i32 0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %35, %22
%42 = load %struct.addrinfo*, %struct.addrinfo** %5
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 0
%44 = load i64, i64* %43
%45 = inttoptr i64 %44 to %struct.sockaddr_in*
store %struct.sockaddr_in* %45, %struct.sockaddr_in** %7
%46 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i8* @inet_ntoa(i32 %48)
store i8* %49, i8** %8
%50 = load %struct.addrinfo*, %struct.addrinfo** %5
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
%52 = load i8*, i8** %8
store i8* %52, i8** %2
br label %53
53: ; preds = %41, %20
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, i32, i32, i32 }
%struct.sockaddr_in = type { i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [44 x i8] c"Error converting domain name to IP address\0A\00"
define i8* @as_numeric(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = load i8, i8* %0
%5 = add i8 %4, -48
%6 = icmp ult i8 %5, 10
br i1 %6, label %35, label %7
7: ; preds = %1
%8 = bitcast %struct.addrinfo* %2 to i8*
%9 = bitcast %struct.addrinfo** %3 to i8*
%10 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 32)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
store i64 0, i64* %17
%18 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %2, %struct.addrinfo** %3)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %7
%21 = call i8* @gai_strerror(i32 %18)
%22 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @error(i32 %23, i32 0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %20, %7
%26 = load %struct.addrinfo*, %struct.addrinfo** %3
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%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
%31 = load i32, i32* %30
%32 = call i8* @inet_ntoa(i32 %31)
%33 = load %struct.addrinfo*, %struct.addrinfo** %3
%34 = call i32 @freeaddrinfo(%struct.addrinfo* %33)
br label %35
35: ; preds = %1, %25
%36 = phi i8* [ %32, %25 ], [ %0, %1 ]
ret i8* %36
}