#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_int32_t;
typedef int /*<<< orphan*/ u_int16_t;
struct in_addr {
  int /*<<< orphan*/ s_addr;
};

/* Variables and functions */
int host_to_addr(char *, struct in_addr *);
int /*<<< orphan*/ htons(long);
scalar_t__ inet_aton(char *, struct in_addr *);
long service_to_port(char *, int /*<<< orphan*/);
char *strchr(char *, char);
long string_to_number(char *, int /*<<< orphan*/, int);
int parse_addrport(char *buf, u_int16_t proto, u_int32_t *addr,
                   u_int16_t *port) {
  char *pp;
  long prt;
  struct in_addr inaddr;
  pp = strchr(buf, ':');
  if (pp)
    *pp = '\0';
  if (inet_aton(buf, &inaddr) != 0)
    *addr = inaddr.s_addr;
  else if (host_to_addr(buf, &inaddr) != -1)
    *addr = inaddr.s_addr;
  else
    return 0;
  if (pp == NULL)
    return 1;
  if ((prt = string_to_number(pp + 1, 0, 65535)) != -1)
    *port = htons(prt);
  else if ((prt = service_to_port(pp + 1, proto)) != -1)
    *port = htons(prt);
  else
    return 0;
  return 2;
}


x86 assembly (clang, -O0):
<parse_addrport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     119b <parse_addrport+0x3b>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%rax
je     11c0 <parse_addrport+0x60>
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11f5 <parse_addrport+0x95>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1040 <host_to_addr@plt>
cmp    $0xffffffff,%eax
je     11e4 <parse_addrport+0x84>
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11f0 <parse_addrport+0x90>
movl   $0x0,-0x4(%rbp)
jmp    1290 <parse_addrport+0x130>
jmp    11f5 <parse_addrport+0x95>
cmpq   $0x0,-0x30(%rbp)
jne    120c <parse_addrport+0xac>
movl   $0x1,-0x4(%rbp)
jmp    1290 <parse_addrport+0x130>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
xor    %esi,%esi
mov    $0xffff,%edx
call   1070 <string_to_number@plt>
mov    %rax,-0x38(%rbp)
cmp    $0xffffffffffffffff,%rax
je     1244 <parse_addrport+0xe4>
mov    -0x38(%rbp),%rdi
call   1050 <htons@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1289 <parse_addrport+0x129>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    -0x14(%rbp),%esi
call   1030 <service_to_port@plt>
mov    %rax,-0x38(%rbp)
cmp    $0xffffffffffffffff,%rax
je     1278 <parse_addrport+0x118>
mov    -0x38(%rbp),%rdi
call   1050 <htons@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1284 <parse_addrport+0x124>
movl   $0x0,-0x4(%rbp)
jmp    1290 <parse_addrport+0x130>
jmp    1289 <parse_addrport+0x129>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_addrport>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%r14d
mov    %rdi,%rbx
mov    $0x3a,%esi
call   1060 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     118e <parse_addrport+0x2e>
movb   $0x0,0x0(%rbp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <inet_aton@plt>
test   %rax,%rax
jne    11b2 <parse_addrport+0x52>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <host_to_addr@plt>
cmp    $0xffffffff,%eax
je     1204 <parse_addrport+0xa4>
mov    0x8(%rsp),%eax
mov    %eax,(%r12)
test   %rbp,%rbp
je     11fd <parse_addrport+0x9d>
add    $0x1,%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xffff,%edx
call   1070 <string_to_number@plt>
cmp    $0xffffffffffffffff,%rax
jne    11eb <parse_addrport+0x8b>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1030 <service_to_port@plt>
cmp    $0xffffffffffffffff,%rax
je     1206 <parse_addrport+0xa6>
mov    %rax,%rdi
call   1050 <htons@plt>
mov    %eax,(%r15)
mov    $0x2,%ebx
jmp    1206 <parse_addrport+0xa6>
mov    $0x1,%ebx
jmp    1206 <parse_addrport+0xa6>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
define i32 @parse_addrport(i8* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca %struct.in_addr
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%13 = load i8*, i8** %6
%14 = call i8* @strchr(i8* %13, i8 signext 58)
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = load i8*, i8** %10
store i8 0, i8* %18
br label %19
19: ; preds = %17, %4
%20 = load i8*, i8** %6
%21 = call i64 @inet_aton(i8* %20, %struct.in_addr* %12)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32*, i32** %8
store i32 %25, i32* %26
br label %37
27: ; preds = %19
%28 = load i8*, i8** %6
%29 = call i32 @host_to_addr(i8* %28, %struct.in_addr* %12)
%30 = icmp ne i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i32*, i32** %8
store i32 %33, i32* %34
br label %36
35: ; preds = %27
store i32 0, i32* %5
br label %63
36: ; preds = %31
br label %37
37: ; preds = %36, %23
%38 = load i8*, i8** %10
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 1, i32* %5
br label %63
41: ; preds = %37
%42 = load i8*, i8** %10
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = call i64 @string_to_number(i8* %43, i32 0, i32 65535)
store i64 %44, i64* %11
%45 = icmp ne i64 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %41
%47 = load i64, i64* %11
%48 = call i32 @htons(i64 %47)
%49 = load i32*, i32** %9
store i32 %48, i32* %49
br label %62
50: ; preds = %41
%51 = load i8*, i8** %10
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = load i32, i32* %7
%54 = call i64 @service_to_port(i8* %52, i32 %53)
store i64 %54, i64* %11
%55 = icmp ne i64 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %50
%57 = load i64, i64* %11
%58 = call i32 @htons(i64 %57)
%59 = load i32*, i32** %9
store i32 %58, i32* %59
br label %61
60: ; preds = %50
store i32 0, i32* %5
br label %63
61: ; preds = %56
br label %62
62: ; preds = %61, %46
store i32 2, i32* %5
br label %63
63: ; preds = %62, %60, %40, %35
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
define i32 @parse_addrport(i8* %0, i32 %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.in_addr
%6 = bitcast %struct.in_addr* %5 to i8*
%7 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 58)
%8 = icmp eq i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %4
store i8 0, i8* %7
br label %10
10: ; preds = %9, %4
%11 = call i64 @inet_aton(i8* %0, %struct.in_addr* %5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i64 0, i32 0
%15 = load i32, i32* %14
br label %22
16: ; preds = %10
%17 = call i32 @host_to_addr(i8* %0, %struct.in_addr* %5)
%18 = icmp eq i32 %17, -1
br i1 %18, label %34, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i64 0, i32 0
%21 = load i32, i32* %20
br label %22
22: ; preds = %19, %13
%23 = phi i32 [ %21, %19 ], [ %15, %13 ]
store i32 %23, i32* %2
br i1 %8, label %34, label %24
24: ; preds = %22
%25 = getelementptr inbounds i8, i8* %7, i64 1
%26 = call i64 @string_to_number(i8* %25, i32 0, i32 65535)
%27 = icmp eq i64 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i64 @service_to_port(i8* %25, i32 %1)
%30 = icmp eq i64 %29, -1
br i1 %30, label %34, label %31
31: ; preds = %28, %24
%32 = phi i64 [ %26, %24 ], [ %29, %28 ]
%33 = call i32 @htons(i64 %32)
store i32 %33, i32* %3
br label %34
34: ; preds = %28, %22, %16, %31
%35 = phi i32 [ 2, %31 ], [ 0, %16 ], [ 1, %22 ], [ 0, %28 ]
ret i32 %35
}