#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 */
char *malloc(scalar_t__);
char *rindex(char const *, char);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int split_host_port(char const *str, char **host, unsigned int *port) {
  char *colon;
  colon = rindex(str, ':');
  if (colon == NULL)
    return -1;
  *colon = '\0';
  *host = malloc(strlen(str) + 1);
  strcpy(*host, str);
  sscanf(colon + 1, "%u", port);
  return 0;
}


x86 assembly (clang, -O0):
<split_host_port>:
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   1070 <rindex@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118d <split_host_port+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <split_host_port+0x92>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rdx
lea    0xe2a(%rip),%rsi
call   1060 <sscanf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_host_port>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    $0x3a,%esi
call   1070 <rindex@plt>
test   %rax,%rax
je     11b0 <split_host_port+0x60>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,(%r12)
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
add    $0x1,%rbx
lea    0xe5f(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <sscanf@plt>
xor    %eax,%eax
jmp    11b5 <split_host_port+0x65>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%u\00"
define i32 @split_host_port(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = call i8* @rindex(i8* %9, i8 signext 58)
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %29
14: ; preds = %3
%15 = load i8*, i8** %8
store i8 0, i8* %15
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = call i8* @malloc(i64 %18)
%20 = load i8**, i8*** %6
store i8* %19, i8** %20
%21 = load i8**, i8*** %6
%22 = load i8*, i8** %21
%23 = load i8*, i8** %5
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = load i8*, i8** %8
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i32*, i32** %7
%28 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %27)
store i32 0, i32* %4
br label %29
29: ; preds = %14, %13
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@.str = [3 x i8] c"%u\00"
define i32 @split_host_port(i8* %0, i8** nocapture writeonly %1, i32* %2) {
%4 = tail call i8* @rindex(i8* %0, i8 signext 58)
%5 = icmp eq i8* %4, null
br i1 %5, label %13, label %6
6: ; preds = %3
store i8 0, i8* %4
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = add nsw i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
store i8* %9, i8** %1
%10 = tail call i32 @strcpy(i8* %9, i8* %0)
%11 = getelementptr inbounds i8, i8* %4, i64 1
%12 = tail call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %13
13: ; preds = %3, %6
%14 = phi i32 [ 0, %6 ], [ -1, %3 ]
ret i32 %14
}