#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*/ AF_INET;
scalar_t__ ERANGE;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
scalar_t__ inet_pton(int /*<<< orphan*/, char *, unsigned int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ strstr(char *, char *);
char *strtok(char *, char *);
long strtol(char *, char **, int);
int paser_ip_port(char *ip_port, unsigned int *ip, unsigned short int *port) {
  char *token = NULL;
  char arg[2][128] = {0};
  int cnt = 0;
  char *endptr = NULL;
  long int val = 0;
  if (!strstr(ip_port, ":")) {
    printf("ip_port %s format invalid.\n", ip_port);
    return -1;
  }
  token = strtok(ip_port, ":");
  while (token != NULL) {
    if (cnt >= 2) {
      break;
    }
    strncpy(arg[cnt], token, 128);
    cnt += 1;
    token = strtok(NULL, ",");
  }
  if (inet_pton(AF_INET, arg[0], ip) <= 0) {
    printf("ip %s format invalid.\n", arg[0]);
    return -1;
  }
  errno = 0;
  val = strtol(arg[1], &endptr, 10);
  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
      (errno != 0 && val == 0) || val < 0 || val > 65536) {
    printf("port %s invalid.\n", arg[1]);
    return -1;
  }
  *port = val;
  return 0;
}


x86 assembly (clang, -O0):
<paser_ip_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
movl   $0x0,-0x134(%rbp)
movq   $0x0,-0x140(%rbp)
movq   $0x0,-0x148(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%eax
jne    11f7 <paser_ip_port+0x87>
mov    -0x10(%rbp),%rsi
lea    0xe1c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ad <paser_ip_port+0x23d>
mov    -0x10(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1273 <paser_ip_port+0x103>
cmpl   $0x2,-0x134(%rbp)
jl     1228 <paser_ip_port+0xb8>
jmp    1273 <paser_ip_port+0x103>
movslq -0x134(%rbp),%rax
lea    -0x130(%rbp),%rdi
shl    $0x7,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    $0x80,%edx
call   1030 <strncpy@plt>
mov    -0x134(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x134(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdb9(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    120b <paser_ip_port+0x9b>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    -0x130(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <inet_pton@plt>
cmp    $0x0,%rax
jg     12b5 <paser_ip_port+0x145>
lea    -0x130(%rbp),%rsi
lea    0xd7c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ad <paser_ip_port+0x23d>
mov    0x2d04(%rip),%rax
movq   $0x0,(%rax)
lea    -0x130(%rbp),%rdi
add    $0x80,%rdi
lea    -0x140(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,-0x148(%rbp)
mov    0x2cd0(%rip),%rax
mov    (%rax),%rax
mov    0x2cde(%rip),%rcx
cmp    (%rcx),%rax
jne    1331 <paser_ip_port+0x1c1>
mov    -0x148(%rbp),%rax
mov    0x2cb7(%rip),%rcx
cmp    (%rcx),%rax
je     136f <paser_ip_port+0x1ff>
mov    -0x148(%rbp),%rax
mov    0x2cb8(%rip),%rcx
cmp    (%rcx),%rax
je     136f <paser_ip_port+0x1ff>
mov    0x2c88(%rip),%rax
cmpq   $0x0,(%rax)
je     1350 <paser_ip_port+0x1e0>
cmpq   $0x0,-0x148(%rbp)
je     136f <paser_ip_port+0x1ff>
cmpq   $0x0,-0x148(%rbp)
jl     136f <paser_ip_port+0x1ff>
cmpq   $0x10000,-0x148(%rbp)
jle    1395 <paser_ip_port+0x225>
lea    -0x130(%rbp),%rsi
add    $0x80,%rsi
lea    0xcb3(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ad <paser_ip_port+0x23d>
mov    -0x148(%rbp),%rax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<paser_ip_port>:
push   %r15
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe0d(%rip),%rsi
call   1080 <strstr@plt>
test   %eax,%eax
je     12ef <paser_ip_port+0x18f>
lea    0xdf9(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     125c <paser_ip_port+0xfc>
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
mov    $0x80,%edx
call   1030 <strncpy@plt>
lea    0xdf1(%rip),%rsi
xor    %edi,%edi
call   1070 <strtok@plt>
test   %rax,%rax
je     125c <paser_ip_port+0xfc>
lea    0x90(%rsp),%rdi
mov    %rax,%rsi
mov    $0x80,%edx
call   1030 <strncpy@plt>
lea    0xdc9(%rip),%rsi
xor    %edi,%edi
call   1070 <strtok@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    %r15,%rdx
call   1060 <inet_pton@plt>
test   %rax,%rax
jle    1307 <paser_ip_port+0x1a7>
mov    0x2d3e(%rip),%rbx
movq   $0x0,(%rbx)
lea    0x90(%rsp),%r15
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    (%rbx),%rcx
mov    0x2d2b(%rip),%rdx
cmp    (%rdx),%rcx
jne    12ca <paser_ip_port+0x16a>
mov    0x2d0f(%rip),%rdx
cmp    (%rdx),%rax
je     12fb <paser_ip_port+0x19b>
mov    0x2d1b(%rip),%rdx
cmp    (%rdx),%rax
je     12fb <paser_ip_port+0x19b>
test   %rcx,%rcx
setne  %cl
test   %rax,%rax
sete   %dl
test   %dl,%cl
jne    12fb <paser_ip_port+0x19b>
test   %rax,%rax
js     12fb <paser_ip_port+0x19b>
cmp    $0x10001,%rax
jge    12fb <paser_ip_port+0x19b>
mov    %ax,(%r14)
xor    %eax,%eax
jmp    131d <paser_ip_port+0x1bd>
lea    0xd0c(%rip),%rdi
mov    %rbx,%rsi
jmp    1313 <paser_ip_port+0x1b3>
lea    0xd35(%rip),%rdi
mov    %r15,%rsi
jmp    1313 <paser_ip_port+0x1b3>
lea    0xd12(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c":\00"
@.str.1 = [28 x i8] c"ip_port %s format invalid.\0A\00"
@.str.2 = [2 x i8] c",\00"
@AF_INET = global i32 0
@.str.3 = [23 x i8] c"ip %s format invalid.\0A\00"
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str.4 = [18 x i8] c"port %s invalid.\0A\00"
define i32 @paser_ip_port(i8* %0, i32* %1, i16* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i16*
%8 = alloca i8*
%9 = alloca [2 x [128 x i8]]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
store i8* %0, i8** %5
store i32* %1, i32** %6
store i16* %2, i16** %7
store i8* null, i8** %8
%13 = bitcast [2 x [128 x i8]]* %9 to i8*
store i32 0, i32* %10
store i8* null, i8** %11
store i64 0, i64* %12
%14 = load i8*, i8** %5
%15 = call i32 @strstr(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -1, i32* %4
br label %86
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = call i8* @strtok(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %22, i8** %8
br label %23
23: ; preds = %30, %20
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %40
26: ; preds = %23
%27 = load i32, i32* %10
%28 = icmp sge i32 %27, 2
br i1 %28, label %29, label %30
29: ; preds = %26
br label %40
30: ; preds = %26
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %9, i64 0, i64 %32
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %33, i64 0, i64 0
%35 = load i8*, i8** %8
%36 = call i32 @strncpy(i8* %34, i8* %35, i32 128)
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
%39 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %39, i8** %8
br label %23
40: ; preds = %29, %23
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %9, i64 0, i64 0
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %42, i64 0, i64 0
%44 = load i32*, i32** %6
%45 = call i64 @inet_pton(i32 %41, i8* %43, i32* %44)
%46 = icmp sle i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %40
%48 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %9, i64 0, i64 0
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %48, i64 0, i64 0
%50 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %49)
store i32 -1, i32* %4
br label %86
51: ; preds = %40
store i64 0, i64* @errno
%52 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %9, i64 0, i64 1
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %52, i64 0, i64 0
%54 = call i64 @strtol(i8* %53, i8** %11, i32 10)
store i64 %54, i64* %12
%55 = load i64, i64* @errno
%56 = load i64, i64* @ERANGE
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %66
58: ; preds = %51
%59 = load i64, i64* %12
%60 = load i64, i64* @LONG_MAX
%61 = icmp eq i64 %59, %60
br i1 %61, label %78, label %62
62: ; preds = %58
%63 = load i64, i64* %12
%64 = load i64, i64* @LONG_MIN
%65 = icmp eq i64 %63, %64
br i1 %65, label %78, label %66
66: ; preds = %62, %51
%67 = load i64, i64* @errno
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i64, i64* %12
%71 = icmp eq i64 %70, 0
br i1 %71, label %78, label %72
72: ; preds = %69, %66
%73 = load i64, i64* %12
%74 = icmp slt i64 %73, 0
br i1 %74, label %78, label %75
75: ; preds = %72
%76 = load i64, i64* %12
%77 = icmp sgt i64 %76, 65536
br i1 %77, label %78, label %82
78: ; preds = %75, %72, %69, %62, %58
%79 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %9, i64 0, i64 1
%80 = getelementptr inbounds [128 x i8], [128 x i8]* %79, i64 0, i64 0
%81 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %80)
store i32 -1, i32* %4
br label %86
82: ; preds = %75
%83 = load i64, i64* %12
%84 = trunc i64 %83 to i16
%85 = load i16*, i16** %7
store i16 %84, i16* %85
store i32 0, i32* %4
br label %86
86: ; preds = %82, %78, %47, %17
%87 = load i32, i32* %4
ret i32 %87
}

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
@.str.1 = [28 x i8] c"ip_port %s format invalid.\0A\00"
@.str.2 = [2 x i8] c",\00"
@AF_INET = global i32 0
@.str.3 = [23 x i8] c"ip %s format invalid.\0A\00"
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str.4 = [18 x i8] c"port %s invalid.\0A\00"
define i32 @paser_ip_port(i8* %0, i32* %1, i16* nocapture writeonly %2) {
%4 = alloca [2 x [128 x i8]]
%5 = alloca i8*
%6 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %4, i64 0, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%8 = tail call i32 @strstr(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %54
12: ; preds = %3
%13 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %4, i64 0, i64 0, i64 0
%17 = call i32 @strncpy(i8* %16, i8* %13, i32 128)
%18 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %4, i64 0, i64 1, i64 0
%22 = call i32 @strncpy(i8* %21, i8* %18, i32 128)
%23 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %15, %20, %12
%25 = load i32, i32* @AF_INET
%26 = call i64 @inet_pton(i32 %25, i8* %6, i32* %1)
%27 = icmp slt i64 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %54
30: ; preds = %24
store i64 0, i64* @errno
%31 = getelementptr inbounds [2 x [128 x i8]], [2 x [128 x i8]]* %4, i64 0, i64 1, i64 0
%32 = call i64 @strtol(i8* %31, i8** %5, i32 10)
%33 = load i64, i64* @errno
%34 = load i64, i64* @ERANGE
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %30
%37 = load i64, i64* @LONG_MAX
%38 = icmp eq i64 %32, %37
%39 = load i64, i64* @LONG_MIN
%40 = icmp eq i64 %32, %39
%41 = select i1 %38, i1 true, i1 %40
br i1 %41, label %50, label %42
42: ; preds = %36, %30
%43 = icmp ne i64 %33, 0
%44 = icmp eq i64 %32, 0
%45 = select i1 %43, i1 %44, i1 false
%46 = icmp slt i64 %32, 0
%47 = select i1 %45, i1 true, i1 %46
%48 = icmp sgt i64 %32, 65536
%49 = select i1 %47, i1 true, i1 %48
br i1 %49, label %50, label %52
50: ; preds = %42, %36
%51 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %31)
br label %54
52: ; preds = %42
%53 = trunc i64 %32 to i16
store i16 %53, i16* %2
br label %54
54: ; preds = %52, %50, %28, %10
%55 = phi i32 [ -1, %28 ], [ -1, %50 ], [ 0, %52 ], [ -1, %10 ]
ret i32 %55
}