#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 {
  unsigned int s_addr;
};
struct hostent {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
struct hostent *gethostbyname(char *);
unsigned int inet_addr(char *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ printf(char *, char *);
unsigned int resolve_ip(char *host, int showmsg, int allownames) {
  struct hostent *new;
  unsigned int hostaddr;
  struct in_addr *ip;
  if ((hostaddr = inet_addr(host)) == (unsigned int)-1) {
    if (allownames) {
      if ((new = gethostbyname(host)) == (struct hostent *)0) {
        return (-1);
      } else {
        ip = ((struct in_addr *)*new->h_addr_list);
        hostaddr = ip->s_addr;
        if (showmsg)
          printf("Connecting to %s...\n", inet_ntoa(*ip));
      }
    } else
      return (-1);
  }
  return (hostaddr);
}


x86 assembly (clang, -O0):
<resolve_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11ea <resolve_ip+0xaa>
cmpl   $0x0,-0x18(%rbp)
je     11d9 <resolve_ip+0x99>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1194 <resolve_ip+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <resolve_ip+0xb0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11cf <resolve_ip+0x8f>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
jmp    11d4 <resolve_ip+0x94>
jmp    11e5 <resolve_ip+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <resolve_ip+0xb0>
jmp    11ea <resolve_ip+0xaa>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_ip>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%rbp
call   1050 <inet_addr@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1169 <resolve_ip+0x29>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebx
test   %r15d,%r15d
je     115c <resolve_ip+0x1c>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     115c <resolve_ip+0x1c>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%ebx
test   %r14d,%r14d
je     115c <resolve_ip+0x1c>
mov    %ebx,%edi
call   1030 <inet_ntoa@plt>
lea    0xe65(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    115c <resolve_ip+0x1c>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
@.str = [21 x i8] c"Connecting to %s...\0A\00"
define i32 @resolve_ip(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.hostent*
%9 = alloca i32
%10 = alloca %struct.in_addr*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = call i32 @inet_addr(i8* %11)
store i32 %12, i32* %9
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %43
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %41
17: ; preds = %14
%18 = load i8*, i8** %5
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %8
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %4
br label %45
22: ; preds = %17
%23 = load %struct.hostent*, %struct.hostent** %8
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load i64*, i64** %24
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to %struct.in_addr*
store %struct.in_addr* %27, %struct.in_addr** %10
%28 = load %struct.in_addr*, %struct.in_addr** %10
%29 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %28, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %9
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %22
%34 = load %struct.in_addr*, %struct.in_addr** %10
%35 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i8* @inet_ntoa(i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %33, %22
br label %40
40: ; preds = %39
br label %42
41: ; preds = %14
store i32 -1, i32* %4
br label %45
42: ; preds = %40
br label %43
43: ; preds = %42, %3
%44 = load i32, i32* %9
store i32 %44, i32* %4
br label %45
45: ; preds = %43, %41, %21
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
@.str = [21 x i8] c"Connecting to %s...\0A\00"
define i32 @resolve_ip(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @inet_addr(i8* %0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %22
6: ; preds = %3
%7 = icmp eq i32 %2, 0
br i1 %7, label %22, label %8
8: ; preds = %6
%9 = tail call %struct.hostent* @gethostbyname(i8* %0)
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %22, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%13 = load i64*, i64** %12
%14 = load i64, i64* %13
%15 = inttoptr i64 %14 to %struct.in_addr*
%16 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %15, i64 0, i32 0
%17 = load i32, i32* %16
%18 = icmp eq i32 %1, 0
br i1 %18, label %22, label %19
19: ; preds = %11
%20 = tail call i8* @inet_ntoa(i32 %17)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %3, %19, %11, %6, %8
%23 = phi i32 [ -1, %8 ], [ -1, %6 ], [ %17, %19 ], [ %17, %11 ], [ %4, %3 ]
ret i32 %23
}