#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_list;
};

/* Variables and functions */
struct hostent *gethostbyname(char *);
char *inet_ntoa(struct in_addr);
scalar_t__ is_dotted(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int gethost(char *host, char *dotted, int limit) {
  int res = 0;
  if (is_dotted(host))
    strncpy(dotted, host, limit);
  struct hostent *he = gethostbyname(host);
  if (he != NULL) {
    int i = 0;
    if (he->h_addr_list[0] != NULL) {
      char *addr = inet_ntoa(*(struct in_addr *)(he->h_addr_list[i++]));
      if (addr != NULL) {
        snprintf(dotted, limit, "%s", addr);
        res = 1;
      }
    }
  }
  return res;
}


x86 assembly (clang, -O0):
<gethost>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <is_dotted@plt>
cmp    $0x0,%rax
je     118d <gethost+0x3d>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1214 <gethost+0xc4>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmpq   $0x0,(%rax)
je     120f <gethost+0xbf>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120a <gethost+0xba>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rcx
lea    0xe02(%rip),%rdx
call   1050 <snprintf@plt>
movl   $0x1,-0x18(%rbp)
jmp    120f <gethost+0xbf>
jmp    1214 <gethost+0xc4>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethost>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1070 <is_dotted@plt>
test   %rax,%rax
je     1175 <gethost+0x25>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11b5 <gethost+0x65>
mov    (%rax),%rax
mov    (%rax),%rax
test   %rax,%rax
je     11b5 <gethost+0x65>
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
test   %rax,%rax
je     11b5 <gethost+0x65>
lea    0xe5e(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.hostent = type { i32** }
@.str = [3 x i8] c"%s\00"
define i32 @gethost(i8* %0, i8* %1, i32 %2) {
%4 = tail call i64 @is_dotted(i8* %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @strncpy(i8* %1, i8* %0, i32 %2)
br label %8
8: ; preds = %6, %3
%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 i32**, i32*** %12
%14 = load i32*, i32** %13
%15 = icmp eq i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %11
%17 = load i32, i32* %14
%18 = tail call i8* @inet_ntoa(i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %22
22: ; preds = %11, %20, %16, %8
%23 = phi i32 [ 0, %8 ], [ 0, %11 ], [ 1, %20 ], [ 0, %16 ]
ret i32 %23
}