#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, int);
char *strdup(char *);
char *strtok(char *, char *);
scalar_t__ tokenize_host_port(char *, char *, int *);
char **portals_arg_to_urls(char *transport, char *portals_arg,
                           uint32_t *urls_vec_len) {
  char *token;
  char delim[] = ";";
  char *str;
  char host[256];
  char url[1024];
  char *array[1024];
  char **vec = NULL;
  uint16_t port;
  int n = 0, i;
  str = strdup(portals_arg);
  token = strtok(str, delim);
  while (token != NULL && n < 1024) {
    if (tokenize_host_port(token, host, &port))
      goto cleanup;
    sprintf(url, "%s://%s:%d", transport, host, port);
    array[n] = strdup(url);
    n++;
    token = strtok(NULL, delim);
  }
  if (n > 0) {
    vec = (char **)calloc(n + 1, sizeof(*vec));
    if (vec == NULL)
      goto cleanup;
    for (i = 0; i < n; i++)
      vec[i] = array[i];
    *urls_vec_len = n;
  }
cleanup:
  free(str);
  return vec;
}


x86 assembly (clang, -O0):
<portals_arg_to_urls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2550,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0xe82(%rip),%ax
mov    %ax,-0x22(%rbp)
movq   $0x0,-0x2538(%rbp)
movl   $0x0,-0x2540(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x22(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x2545(%rbp)
je     11db <portals_arg_to_urls+0x7b>
cmpl   $0x400,-0x2540(%rbp)
setl   %al
mov    %al,-0x2545(%rbp)
mov    -0x2545(%rbp),%al
test   $0x1,%al
jne    11ee <portals_arg_to_urls+0x8e>
jmp    127c <portals_arg_to_urls+0x11c>
mov    -0x20(%rbp),%rdi
lea    -0x130(%rbp),%rsi
lea    -0x253c(%rbp),%rdx
call   1080 <tokenize_host_port@plt>
cmp    $0x0,%rax
je     1214 <portals_arg_to_urls+0xb4>
jmp    1318 <portals_arg_to_urls+0x1b8>
lea    -0x530(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    -0x130(%rbp),%rcx
mov    -0x253c(%rbp),%r8d
lea    0xdce(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x530(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,%rcx
movslq -0x2540(%rbp),%rax
mov    %rcx,-0x2530(%rbp,%rax,8)
ff
mov    -0x2540(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2540(%rbp)
lea    -0x22(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11b5 <portals_arg_to_urls+0x55>
cmpl   $0x0,-0x2540(%rbp)
jle    1313 <portals_arg_to_urls+0x1b3>
mov    -0x2540(%rbp),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x2538(%rbp)
cmpq   $0x0,-0x2538(%rbp)
jne    12b6 <portals_arg_to_urls+0x156>
jmp    1318 <portals_arg_to_urls+0x1b8>
movl   $0x0,-0x2544(%rbp)
mov    -0x2544(%rbp),%eax
cmp    -0x2540(%rbp),%eax
jge    1307 <portals_arg_to_urls+0x1a7>
movslq -0x2544(%rbp),%rax
mov    -0x2530(%rbp,%rax,8),%rdx
ff
mov    -0x2538(%rbp),%rax
movslq -0x2544(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2544(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2544(%rbp)
jmp    12c0 <portals_arg_to_urls+0x160>
mov    -0x2540(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1318 <portals_arg_to_urls+0x1b8>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2538(%rbp),%rax
add    $0x2550,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<portals_arg_to_urls>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2528,%rsp
mov    %rdx,%rbp
mov    %rdi,%r13
movw   $0x3b,0x6(%rsp)
mov    %rsi,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
lea    0x6(%rsp),%rsi
mov    %rax,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     126f <portals_arg_to_urls+0xff>
mov    %rbp,0x18(%rsp)
mov    %rbx,0x8(%rsp)
lea    0x20(%rsp),%rbp
lea    0x120(%rsp),%r12
xor    %ebx,%ebx
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
mov    %rbp,%rsi
lea    0x14(%rsp),%rdx
call   1090 <tokenize_host_port@plt>
test   %rax,%rax
jne    1273 <portals_arg_to_urls+0x103>
mov    0x14(%rsp),%r8d
mov    %r12,%rdi
lea    0xe08(%rip),%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   1070 <sprintf@plt>
mov    %r12,%rdi
call   1080 <strdup@plt>
mov    %rax,0x520(%rsp,%r14,8)
lea    0x1(%r14),%r15
xor    %edi,%edi
lea    0x6(%rsp),%rsi
call   1060 <strtok@plt>
add    $0x8,%rbx
test   %rax,%rax
je     1238 <portals_arg_to_urls+0xc8>
cmp    $0x3ff,%r14
mov    %r15,%r14
jb     11d0 <portals_arg_to_urls+0x60>
test   %ebx,%ebx
je     127c <portals_arg_to_urls+0x10c>
lea    0x1(%r15),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     127c <portals_arg_to_urls+0x10c>
mov    %rax,%rbp
lea    0x520(%rsp),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    0x18(%rsp),%rax
mov    %r15d,(%rax)
jmp    127e <portals_arg_to_urls+0x10e>
xor    %ebp,%ebp
jmp    1283 <portals_arg_to_urls+0x113>
mov    0x8(%rsp),%rbx
xor    %ebp,%ebp
jmp    1283 <portals_arg_to_urls+0x113>
xor    %ebp,%ebp
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x2528,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.portals_arg_to_urls.delim = [2 x i8] c";\00"
@.str = [11 x i8] c"%s://%s:%d\00"
define i8** @portals_arg_to_urls(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [2 x i8]
%9 = alloca i8*
%10 = alloca [256 x i8]
%11 = alloca [1024 x i8]
%12 = alloca [1024 x i8*]
%13 = alloca i8**
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%17 = bitcast [2 x i8]* %8 to i8*
store i8** null, i8*** %13
store i32 0, i32* %15
%18 = load i8*, i8** %5
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%22 = call i8* @strtok(i8* %20, i8* %21)
store i8* %22, i8** %7
br label %23
23: ; preds = %37, %3
%24 = load i8*, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %15
%28 = icmp slt i32 %27, 1024
br label %29
29: ; preds = %26, %23
%30 = phi i1 [ false, %23 ], [ %28, %26 ]
br i1 %30, label %31, label %52
31: ; preds = %29
%32 = load i8*, i8** %7
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%34 = call i64 @tokenize_host_port(i8* %32, i8* %33, i32* %14)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
br label %84
37: ; preds = %31
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%39 = load i8*, i8** %4
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%41 = load i32, i32* %14
%42 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %39, i8* %40, i32 %41)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%44 = call i8* @strdup(i8* %43)
%45 = load i32, i32* %15
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %12, i64 0, i64 %46
store i8* %44, i8** %47
%48 = load i32, i32* %15
%49 = add nsw i32 %48, 1
store i32 %49, i32* %15
%50 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%51 = call i8* @strtok(i8* null, i8* %50)
store i8* %51, i8** %7
br label %23
52: ; preds = %29
%53 = load i32, i32* %15
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %83
55: ; preds = %52
%56 = load i32, i32* %15
%57 = add nsw i32 %56, 1
%58 = call i64 @calloc(i32 %57, i32 8)
%59 = inttoptr i64 %58 to i8**
store i8** %59, i8*** %13
%60 = load i8**, i8*** %13
%61 = icmp eq i8** %60, null
br i1 %61, label %62, label %63
62: ; preds = %55
br label %84
63: ; preds = %55
store i32 0, i32* %16
br label %64
64: ; preds = %77, %63
%65 = load i32, i32* %16
%66 = load i32, i32* %15
%67 = icmp slt i32 %65, %66
br i1 %67, label %68, label %80
68: ; preds = %64
%69 = load i32, i32* %16
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %12, i64 0, i64 %70
%72 = load i8*, i8** %71
%73 = load i8**, i8*** %13
%74 = load i32, i32* %16
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8*, i8** %73, i64 %75
store i8* %72, i8** %76
br label %77
77: ; preds = %68
%78 = load i32, i32* %16
%79 = add nsw i32 %78, 1
store i32 %79, i32* %16
br label %64
80: ; preds = %64
%81 = load i32, i32* %15
%82 = load i32*, i32** %6
store i32 %81, i32* %82
br label %83
83: ; preds = %80, %52
br label %84
84: ; preds = %83, %62, %36
%85 = load i8*, i8** %9
%86 = call i32 @free(i8* %85)
%87 = load i8**, i8*** %13
ret i8** %87
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s://%s:%d\00"
define i8** @portals_arg_to_urls(i8* %0, i8* nocapture %1, i32* nocapture writeonly %2) {
%4 = alloca i16
%5 = alloca [256 x i8]
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8*]
%8 = bitcast [1024 x i8*]* %7 to i8*
%9 = alloca i32
%10 = bitcast i16* %4 to i8*
store i16 59, i16* %4
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%13 = bitcast i32* %9 to i8*
%14 = tail call i8* @strdup(i8* %1)
%15 = call i8* @strtok(i8* %14, i8* %10)
%16 = icmp eq i8* %15, null
br i1 %16, label %44, label %17
17: ; preds = %3, %22
%18 = phi i64 [ %27, %22 ], [ 0, %3 ]
%19 = phi i8* [ %28, %22 ], [ %15, %3 ]
%20 = call i64 @tokenize_host_port(i8* %19, i8* %11, i32* %9)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %44
22: ; preds = %17
%23 = load i32, i32* %9
%24 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11, i32 %23)
%25 = call i8* @strdup(i8* %12)
%26 = getelementptr inbounds [1024 x i8*], [1024 x i8*]* %7, i64 0, i64 %18
store i8* %25, i8** %26
%27 = add nuw nsw i64 %18, 1
%28 = call i8* @strtok(i8* null, i8* %10)
%29 = icmp ne i8* %28, null
%30 = icmp ult i64 %18, 1023
%31 = select i1 %29, i1 %30, i1 false
br i1 %31, label %17, label %32
32: ; preds = %22
%33 = trunc i64 %27 to i32
%34 = icmp eq i32 %33, 0
br i1 %34, label %44, label %35
35: ; preds = %32
%36 = add nuw nsw i32 %33, 1
%37 = call i64 @calloc(i32 %36, i32 8)
%38 = icmp eq i64 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = inttoptr i64 %37 to i8*
%41 = inttoptr i64 %37 to i8**
%42 = shl i64 %27, 3
%43 = and i64 %42, 34359738360
store i32 %33, i32* %2
br label %44
44: ; preds = %17, %3, %32, %39, %35
%45 = phi i8** [ null, %35 ], [ %41, %39 ], [ null, %32 ], [ null, %3 ], [ null, %17 ]
%46 = call i32 @free(i8* %14)
ret i8** %45
}