#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*/ uint16_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INET_ADDRSTRLEN;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
char *strrchr(char *, char);
long strtol(char *, char **, int);
int parse_ip4port(const char *ipport, struct sockaddr_in *addrp) {
  char *c, *endptr;
  long port;
  char buf[INET_ADDRSTRLEN + sizeof("65536")];
  (void)strlcpy(buf, ipport, sizeof(buf));
  c = strrchr(buf, ':');
  if (c == NULL) {
    return (-1);
  }
  *c = '\0';
  if (inet_pton(AF_INET, buf, &addrp->sin_addr) != 1) {
    return (-1);
  }
  port = strtol(c + 1, &endptr, 10);
  if (*endptr != '\0' || port <= 0 || port >= 65536) {
    return (-1);
  }
  addrp->sin_family = AF_INET;
  addrp->sin_port = htons((uint16_t)port);
  return (0);
}


x86 assembly (clang, -O0):
<parse_ip4port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
movslq (%rax),%rcx
mov    %rcx,%rax
add    $0x6,%rax
mov    %rsp,%rdx
mov    %rdx,-0x38(%rbp)
add    $0x15,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rsi
mov    %eax,%edx
call   1070 <strlcpy@plt>
mov    -0x50(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cc <parse_ip4port+0x7c>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1290 <parse_ip4port+0x140>
mov    -0x50(%rbp),%rsi
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
add    $0x8,%rdx
call   1060 <inet_pton@plt>
cmp    $0x1,%eax
je     1209 <parse_ip4port+0xb9>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1290 <parse_ip4port+0x140>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    124c <parse_ip4port+0xfc>
cmpq   $0x0,-0x30(%rbp)
jle    124c <parse_ip4port+0xfc>
cmpq   $0x10000,-0x30(%rbp)
jl     125f <parse_ip4port+0x10f>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1290 <parse_ip4port+0x140>
mov    0x2d82(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%rax
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ip4port>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rsi
mov    %rsp,%r13
mov    0x2e6a(%rip),%rax
movslq (%rax),%rax
lea    0x6(%rax),%edx
mov    %rsp,%rbx
add    $0x15,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1070 <strlcpy@plt>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strrchr@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     120a <parse_ip4port+0xba>
mov    %rax,%r12
movb   $0x0,(%rax)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0x8(%r14),%rdx
mov    %rbx,%rsi
call   1060 <inet_pton@plt>
cmp    $0x1,%eax
jne    120a <parse_ip4port+0xba>
add    $0x1,%r12
lea    -0x30(%rbp),%rsi
mov    %r12,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    -0x30(%rbp),%rcx
cmpb   $0x0,(%rcx)
jne    120a <parse_ip4port+0xba>
test   %rax,%rax
jle    120a <parse_ip4port+0xba>
cmp    $0xffff,%rax
jg     120a <parse_ip4port+0xba>
mov    0x2df1(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%r14)
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,(%r14)
xor    %r15d,%r15d
mov    %r13,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
define i32 @parse_ip4port(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%12 = load i32, i32* @INET_ADDRSTRLEN
%13 = sext i32 %12 to i64
%14 = add i64 %13, 6
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = load i8*, i8** %4
%18 = trunc i64 %14 to i32
%19 = call i32 @strlcpy(i8* %16, i8* %17, i32 %18)
%20 = call i8* @strrchr(i8* %16, i8 signext 58)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %11
br label %56
24: ; preds = %2
%25 = load i8*, i8** %6
store i8 0, i8* %25
%26 = load i32, i32* @AF_INET
%27 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i32 0, i32 2
%29 = call i32 @inet_pton(i32 %26, i8* %16, i32* %28)
%30 = icmp ne i32 %29, 1
br i1 %30, label %31, label %32
31: ; preds = %24
store i32 -1, i32* %3
store i32 1, i32* %11
br label %56
32: ; preds = %24
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = call i64 @strtol(i8* %34, i8** %7, i32 10)
store i64 %35, i64* %8
%36 = load i8*, i8** %7
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %32
%41 = load i64, i64* %8
%42 = icmp sle i64 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %40
%44 = load i64, i64* %8
%45 = icmp sge i64 %44, 65536
br i1 %45, label %46, label %47
46: ; preds = %43, %40, %32
store i32 -1, i32* %3
store i32 1, i32* %11
br label %56
47: ; preds = %43
%48 = load i32, i32* @AF_INET
%49 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %49, i32 0, i32 1
store i32 %48, i32* %50
%51 = load i64, i64* %8
%52 = trunc i64 %51 to i32
%53 = call i32 @htons(i32 %52)
%54 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i32 0, i32 0
store i32 %53, i32* %55
store i32 0, i32* %3
store i32 1, i32* %11
br label %56
56: ; preds = %47, %46, %31, %23
%57 = load i8*, i8** %9
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
define i32 @parse_ip4port(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = load i32, i32* @INET_ADDRSTRLEN
%6 = sext i32 %5 to i64
%7 = add nsw i64 %6, 6
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = trunc i64 %7 to i32
%11 = call i32 @strlcpy(i8* %9, i8* %0, i32 %10)
%12 = call i8* @strrchr(i8* %9, i8 signext 58)
%13 = icmp eq i8* %12, null
br i1 %13, label %35, label %14
14: ; preds = %2
store i8 0, i8* %12
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
%17 = call i32 @inet_pton(i32 %15, i8* %9, i32* %16)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %35
19: ; preds = %14
%20 = getelementptr inbounds i8, i8* %12, i64 1
%21 = call i64 @strtol(i8* %20, i8** %3, i32 10)
%22 = load i8*, i8** %3
%23 = load i8, i8* %22
%24 = icmp ne i8 %23, 0
%25 = icmp slt i64 %21, 1
%26 = select i1 %24, i1 true, i1 %25
%27 = icmp sgt i64 %21, 65535
%28 = select i1 %26, i1 true, i1 %27
br i1 %28, label %35, label %29
29: ; preds = %19
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %30, i32* %31
%32 = trunc i64 %21 to i32
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %33, i32* %34
br label %35
35: ; preds = %19, %14, %2, %29
%36 = phi i32 [ 0, %29 ], [ -1, %2 ], [ -1, %14 ], [ -1, %19 ]
ret i32 %36
}