#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 */
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
scalar_t__ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
void resolve(struct sockaddr *addr, char *hostname) {
  struct sockaddr_in *address;
  struct hostent *host;
  address = (struct sockaddr_in *)addr;
  bzero((char *)address, sizeof(struct sockaddr_in));
  address->sin_family = AF_INET;
  address->sin_addr.s_addr = inet_addr(hostname);
  if ((int)address->sin_addr.s_addr == -1) {
    host = gethostbyname(hostname);
    if (host) {
      bcopy(host->h_addr, (char *)&address->sin_addr, host->h_length);
    } else {
      perror("[resolve] Could not resolve address");
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x10,%esi
call   1070 <bzero@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    $0xffffffff,%eax
jne    1207 <resolve+0xa7>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ec <resolve+0x8c>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1050 <bcopy@plt>
jmp    1202 <resolve+0xa2>
lea    0xe0d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1207 <resolve+0xa7>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x10,%esi
call   1070 <bzero@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    %r14,%rdi
call   1030 <inet_addr@plt>
mov    %rax,(%rbx)
cmp    $0xffffffff,%eax
je     1198 <resolve+0x38>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11b9 <resolve+0x59>
mov    (%rax),%edx
mov    0x4(%rax),%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <bcopy@plt>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i32, i32 }
@AF_INET = global i32 0
@.str = [36 x i8] c"[resolve] Could not resolve address\00"
define void @resolve(%struct.sockaddr* %0, i8* %1) {
%3 = alloca %struct.sockaddr*
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.hostent*
store %struct.sockaddr* %0, %struct.sockaddr** %3
store i8* %1, i8** %4
%7 = load %struct.sockaddr*, %struct.sockaddr** %3
%8 = bitcast %struct.sockaddr* %7 to %struct.sockaddr_in*
store %struct.sockaddr_in* %8, %struct.sockaddr_in** %5
%9 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%10 = bitcast %struct.sockaddr_in* %9 to i8*
%11 = call i32 @bzero(i8* %10, i32 16)
%12 = load i32, i32* @AF_INET
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
store i32 %12, i32* %14
%15 = load i8*, i8** %4
%16 = call i64 @inet_addr(i8* %15)
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i64 %16, i64* %19
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = trunc i64 %23 to i32
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %46
26: ; preds = %2
%27 = load i8*, i8** %4
%28 = call %struct.hostent* @gethostbyname(i8* %27)
store %struct.hostent* %28, %struct.hostent** %6
%29 = load %struct.hostent*, %struct.hostent** %6
%30 = icmp ne %struct.hostent* %29, null
br i1 %30, label %31, label %42
31: ; preds = %26
%32 = load %struct.hostent*, %struct.hostent** %6
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i32 0, i32 0
%37 = bitcast %struct.TYPE_2__* %36 to i8*
%38 = load %struct.hostent*, %struct.hostent** %6
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @bcopy(i32 %34, i8* %37, i32 %40)
br label %45
42: ; preds = %26
%43 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %31
br label %46
46: ; preds = %45, %2
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.hostent = type { i32, i32 }
@AF_INET = global i32 0
@.str = [36 x i8] c"[resolve] Could not resolve address\00"
define void @resolve(%struct.sockaddr* %0, i8* %1) {
%3 = bitcast %struct.sockaddr* %0 to i8*
%4 = tail call i32 @bzero(i8* %3, i32 16)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 2, i32 0
store i32 %5, i32* %6
%7 = tail call i64 @inet_addr(i8* %1)
%8 = bitcast %struct.sockaddr* %0 to i64*
store i64 %7, i64* %8
%9 = trunc i64 %7 to i32
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %23
11: ; preds = %2
%12 = tail call %struct.hostent* @gethostbyname(i8* %1)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 1
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @bcopy(i32 %16, i8* %3, i32 %18)
br label %23
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %14, %2
ret void
}