#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 {
  char **h_addr_list;
};

/* Variables and functions */
struct hostent *gethostbyname(char *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ isdigit(char);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ validaddr(char *);
char *resolve(char *addr) {
  static char address[40];
  struct hostent *hostptr;
  struct in_addr *ptr;
  char **listptr;
  strcpy(address, addr);
  if (!isdigit(address[0])) {
    hostptr = gethostbyname(addr);
    if (!hostptr) {
      return ((char *)0);
    }
    listptr = hostptr->h_addr_list;
    ptr = (struct in_addr *)*listptr;
    strcpy(address, inet_ntoa(*ptr));
    return address;
  } else {
    if (!validaddr(address)) {
      return ((char *)0);
    }
    return address;
  }
  return "";
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0x2ef9(%rip),%rdi
call   1040 <strcpy@plt>
movsbl 0x2eed(%rip),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    11e6 <resolve+0x96>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a6 <resolve+0x56>
movq   $0x0,-0x8(%rbp)
jmp    1213 <resolve+0xc3>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0x2e8f(%rip),%rdi
call   1040 <strcpy@plt>
lea    0x2e83(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1213 <resolve+0xc3>
lea    0x2e73(%rip),%rdi
call   1060 <validaddr@plt>
cmp    $0x0,%eax
jne    1208 <resolve+0xb8>
movq   $0x0,-0x8(%rbp)
jmp    1213 <resolve+0xc3>
lea    0x2e51(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0x2f02(%rip),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
movsbl 0x2ef0(%rip),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     118b <resolve+0x3b>
mov    %rbx,%rdi
call   1060 <validaddr@plt>
xor    %ecx,%ecx
test   %eax,%eax
cmove  %rcx,%rbx
jmp    11b4 <resolve+0x64>
mov    %r14,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     11b2 <resolve+0x62>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1050 <inet_ntoa@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
jmp    11b4 <resolve+0x64>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.hostent = type { i8** }
%struct.in_addr = type { i32 }
@resolve.address = internal global [40 x i8] zeroinitializer
define i8* @resolve(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.hostent*
%5 = alloca %struct.in_addr*
%6 = alloca i8**
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @strcpy(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0), i8* %7)
%9 = load i8, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0)
%10 = call i32 @isdigit(i8 signext %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %30, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call %struct.hostent* @gethostbyname(i8* %13)
store %struct.hostent* %14, %struct.hostent** %4
%15 = load %struct.hostent*, %struct.hostent** %4
%16 = icmp ne %struct.hostent* %15, null
br i1 %16, label %18, label %17
17: ; preds = %12
store i8* null, i8** %2
br label %35
18: ; preds = %12
%19 = load %struct.hostent*, %struct.hostent** %4
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 0
%21 = load i8**, i8*** %20
store i8** %21, i8*** %6
%22 = load i8**, i8*** %6
%23 = load i8*, i8** %22
%24 = bitcast i8* %23 to %struct.in_addr*
store %struct.in_addr* %24, %struct.in_addr** %5
%25 = load %struct.in_addr*, %struct.in_addr** %5
%26 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i8* @inet_ntoa(i32 %27)
%29 = call i32 @strcpy(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0), i8* %28)
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0), i8** %2
br label %35
30: ; preds = %1
%31 = call i32 @validaddr(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %30
store i8* null, i8** %2
br label %35
34: ; preds = %30
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0), i8** %2
br label %35
35: ; preds = %34, %33, %18, %17
%36 = load i8*, i8** %2
ret i8* %36
}

LLVM IR (-O2):
%struct.hostent = type { i8** }
%struct.in_addr = type { i32 }
@resolve.address = internal global [40 x i8] zeroinitializer
define i8* @resolve(i8* %0) {
%2 = tail call i32 @strcpy(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0), i8* %0)
%3 = load i8, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0)
%4 = tail call i32 @isdigit(i8 signext %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %1
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %21, label %9
9: ; preds = %6
%10 = bitcast %struct.hostent* %7 to %struct.in_addr***
%11 = load %struct.in_addr**, %struct.in_addr*** %10
%12 = load %struct.in_addr*, %struct.in_addr** %11
%13 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i8* @inet_ntoa(i32 %14)
%16 = tail call i32 @strcpy(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0), i8* %15)
br label %21
17: ; preds = %1
%18 = tail call i32 @validaddr(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0))
%19 = icmp eq i32 %18, 0
%20 = select i1 %19, i8* null, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0)
br label %21
21: ; preds = %17, %6, %9
%22 = phi i8* [ getelementptr inbounds ([40 x i8], [40 x i8]* @resolve.address, i64 0, i64 0), %9 ], [ null, %6 ], [ %20, %17 ]
ret i8* %22
}