#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 in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(struct in_addr *);
struct hostent *gethostbyname(char const *);
struct in_addr *malloc(int);
int /*<<< orphan*/ memcpy(struct in_addr *, int /*<<< orphan*/, int);
struct in_addr *gg_gethostbyname(const char *hostname) {
  struct in_addr *addr = NULL;
  struct hostent *hp;
  if (!(addr = malloc(sizeof(struct in_addr)))) {
    errno = ENOMEM;
    goto cleanup;
  }
  if (!(hp = gethostbyname(hostname)))
    goto cleanup;
  memcpy(addr, hp->h_addr, sizeof(struct in_addr));
  return addr;
cleanup:
  if (addr)
    free(addr);
  return NULL;
}


x86 assembly (clang, -O0):
<gg_gethostbyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1183 <gg_gethostbyname+0x43>
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5c(%rip),%rax
mov    %ecx,(%rax)
jmp    11c0 <gg_gethostbyname+0x80>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119f <gg_gethostbyname+0x5f>
jmp    11c0 <gg_gethostbyname+0x80>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11dc <gg_gethostbyname+0x9c>
cmpq   $0x0,-0x18(%rbp)
je     11d4 <gg_gethostbyname+0x94>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gg_gethostbyname>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x4,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1177 <gg_gethostbyname+0x37>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     118b <gg_gethostbyname+0x4b>
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
jmp    1195 <gg_gethostbyname+0x55>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    0x2e51(%rip),%rcx
mov    %eax,(%rcx)
jmp    1193 <gg_gethostbyname+0x53>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@ENOMEM = global i32 0
@errno = global i32 0
define %struct.in_addr* @gg_gethostbyname(i8* %0) {
%2 = alloca %struct.in_addr*
%3 = alloca i8*
%4 = alloca %struct.in_addr*
%5 = alloca %struct.hostent*
store i8* %0, i8** %3
store %struct.in_addr* null, %struct.in_addr** %4
%6 = call %struct.in_addr* @malloc(i32 4)
store %struct.in_addr* %6, %struct.in_addr** %4
%7 = icmp ne %struct.in_addr* %6, null
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = load i32, i32* @ENOMEM
store i32 %9, i32* @errno
br label %22
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %5
%13 = icmp ne %struct.hostent* %12, null
br i1 %13, label %15, label %14
14: ; preds = %10
br label %22
15: ; preds = %10
%16 = load %struct.in_addr*, %struct.in_addr** %4
%17 = load %struct.hostent*, %struct.hostent** %5
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(%struct.in_addr* %16, i32 %19, i32 4)
%21 = load %struct.in_addr*, %struct.in_addr** %4
store %struct.in_addr* %21, %struct.in_addr** %2
br label %29
22: ; preds = %14, %8
%23 = load %struct.in_addr*, %struct.in_addr** %4
%24 = icmp ne %struct.in_addr* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load %struct.in_addr*, %struct.in_addr** %4
%27 = call i32 @free(%struct.in_addr* %26)
br label %28
28: ; preds = %25, %22
store %struct.in_addr* null, %struct.in_addr** %2
br label %29
29: ; preds = %28, %15
%30 = load %struct.in_addr*, %struct.in_addr** %2
ret %struct.in_addr* %30
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@ENOMEM = global i32 0
@errno = global i32 0
define %struct.in_addr* @gg_gethostbyname(i8* %0) {
%2 = tail call %struct.in_addr* @malloc(i32 4)
%3 = icmp eq %struct.in_addr* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = load i32, i32* @ENOMEM
store i32 %5, i32* @errno
br label %15
6: ; preds = %1
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @memcpy(%struct.in_addr* %2, i32 %11, i32 4)
br label %15
13: ; preds = %6
%14 = tail call i32 @free(%struct.in_addr* %2)
br label %15
15: ; preds = %4, %13, %9
%16 = phi %struct.in_addr* [ %2, %9 ], [ null, %13 ], [ null, %4 ]
ret %struct.in_addr* %16
}