#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 */
size_t HOST_NAME_MAX;
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char const *, char);
int hostport_parse(const char *str, char *hostname, char *port) {
  char *semicolon_pos;
  size_t cpy_cnt;
  semicolon_pos = strrchr(str, ':');
  if (semicolon_pos == NULL)
    return -1;
  cpy_cnt = semicolon_pos - str;
  if (cpy_cnt > HOST_NAME_MAX)
    cpy_cnt = HOST_NAME_MAX - 1;
  strncpy(hostname, str, cpy_cnt);
  hostname[cpy_cnt] = '\0';
  strncpy(port, (semicolon_pos + 1), 6);
  port[6 - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<hostport_parse>:
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   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    115d <hostport_parse+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <hostport_parse+0xb6>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
jbe    1192 <hostport_parse+0x72>
mov    0x2e61(%rip),%rax
mov    (%rax),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,0x5(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostport_parse>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x3a,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     118c <hostport_parse+0x6c>
mov    %rax,%rbx
sub    %r12,%rax
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%rcx
lea    -0x1(%rcx),%r13
cmp    %rcx,%rax
cmovbe %rax,%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%r15,%r13,1)
add    $0x1,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x5(%r14)
xor    %eax,%eax
jmp    1191 <hostport_parse+0x71>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@HOST_NAME_MAX = global i64 0
define i32 @hostport_parse(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = call i8* @strrchr(i8* %10, i8 signext 58)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %42
15: ; preds = %3
%16 = load i8*, i8** %8
%17 = load i8*, i8** %5
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = load i64, i64* @HOST_NAME_MAX
%23 = icmp ugt i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i64, i64* @HOST_NAME_MAX
%26 = sub i64 %25, 1
store i64 %26, i64* %9
br label %27
27: ; preds = %24, %15
%28 = load i8*, i8** %6
%29 = load i8*, i8** %5
%30 = load i64, i64* %9
%31 = trunc i64 %30 to i32
%32 = call i32 @strncpy(i8* %28, i8* %29, i32 %31)
%33 = load i8*, i8** %6
%34 = load i64, i64* %9
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %7
%37 = load i8*, i8** %8
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = call i32 @strncpy(i8* %36, i8* %38, i32 6)
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 5
store i8 0, i8* %41
store i32 0, i32* %4
br label %42
42: ; preds = %27, %14
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@HOST_NAME_MAX = global i64 0
define i32 @hostport_parse(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 58)
%5 = icmp eq i8* %4, null
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = ptrtoint i8* %4 to i64
%8 = ptrtoint i8* %0 to i64
%9 = sub i64 %7, %8
%10 = load i64, i64* @HOST_NAME_MAX
%11 = icmp ugt i64 %9, %10
%12 = add i64 %10, -1
%13 = select i1 %11, i64 %12, i64 %9
%14 = trunc i64 %13 to i32
%15 = tail call i32 @strncpy(i8* %1, i8* %0, i32 %14)
%16 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 0, i8* %16
%17 = getelementptr inbounds i8, i8* %4, i64 1
%18 = tail call i32 @strncpy(i8* %2, i8* %17, i32 6)
%19 = getelementptr inbounds i8, i8* %2, i64 5
store i8 0, i8* %19
br label %20
20: ; preds = %3, %6
%21 = phi i32 [ 0, %6 ], [ -1, %3 ]
ret i32 %21
}