#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 */
typedef int /*<<< orphan*/ u_long;
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ stderr;
struct in_addr getaddr(char *addr) {
  struct in_addr ipaddr;
  if (*addr >= '0' && *addr <= '9') {
    ipaddr.s_addr = inet_addr(addr);
  } else {
    struct hostent *hst;
    hst = gethostbyname(addr);
    if (hst == 0) {
      fprintf(stderr, "Unknown host: %s\n", addr);
      exit(1);
    }
    ipaddr.s_addr = *(u_long *)hst->h_addr;
  }
  return ipaddr;
}


x86 assembly (clang, -O0):
<getaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     117d <getaddr+0x3d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     117d <getaddr+0x3d>
mov    -0x10(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %eax,-0x8(%rbp)
jmp    11c4 <getaddr+0x84>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <getaddr+0x78>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe57(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaddr>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%al
add    $0xd0,%al
cmp    $0x9,%al
ja     1155 <getaddr+0x15>
mov    %rbx,%rdi
pop    %rbx
jmp    1030 <inet_addr@plt>
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1169 <getaddr+0x29>
mov    (%rax),%rax
mov    (%rax),%eax
pop    %rbx
ret
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
@stderr = global i32 0
@.str = [18 x i8] c"Unknown host: %s\0A\00"
define i32 @getaddr(i8* %0) {
%2 = alloca %struct.in_addr
%3 = alloca i8*
%4 = alloca %struct.hostent*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
%8 = icmp sge i32 %7, 48
br i1 %8, label %9, label %18
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp sle i32 %12, 57
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = call i32 @inet_addr(i8* %15)
%17 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i32 0, i32 0
store i32 %16, i32* %17
br label %35
18: ; preds = %9, %1
%19 = load i8*, i8** %3
%20 = call %struct.hostent* @gethostbyname(i8* %19)
store %struct.hostent* %20, %struct.hostent** %4
%21 = load %struct.hostent*, %struct.hostent** %4
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %3
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = load %struct.hostent*, %struct.hostent** %4
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i32*
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i32 0, i32 0
store i32 %33, i32* %34
br label %35
35: ; preds = %28, %14
%36 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i32 0, i32 0
%37 = load i32, i32* %36
ret i32 %37
}

LLVM IR (-O2):
%struct.hostent = type { i64 }
@stderr = global i32 0
@.str = [18 x i8] c"Unknown host: %s\0A\00"
define i32 @getaddr(i8* %0) {
%2 = load i8, i8* %0
%3 = add i8 %2, -48
%4 = icmp ult i8 %3, 10
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @inet_addr(i8* %0)
br label %19
7: ; preds = %1
%8 = tail call %struct.hostent* @gethostbyname(i8* %0)
%9 = icmp eq %struct.hostent* %8, null
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %7
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %8, i64 0, i32 0
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i32*
%18 = load i32, i32* %17
br label %19
19: ; preds = %14, %5
%20 = phi i32 [ %6, %5 ], [ %18, %14 ]
ret i32 %20
}