#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 h_addrtype;
  char *h_name;
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
#define AF_INET 128
struct hostent *gethostbyaddr(scalar_t__, int /*<<< orphan*/, int);
struct hostent *gethostbyname(char *);
char *inet_ntoa(struct in_addr);
scalar_t__ strcmp(char *, char *);
char *canon_host(char *host) {
  struct hostent *he = gethostbyname(host);
  if (he) {
    char *addr = 0;
    switch (he->h_addrtype) {
    case AF_INET:
      addr = inet_ntoa(*(struct in_addr *)he->h_addr);
      break;
    }
    if (addr && strcmp(he->h_name, addr) == 0)
      he = gethostbyaddr(he->h_addr, he->h_length, he->h_addrtype);
    if (he)
      return he->h_name;
  }
  return 0;
}


x86 assembly (clang, -O0):
<canon_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11fa <canon_host+0xba>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1195 <canon_host+0x55>
jmp    1182 <canon_host+0x42>
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d9 <canon_host+0x99>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11d9 <canon_host+0x99>
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1060 <gethostbyaddr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f5 <canon_host+0xb5>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1202 <canon_host+0xc2>
jmp    11fa <canon_host+0xba>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canon_host>:
push   %rbx
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1193 <canon_host+0x53>
mov    %rax,%rbx
cmpl   $0x80,(%rax)
jne    118d <canon_host+0x4d>
mov    0x18(%rbx),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
test   %rax,%rax
je     118d <canon_host+0x4d>
mov    0x8(%rbx),%rdi
mov    %rax,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    118d <canon_host+0x4d>
mov    0x18(%rbx),%rdi
mov    (%rbx),%edx
mov    0x10(%rbx),%esi
call   1060 <gethostbyaddr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1193 <canon_host+0x53>
mov    0x8(%rbx),%rax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.hostent = type { i32, i8*, i32, i64 }
%struct.in_addr = type { i32 }
define i8* @canon_host(i8* %0) {
%2 = tail call %struct.hostent* @gethostbyname(i8* %0)
%3 = icmp eq %struct.hostent* %2, null
br i1 %3, label %32, label %4
4: ; preds = %1
%5 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 0
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 128
br i1 %7, label %8, label %28
8: ; preds = %4
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 3
%10 = load i64, i64* %9
%11 = inttoptr i64 %10 to %struct.in_addr*
%12 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i8* @inet_ntoa(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %28, label %16
16: ; preds = %8
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 1
%18 = load i8*, i8** %17
%19 = tail call i64 @strcmp(i8* %18, i8* %14)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i64, i64* %9
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %2, i64 0, i32 2
%24 = load i32, i32* %23
%25 = load i32, i32* %5
%26 = tail call %struct.hostent* @gethostbyaddr(i64 %22, i32 %24, i32 %25)
%27 = icmp eq %struct.hostent* %26, null
br i1 %27, label %32, label %28
28: ; preds = %8, %16, %4, %21
%29 = phi %struct.hostent* [ %26, %21 ], [ %2, %4 ], [ %2, %16 ], [ %2, %8 ]
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i64 0, i32 1
%31 = load i8*, i8** %30
br label %32
32: ; preds = %28, %21, %1
%33 = phi i8* [ null, %1 ], [ null, %21 ], [ %31, %28 ]
ret i8* %33
}