#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*/ free(char *);
scalar_t__ malloc(int);
char *req_swoa_chars(char *, char const *, char const *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, short);
char *strtok(char *, char *);
int switches_list(const char *hostname, const char *session_id, char *ains[],
                  size_t size) {
  char *res = req_swoa_chars("getswitchlist", hostname, session_id);
  if (0 == strlen(res)) {
    free(res);
    return 0;
  }
  short i = 0;
  short len = 0;
  char *delimiter = ",";
  char *ptr = strtok(res, delimiter);
  while (ptr != NULL && i < size) {
    len = strlen(ptr) + 1;
    ains[i] = (char *)malloc(sizeof(char) * len);
    strncpy(ains[i], ptr, len);
    ptr = strtok(NULL, delimiter);
    if (ptr != NULL) {
      i++;
    }
  }
  free(res);
  return ++i;
}


x86 assembly (clang, -O0):
<switches_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe79(%rip),%rdi
call   1080 <req_swoa_chars@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    11ba <switches_list+0x5a>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a1 <switches_list+0x141>
movw   $0x0,-0x32(%rbp)
movw   $0x0,-0x34(%rbp)
lea    0xe41(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x48(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x48(%rbp)
mov    %al,-0x49(%rbp)
je     1201 <switches_list+0xa1>
movswq -0x32(%rbp),%rax
cmp    -0x28(%rbp),%rax
setb   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1211 <switches_list+0xb1>
jmp    1288 <switches_list+0x128>
mov    -0x48(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %ax,-0x34(%rbp)
movswq -0x34(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movswq -0x32(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movswq -0x32(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x48(%rbp),%rsi
movswl -0x34(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <strtok@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1283 <switches_list+0x123>
mov    -0x32(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x32(%rbp)
jmp    11e2 <switches_list+0x82>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x32(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x32(%rbp)
cwtl
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switches_list>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%rdx
mov    %rdi,%rsi
lea    0xe82(%rip),%rdi
call   1080 <req_swoa_chars@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     122e <switches_list+0xce>
lea    0xe71(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strtok@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     1231 <switches_list+0xd1>
test   %r12,%r12
je     1231 <switches_list+0xd1>
mov    %rax,%rbx
mov    %rbp,(%rsp)
xor    %r13d,%r13d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movswl %ax,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,(%r15,%r13,8)
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
xor    %edi,%edi
lea    0xe0f(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,%rbx
xor    %eax,%eax
test   %rbx,%rbx
setne  %al
add    %eax,%r14d
test   %rbx,%rbx
je     1220 <switches_list+0xc0>
movswq %r14w,%r13
cmp    %r12,%r13
jb     11d0 <switches_list+0x70>
lea    0x1(%r14),%eax
movswl %ax,%r14d
mov    (%rsp),%rbp
jmp    1231 <switches_list+0xd1>
xor    %r14d,%r14d
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"getswitchlist\00"
@.str.1 = [2 x i8] c",\00"
define i32 @switches_list(i8* %0, i8* %1, i8** %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i16
%12 = alloca i16
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i64 %3, i64* %9
%15 = load i8*, i8** %6
%16 = load i8*, i8** %7
%17 = call i8* @req_swoa_chars(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = call i32 @strlen(i8* %18)
%20 = icmp eq i32 0, %19
br i1 %20, label %21, label %24
21: ; preds = %4
%22 = load i8*, i8** %10
%23 = call i32 @free(i8* %22)
store i32 0, i32* %5
br label %75
24: ; preds = %4
store i16 0, i16* %11
store i16 0, i16* %12
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %13
%25 = load i8*, i8** %10
%26 = load i8*, i8** %13
%27 = call i8* @strtok(i8* %25, i8* %26)
store i8* %27, i8** %14
br label %28
28: ; preds = %68, %24
%29 = load i8*, i8** %14
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i16, i16* %11
%33 = sext i16 %32 to i64
%34 = load i64, i64* %9
%35 = icmp ult i64 %33, %34
br label %36
36: ; preds = %31, %28
%37 = phi i1 [ false, %28 ], [ %35, %31 ]
br i1 %37, label %38, label %69
38: ; preds = %36
%39 = load i8*, i8** %14
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %40, 1
%42 = trunc i32 %41 to i16
store i16 %42, i16* %12
%43 = load i16, i16* %12
%44 = sext i16 %43 to i64
%45 = mul i64 1, %44
%46 = trunc i64 %45 to i32
%47 = call i64 @malloc(i32 %46)
%48 = inttoptr i64 %47 to i8*
%49 = load i8**, i8*** %8
%50 = load i16, i16* %11
%51 = sext i16 %50 to i64
%52 = getelementptr inbounds i8*, i8** %49, i64 %51
store i8* %48, i8** %52
%53 = load i8**, i8*** %8
%54 = load i16, i16* %11
%55 = sext i16 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
%58 = load i8*, i8** %14
%59 = load i16, i16* %12
%60 = call i32 @strncpy(i8* %57, i8* %58, i16 signext %59)
%61 = load i8*, i8** %13
%62 = call i8* @strtok(i8* null, i8* %61)
store i8* %62, i8** %14
%63 = load i8*, i8** %14
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %68
65: ; preds = %38
%66 = load i16, i16* %11
%67 = add i16 %66, 1
store i16 %67, i16* %11
br label %68
68: ; preds = %65, %38
br label %28
69: ; preds = %36
%70 = load i8*, i8** %10
%71 = call i32 @free(i8* %70)
%72 = load i16, i16* %11
%73 = add i16 %72, 1
store i16 %73, i16* %11
%74 = sext i16 %73 to i32
store i32 %74, i32* %5
br label %75
75: ; preds = %69, %21
%76 = load i32, i32* %5
ret i32 %76
}

LLVM IR (-O2):
@.str = [14 x i8] c"getswitchlist\00"
@.str.1 = [2 x i8] c",\00"
define i32 @switches_list(i8* %0, i8* %1, i8** nocapture writeonly %2, i64 %3) {
%5 = tail call i8* @req_swoa_chars(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%6 = tail call i32 @strlen(i8* %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %35, label %8
8: ; preds = %4
%9 = tail call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp ne i8* %9, null
%11 = icmp ne i64 %3, 0
%12 = and i1 %10, %11
br i1 %12, label %13, label %35
13: ; preds = %8, %13
%14 = phi i64 [ %29, %13 ], [ 0, %8 ]
%15 = phi i8* [ %25, %13 ], [ %9, %8 ]
%16 = phi i16 [ %28, %13 ], [ 0, %8 ]
%17 = tail call i32 @strlen(i8* %15)
%18 = trunc i32 %17 to i16
%19 = add i16 %18, 1
%20 = sext i16 %19 to i32
%21 = tail call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
%23 = getelementptr inbounds i8*, i8** %2, i64 %14
store i8* %22, i8** %23
%24 = tail call i32 @strncpy(i8* %22, i8* %15, i16 signext %19)
%25 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp ne i8* %25, null
%27 = zext i1 %26 to i16
%28 = add i16 %16, %27
%29 = sext i16 %28 to i64
%30 = icmp ult i64 %29, %3
%31 = select i1 %26, i1 %30, i1 false
br i1 %31, label %13, label %32
32: ; preds = %13
%33 = add i16 %28, 1
%34 = sext i16 %33 to i32
br label %35
35: ; preds = %8, %32, %4
%36 = phi i32 [ 0, %4 ], [ 1, %8 ], [ %34, %32 ]
%37 = tail call i32 @free(i8* %5)
ret i32 %36
}