#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int getipv4byname(const char *restrict name, uint32_t *restrict ipv4) {
  struct addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_DGRAM},
                  *res;
  int val = getaddrinfo(name, NULL, &hints, &res);
  if (val)
    return val;
  *ipv4 = ((const struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
  freeaddrinfo(res);
  return 0;
}


x86 assembly (clang, -O0):
<getipv4byname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1030 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     117d <getipv4byname+0x5d>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119c <getipv4byname+0x7c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
call   1040 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getipv4byname>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
movq   $0x0,0x8(%rsp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
xor    %esi,%esi
call   1030 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1171 <getipv4byname+0x51>
mov    (%rsp),%rdi
mov    (%rdi),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
call   1040 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
define i32 @getipv4byname(i8* noalias %0, i32* noalias %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 1
%11 = load i32, i32* @SOCK_DGRAM
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
%13 = load i32, i32* @AF_INET
store i32 %13, i32* %12
%14 = load i8*, i8** %4
%15 = call i32 @getaddrinfo(i8* %14, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = load i32, i32* %8
store i32 %19, i32* %3
br label %31
20: ; preds = %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %7
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to %struct.sockaddr_in*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32*, i32** %5
store i32 %27, i32* %28
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
%30 = call i32 @freeaddrinfo(%struct.addrinfo* %29)
store i32 0, i32* %3
br label %31
31: ; preds = %20, %18
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
define i32 @getipv4byname(i8* noalias %0, i32* noalias nocapture writeonly %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
%8 = load i32, i32* @SOCK_DGRAM
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
%10 = load i32, i32* @AF_INET
store i32 %10, i32* %9
%11 = bitcast %struct.addrinfo** %4 to i8*
%12 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 0
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to %struct.sockaddr_in*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i64 0, i32 0, i32 0
%20 = load i32, i32* %19
store i32 %20, i32* %1
%21 = call i32 @freeaddrinfo(%struct.addrinfo* %15)
br label %22
22: ; preds = %2, %14
ret i32 %12
}