#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 */
int /*<<< orphan*/ check_valid(int, char *, char *);
char *optarg;
int sscanf(char *, char *, int *);
char *strchr(char *, int);
scalar_t__ strlen(char *);
int get_host_ports(char **host, int *port, int *src_port, char *arg) {
  char *port_str = NULL, *host_str = NULL, *src_port_str = NULL, *p, *p2;
  if ((p = strchr(arg, (int)':')) == NULL) {
    port_str = arg;
  } else {
    *p = '\0';
    p++;
    if ((p2 = strchr(p, (int)':')) == NULL) {
      host_str = arg;
      port_str = p;
    } else {
      *p2 = '\0';
      p2++;
      src_port_str = arg;
      host_str = p;
      port_str = p2;
    }
  }
  check_valid(sscanf(port_str, "%d", port) == 1,
              "`%s' does not have a valid port\n", port_str);
  if (src_port_str != NULL) {
    check_valid(sscanf(optarg, "%d", src_port) == 1,
                "`%s' does not have a valid source port\n", src_port_str);
  } else {
    *port_str = -1;
  }
  if (host_str != NULL && strlen(host_str) > 0) {
    *host = host_str;
  } else {
    *host = "localhost";
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_host_ports>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1199 <get_host_ports+0x59>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    120d <get_host_ports+0xcd>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    11dd <get_host_ports+0x9d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1208 <get_host_ports+0xc8>
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    120d <get_host_ports+0xcd>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xde4(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    -0x28(%rbp),%rdx
lea    0xdcc(%rip),%rsi
call   1030 <check_valid@plt>
cmpq   $0x0,-0x38(%rbp)
je     1281 <get_host_ports+0x141>
mov    0x2daa(%rip),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rdx
lea    0xda4(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    -0x38(%rbp),%rdx
lea    0xdad(%rip),%rsi
call   1030 <check_valid@plt>
jmp    1288 <get_host_ports+0x148>
mov    -0x28(%rbp),%rax
movb   $0xff,(%rax)
cmpq   $0x0,-0x30(%rbp)
je     12b6 <get_host_ports+0x176>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jle    12b6 <get_host_ports+0x176>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    12c4 <get_host_ports+0x184>
mov    -0x8(%rbp),%rax
lea    0xd8b(%rip),%rcx
mov    %rcx,(%rax)
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_host_ports>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
mov    %rcx,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1191 <get_host_ports+0x51>
mov    %rax,%rbx
movb   $0x0,(%rax)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1198 <get_host_ports+0x58>
mov    %rax,%rbp
movb   $0x0,(%rax)
add    $0x1,%rbp
jmp    11a1 <get_host_ports+0x61>
xor    %ebx,%ebx
mov    %r15,%rbp
jmp    119e <get_host_ports+0x5e>
mov    %rbx,%rbp
mov    %r15,%rbx
xor    %r15d,%r15d
lea    0xe58(%rip),%rsi
mov    %rbp,%rdi
mov    %r13,%rdx
call   1060 <sscanf@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
lea    0xe40(%rip),%rsi
mov    %rbp,%rdx
call   1030 <check_valid@plt>
test   %r15,%r15
je     1208 <get_host_ports+0xc8>
mov    0x2e21(%rip),%rax
mov    (%rax),%rdi
lea    0xe1f(%rip),%rsi
mov    %r12,%rdx
call   1060 <sscanf@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
lea    0xe2b(%rip),%rsi
mov    %r15,%rdx
call   1030 <check_valid@plt>
test   %rbx,%rbx
jne    1211 <get_host_ports+0xd1>
jmp    121e <get_host_ports+0xde>
movb   $0xff,0x0(%rbp)
test   %rbx,%rbx
je     121e <get_host_ports+0xde>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
jg     1225 <get_host_ports+0xe5>
lea    0xe27(%rip),%rbx
mov    %rbx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [33 x i8] c"`%s' does not have a valid port\0A\00"
@optarg = global i8* null
@.str.2 = [40 x i8] c"`%s' does not have a valid source port\0A\00"
@.str.3 = [10 x i8] c"localhost\00"
define i32 @get_host_ports(i8** %0, i32* %1, i32* %2, i8* %3) {
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i8* %3, i8** %8
store i8* null, i8** %9
store i8* null, i8** %10
store i8* null, i8** %11
%14 = load i8*, i8** %8
%15 = call i8* @strchr(i8* %14, i32 58)
store i8* %15, i8** %12
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = load i8*, i8** %8
store i8* %18, i8** %9
br label %37
19: ; preds = %4
%20 = load i8*, i8** %12
store i8 0, i8* %20
%21 = load i8*, i8** %12
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %12
%23 = load i8*, i8** %12
%24 = call i8* @strchr(i8* %23, i32 58)
store i8* %24, i8** %13
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i8*, i8** %8
store i8* %27, i8** %10
%28 = load i8*, i8** %12
store i8* %28, i8** %9
br label %36
29: ; preds = %19
%30 = load i8*, i8** %13
store i8 0, i8* %30
%31 = load i8*, i8** %13
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %13
%33 = load i8*, i8** %8
store i8* %33, i8** %11
%34 = load i8*, i8** %12
store i8* %34, i8** %10
%35 = load i8*, i8** %13
store i8* %35, i8** %9
br label %36
36: ; preds = %29, %26
br label %37
37: ; preds = %36, %17
%38 = load i8*, i8** %9
%39 = load i32*, i32** %6
%40 = call i32 @sscanf(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %39)
%41 = icmp eq i32 %40, 1
%42 = zext i1 %41 to i32
%43 = load i8*, i8** %9
%44 = call i32 @check_valid(i32 %42, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = load i8*, i8** %11
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %55
47: ; preds = %37
%48 = load i8*, i8** @optarg
%49 = load i32*, i32** %7
%50 = call i32 @sscanf(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %49)
%51 = icmp eq i32 %50, 1
%52 = zext i1 %51 to i32
%53 = load i8*, i8** %11
%54 = call i32 @check_valid(i32 %52, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %53)
br label %57
55: ; preds = %37
%56 = load i8*, i8** %9
store i8 -1, i8* %56
br label %57
57: ; preds = %55, %47
%58 = load i8*, i8** %10
%59 = icmp ne i8* %58, null
br i1 %59, label %60, label %67
60: ; preds = %57
%61 = load i8*, i8** %10
%62 = call i64 @strlen(i8* %61)
%63 = icmp sgt i64 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %60
%65 = load i8*, i8** %10
%66 = load i8**, i8*** %5
store i8* %65, i8** %66
br label %69
67: ; preds = %60, %57
%68 = load i8**, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8** %68
br label %69
69: ; preds = %67, %64
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [33 x i8] c"`%s' does not have a valid port\0A\00"
@optarg = global i8* null
@.str.2 = [40 x i8] c"`%s' does not have a valid source port\0A\00"
@.str.3 = [10 x i8] c"localhost\00"
define i32 @get_host_ports(i8** nocapture writeonly %0, i32* %1, i32* %2, i8* %3) {
%5 = tail call i8* @strchr(i8* dereferenceable(1) %3, i32 58)
%6 = icmp eq i8* %5, null
br i1 %6, label %13, label %7
7: ; preds = %4
store i8 0, i8* %5
%8 = getelementptr inbounds i8, i8* %5, i64 1
%9 = tail call i8* @strchr(i8* %8, i32 58)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %7
store i8 0, i8* %9
%12 = getelementptr inbounds i8, i8* %9, i64 1
br label %13
13: ; preds = %7, %4, %11
%14 = phi i8* [ %12, %11 ], [ %3, %4 ], [ %8, %7 ]
%15 = phi i8* [ %8, %11 ], [ null, %4 ], [ %3, %7 ]
%16 = phi i8* [ %3, %11 ], [ null, %4 ], [ null, %7 ]
%17 = tail call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%18 = icmp eq i32 %17, 1
%19 = zext i1 %18 to i32
%20 = tail call i32 @check_valid(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%21 = icmp eq i8* %16, null
br i1 %21, label %28, label %22
22: ; preds = %13
%23 = load i8*, i8** @optarg
%24 = tail call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%25 = icmp eq i32 %24, 1
%26 = zext i1 %25 to i32
%27 = tail call i32 @check_valid(i32 %26, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %29
28: ; preds = %13
store i8 -1, i8* %14
br label %29
29: ; preds = %28, %22
%30 = icmp eq i8* %15, null
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = tail call i64 @strlen(i8* %15)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %31, %29
br label %35
35: ; preds = %31, %34
%36 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %34 ], [ %15, %31 ]
store i8* %36, i8** %0
ret i32 0
}