#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 */

/* Variables and functions */
unsigned short atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
unsigned int hcache_lookup_hostname(char *);
unsigned int inet_addr(char *);
int /*<<< orphan*/ isdigit(unsigned char);
unsigned int ntohl(unsigned int);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int parse_source_address(char *addr, unsigned int *ip, unsigned short *port) {
  char *colon;
  colon = strchr(addr, ':');
  if (colon) {
    *colon = '\0';
    *port = atoi(colon + 1);
    if (colon == addr) {
      return 0;
    }
  } else {
    *port = 0;
  }
  *ip = inet_addr(addr);
  if (*ip == ~0 && !isdigit((unsigned char)*ip)) {
    *ip = hcache_lookup_hostname(addr);
  }
  if (*ip == ~0) {
    fprintf(stderr, "%s: Not an IP address or unknown host name\n", addr);
    return -1;
  }
  *ip = ntohl(*ip);
  return 0;
}


x86 assembly (clang, -O0):
<parse_source_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11de <parse_source_address+0x6e>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11d9 <parse_source_address+0x69>
movl   $0x0,-0x4(%rbp)
jmp    1279 <parse_source_address+0x109>
jmp    11e7 <parse_source_address+0x77>
mov    -0x20(%rbp),%rax
movw   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    122d <parse_source_address+0xbd>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
movzbl %al,%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    122d <parse_source_address+0xbd>
mov    -0x10(%rbp),%rdi
call   1040 <hcache_lookup_hostname@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    125f <parse_source_address+0xef>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb2(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <parse_source_address+0x109>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1090 <ntohl@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_source_address>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11ab <parse_source_address+0x3b>
mov    %rax,%rbx
movb   $0x0,(%rax)
lea    0x1(%rax),%rdi
call   1080 <atoi@plt>
mov    %ax,(%r12)
cmp    %r15,%rbx
jne    11b2 <parse_source_address+0x42>
jmp    11e2 <parse_source_address+0x72>
movw   $0x0,(%r12)
mov    %r15,%rdi
call   1060 <inet_addr@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
jne    11d8 <parse_source_address+0x68>
mov    $0xff,%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11f0 <parse_source_address+0x80>
mov    (%r14),%eax
cmp    $0xffffffff,%eax
je     1200 <parse_source_address+0x90>
mov    %eax,%edi
call   1090 <ntohl@plt>
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1040 <hcache_lookup_hostname@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
jne    11d8 <parse_source_address+0x68>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11e4 <parse_source_address+0x74>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"%s: Not an IP address or unknown host name\0A\00"
define i32 @parse_source_address(i8* %0, i32* %1, i16* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i16*
%8 = alloca i8*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i16* %2, i16** %7
%9 = load i8*, i8** %5
%10 = call i8* @strchr(i8* %9, i8 signext 58)
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %24
13: ; preds = %3
%14 = load i8*, i8** %8
store i8 0, i8* %14
%15 = load i8*, i8** %8
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = call zeroext i16 @atoi(i8* %16)
%18 = load i16*, i16** %7
store i16 %17, i16* %18
%19 = load i8*, i8** %8
%20 = load i8*, i8** %5
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %23
22: ; preds = %13
store i32 0, i32* %4
br label %56
23: ; preds = %13
br label %26
24: ; preds = %3
%25 = load i16*, i16** %7
store i16 0, i16* %25
br label %26
26: ; preds = %24, %23
%27 = load i8*, i8** %5
%28 = call i32 @inet_addr(i8* %27)
%29 = load i32*, i32** %6
store i32 %28, i32* %29
%30 = load i32*, i32** %6
%31 = load i32, i32* %30
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %43
33: ; preds = %26
%34 = load i32*, i32** %6
%35 = load i32, i32* %34
%36 = trunc i32 %35 to i8
%37 = call i32 @isdigit(i8 zeroext %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %33
%40 = load i8*, i8** %5
%41 = call i32 @hcache_lookup_hostname(i8* %40)
%42 = load i32*, i32** %6
store i32 %41, i32* %42
br label %43
43: ; preds = %39, %33, %26
%44 = load i32*, i32** %6
%45 = load i32, i32* %44
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %5
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %49)
store i32 -1, i32* %4
br label %56
51: ; preds = %43
%52 = load i32*, i32** %6
%53 = load i32, i32* %52
%54 = call i32 @ntohl(i32 %53)
%55 = load i32*, i32** %6
store i32 %54, i32* %55
store i32 0, i32* %4
br label %56
56: ; preds = %51, %47, %22
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"%s: Not an IP address or unknown host name\0A\00"
define i32 @parse_source_address(i8* %0, i32* nocapture %1, i16* nocapture writeonly %2) {
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 58)
%5 = icmp eq i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %3
store i8 0, i8* %4
%7 = getelementptr inbounds i8, i8* %4, i64 1
%8 = tail call zeroext i16 @atoi(i8* %7)
store i16 %8, i16* %2
%9 = icmp eq i8* %4, %0
br i1 %9, label %30, label %11
10: ; preds = %3
store i16 0, i16* %2
br label %11
11: ; preds = %6, %10
%12 = tail call i32 @inet_addr(i8* %0)
store i32 %12, i32* %1
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = tail call i32 @isdigit(i8 zeroext -1)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @hcache_lookup_hostname(i8* %0)
store i32 %18, i32* %1
br label %21
19: ; preds = %14
%20 = load i32, i32* %1
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %20, %19 ], [ %18, %17 ]
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %30
27: ; preds = %11, %21
%28 = phi i32 [ %22, %21 ], [ %12, %11 ]
%29 = tail call i32 @ntohl(i32 %28)
store i32 %29, i32* %1
br label %30
30: ; preds = %6, %27, %24
%31 = phi i32 [ -1, %24 ], [ 0, %27 ], [ 0, %6 ]
ret i32 %31
}