#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;

/* Variables and functions */
int AF_INET;
int AF_INET6;
int /*<<< orphan*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
char *strsep(char **, char *);
scalar_t__ strstr(char *, char *);
int parse_ip_and_port(char *line, char *ip, uint16_t *port) {
  char *needle;
  char *iptmp = NULL;
  int af = AF_INET;
  if (strstr(line, "[") && (strstr(line, "]"))) {
    af = AF_INET6;
    needle = strsep(&line, "[");
    needle = strsep(&line, "]");
    iptmp = needle;
  }
  needle = strsep(&line, ":");
  *port = atoi(line);
  if (af == AF_INET) {
    iptmp = needle;
  }
  strncpy(ip, iptmp, INET6_ADDRSTRLEN);
  return af;
}


x86 assembly (clang, -O0):
<parse_ip_and_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     11d8 <parse_ip_and_port+0x98>
mov    -0x8(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     11d8 <parse_ip_and_port+0x98>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x8(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1040 <strsep@plt>
mov    %rax,-0x20(%rbp)
lea    -0x8(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1040 <strsep@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x8(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1040 <strsep@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%eax
jne    1217 <parse_ip_and_port+0xd7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ip_and_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rdi,(%rsp)
mov    0x2e81(%rip),%r13
mov    0x0(%r13),%r12d
lea    0xe96(%rip),%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11b8 <parse_ip_and_port+0x78>
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     11b8 <parse_ip_and_port+0x78>
mov    0x2e49(%rip),%rax
mov    (%rax),%r12d
lea    0xe67(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strsep@plt>
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strsep@plt>
mov    %rax,%rbx
jmp    11ba <parse_ip_and_port+0x7a>
xor    %ebx,%ebx
lea    0xe43(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strsep@plt>
mov    %rax,%rbp
mov    (%rsp),%rdi
call   1050 <atoi@plt>
mov    %eax,(%r15)
cmp    0x0(%r13),%r12d
cmovne %rbx,%rbp
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AF_INET = global i32 0
@.str = [2 x i8] c"[\00"
@.str.1 = [2 x i8] c"]\00"
@AF_INET6 = global i32 0
@.str.2 = [2 x i8] c":\00"
@INET6_ADDRSTRLEN = global i32 0
define i32 @parse_ip_and_port(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i8* null, i8** %8
%10 = load i32, i32* @AF_INET
store i32 %10, i32* %9
%11 = load i8*, i8** %4
%12 = call i64 @strstr(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i64 @strstr(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @AF_INET6
store i32 %19, i32* %9
%20 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %20, i8** %7
%21 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %21, i8** %7
%22 = load i8*, i8** %7
store i8* %22, i8** %8
br label %23
23: ; preds = %18, %14, %3
%24 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %24, i8** %7
%25 = load i8*, i8** %4
%26 = call i32 @atoi(i8* %25)
%27 = load i32*, i32** %6
store i32 %26, i32* %27
%28 = load i32, i32* %9
%29 = load i32, i32* @AF_INET
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i8*, i8** %7
store i8* %32, i8** %8
br label %33
33: ; preds = %31, %23
%34 = load i8*, i8** %5
%35 = load i8*, i8** %8
%36 = load i32, i32* @INET6_ADDRSTRLEN
%37 = call i32 @strncpy(i8* %34, i8* %35, i32 %36)
%38 = load i32, i32* %9
ret i32 %38
}

LLVM IR (-O2):
@AF_INET = global i32 0
@.str = [2 x i8] c"[\00"
@.str.1 = [2 x i8] c"]\00"
@AF_INET6 = global i32 0
@.str.2 = [2 x i8] c":\00"
@INET6_ADDRSTRLEN = global i32 0
define i32 @parse_ip_and_port(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca i8*
store i8* %0, i8** %4
%5 = load i32, i32* @AF_INET
%6 = tail call i64 @strstr(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %3
%9 = tail call i64 @strstr(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = load i32, i32* @AF_INET6
%13 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %11, %8, %3
%16 = phi i8* [ %14, %11 ], [ null, %8 ], [ null, %3 ]
%17 = phi i32 [ %12, %11 ], [ %5, %8 ], [ %5, %3 ]
%18 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = load i8*, i8** %4
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %2
%21 = load i32, i32* @AF_INET
%22 = icmp eq i32 %17, %21
%23 = select i1 %22, i8* %18, i8* %16
%24 = load i32, i32* @INET6_ADDRSTRLEN
%25 = call i32 @strncpy(i8* %1, i8* %23, i32 %24)
ret i32 %17
}