#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 */
struct t_weechat_plugin {
  int dummy;
};
struct t_gui_completion {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ gui_completion_custom(struct t_gui_completion *, char *,
                                         struct t_weechat_plugin *);
int /*<<< orphan*/ gui_completion_list_add(struct t_gui_completion *, char *,
                                           int /*<<< orphan*/, char *);
int /*<<< orphan*/ gui_completion_stop(struct t_gui_completion *, int);
char *strchr(char const *, char);
char *strdup(char const *);
char *string_strndup(char const *, int);
void gui_completion_build_list_template(struct t_gui_completion *completion,
                                        const char *template,
                                        struct t_weechat_plugin *plugin) {
  char *word, *custom_completion;
  const char *pos, *pos_end;
  int word_offset;
  word = strdup(template);
  word_offset = 0;
  pos = template;
  while (pos) {
    switch (pos[0]) {
    case '\0':
    case ' ':
    case '|':
      if (word_offset > 0) {
        word[word_offset] = '\0';
        gui_completion_list_add(completion, word, 0, "sort");
      }
      word_offset = 0;
      break;
    case '%':
      pos++;
      if (pos && pos[0]) {
        switch (pos[0]) {
        case '-':
          gui_completion_stop(completion, 1);
          free(word);
          return;
          break;
        case '*':
          break;
        case '(':
          pos++;
          pos_end = strchr(pos, ')');
          if (pos_end) {
            if (pos_end > pos) {
              custom_completion = string_strndup(pos, pos_end - pos);
              if (custom_completion) {
                gui_completion_custom(completion, custom_completion, plugin);
                free(custom_completion);
              }
            }
            pos = pos_end + 1;
          }
        }
      }
      break;
    default:
      word[word_offset++] = pos[0];
    }
    if (!pos[0] || (pos[0] == ' '))
      pos = NULL;
    else
      pos++;
  }
  free(word);
}


x86 assembly (clang, -O0):
<gui_completion_build_list_template>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     139b <gui_completion_build_list_template+0x22b>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x40(%rbp)
test   %eax,%eax
je     11f5 <gui_completion_build_list_template+0x85>
jmp    11c2 <gui_completion_build_list_template+0x52>
mov    -0x40(%rbp),%eax
sub    $0x20,%eax
je     11f5 <gui_completion_build_list_template+0x85>
jmp    11d3 <gui_completion_build_list_template+0x63>
mov    -0x40(%rbp),%eax
sub    $0x25,%eax
je     122d <gui_completion_build_list_template+0xbd>
jmp    11e4 <gui_completion_build_list_template+0x74>
mov    -0x40(%rbp),%eax
sub    $0x7c,%eax
jne    1345 <gui_completion_build_list_template+0x1d5>
jmp    11f5 <gui_completion_build_list_template+0x85>
cmpl   $0x0,-0x3c(%rbp)
jle    1221 <gui_completion_build_list_template+0xb1>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
lea    0xde4(%rip),%rcx
call   1060 <gui_completion_list_add@plt>
movl   $0x0,-0x3c(%rbp)
jmp    1360 <gui_completion_build_list_template+0x1f0>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1340 <gui_completion_build_list_template+0x1d0>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1340 <gui_completion_build_list_template+0x1d0>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x44(%rbp)
sub    $0x28,%eax
je     12af <gui_completion_build_list_template+0x13f>
jmp    126c <gui_completion_build_list_template+0xfc>
mov    -0x44(%rbp),%eax
sub    $0x2a,%eax
je     12aa <gui_completion_build_list_template+0x13a>
jmp    127d <gui_completion_build_list_template+0x10d>
mov    -0x44(%rbp),%eax
sub    $0x2d,%eax
jne    133b <gui_completion_build_list_template+0x1cb>
jmp    128e <gui_completion_build_list_template+0x11e>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1070 <gui_completion_stop@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    13a4 <gui_completion_build_list_template+0x234>
jmp    133b <gui_completion_build_list_template+0x1cb>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x29,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1336 <gui_completion_build_list_template+0x1c6>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    132a <gui_completion_build_list_template+0x1ba>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1080 <string_strndup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1325 <gui_completion_build_list_template+0x1b5>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <gui_completion_custom@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    132a <gui_completion_build_list_template+0x1ba>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    133b <gui_completion_build_list_template+0x1cb>
jmp    1340 <gui_completion_build_list_template+0x1d0>
jmp    1360 <gui_completion_build_list_template+0x1f0>
mov    -0x30(%rbp),%rax
mov    (%rax),%dl
mov    -0x20(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x3c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     137d <gui_completion_build_list_template+0x20d>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    138a <gui_completion_build_list_template+0x21a>
movq   $0x0,-0x30(%rbp)
jmp    1396 <gui_completion_build_list_template+0x226>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11a0 <gui_completion_build_list_template+0x30>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gui_completion_build_list_template>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1090 <strdup@plt>
mov    %rax,%r13
test   %rbx,%rbx
je     12bd <gui_completion_build_list_template+0x14d>
xor    %r15d,%r15d
lea    0xe5d(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rbx),%eax
movsbl %al,%ecx
cmp    $0x24,%ecx
jg     11e0 <gui_completion_build_list_template+0x70>
test   %ecx,%ecx
je     11ea <gui_completion_build_list_template+0x7a>
cmp    $0x20,%ecx
je     11ea <gui_completion_build_list_template+0x7a>
movslq %r15d,%rcx
add    $0x1,%r15d
mov    %al,0x0(%r13,%rcx,1)
jmp    12a0 <gui_completion_build_list_template+0x130>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x25,%ecx
je     1200 <gui_completion_build_list_template+0x90>
cmp    $0x7c,%ecx
jne    11c4 <gui_completion_build_list_template+0x54>
test   %r15d,%r15d
jg     1230 <gui_completion_build_list_template+0xc0>
xor    %r15d,%r15d
jmp    12a0 <gui_completion_build_list_template+0x130>
nopw   0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%eax
test   %eax,%eax
je     12bd <gui_completion_build_list_template+0x14d>
cmp    $0x28,%eax
je     124e <gui_completion_build_list_template+0xde>
cmp    $0x2d,%eax
je     12b0 <gui_completion_build_list_template+0x140>
add    $0x1,%rbx
test   $0xdf,%al
jne    12a7 <gui_completion_build_list_template+0x137>
jmp    12bd <gui_completion_build_list_template+0x14d>
nopl   0x0(%rax,%rax,1)
mov    %r15d,%eax
movb   $0x0,0x0(%r13,%rax,1)
xor    %r15d,%r15d
mov    %r14,%rdi
mov    %r13,%rsi
xor    %edx,%edx
mov    %r12,%rcx
call   1060 <gui_completion_list_add@plt>
jmp    12a0 <gui_completion_build_list_template+0x130>
add    $0x2,%rbx
mov    %rbx,%rdi
mov    $0x29,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12a0 <gui_completion_build_list_template+0x130>
mov    %rax,%rbp
cmp    %rbx,%rax
jbe    1297 <gui_completion_build_list_template+0x127>
mov    %ebp,%esi
sub    %ebx,%esi
mov    %rbx,%rdi
call   1080 <string_strndup@plt>
test   %rax,%rax
je     1297 <gui_completion_build_list_template+0x127>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
mov    (%rsp),%rdx
call   1040 <gui_completion_custom@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x1,%rbp
mov    %rbp,%rbx
xchg   %ax,%ax
movzbl (%rbx),%eax
test   $0xdf,%al
je     12bd <gui_completion_build_list_template+0x14d>
add    $0x1,%rbx
jmp    11b0 <gui_completion_build_list_template+0x40>
mov    %r14,%rdi
mov    $0x1,%esi
call   1070 <gui_completion_stop@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.t_gui_completion = type { i32 }
%struct.t_weechat_plugin = type { i32 }
@.str = [5 x i8] c"sort\00"
define void @gui_completion_build_list_template(%struct.t_gui_completion* %0, i8* %1, %struct.t_weechat_plugin* %2) {
%4 = alloca %struct.t_gui_completion*
%5 = alloca i8*
%6 = alloca %struct.t_weechat_plugin*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store %struct.t_gui_completion* %0, %struct.t_gui_completion** %4
store i8* %1, i8** %5
store %struct.t_weechat_plugin* %2, %struct.t_weechat_plugin** %6
%12 = load i8*, i8** %5
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %7
store i32 0, i32* %11
%14 = load i8*, i8** %5
store i8* %14, i8** %9
br label %15
15: ; preds = %117, %3
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %118
18: ; preds = %15
%19 = load i8*, i8** %9
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
switch i32 %22, label %93 [
i32 0, label %23
i32 32, label %23
i32 124, label %23
i32 37, label %35
]
23: ; preds = %18, %18, %18
%24 = load i32, i32* %11
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i8*, i8** %7
%28 = load i32, i32* %11
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
%31 = load %struct.t_gui_completion*, %struct.t_gui_completion** %4
%32 = load i8*, i8** %7
%33 = call i32 @gui_completion_list_add(%struct.t_gui_completion* %31, i8* %32, i32 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %26, %23
store i32 0, i32* %11
br label %102
35: ; preds = %18
%36 = load i8*, i8** %9
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %9
%38 = load i8*, i8** %9
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %92
40: ; preds = %35
%41 = load i8*, i8** %9
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %92
46: ; preds = %40
%47 = load i8*, i8** %9
%48 = getelementptr inbounds i8, i8* %47, i64 0
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
switch i32 %50, label %91 [
i32 45, label %51
i32 42, label %56
i32 40, label %57
]
51: ; preds = %46
%52 = load %struct.t_gui_completion*, %struct.t_gui_completion** %4
%53 = call i32 @gui_completion_stop(%struct.t_gui_completion* %52, i32 1)
%54 = load i8*, i8** %7
%55 = call i32 @free(i8* %54)
br label %121
56: ; preds = %46
br label %91
57: ; preds = %46
%58 = load i8*, i8** %9
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %9
%60 = load i8*, i8** %9
%61 = call i8* @strchr(i8* %60, i8 signext 41)
store i8* %61, i8** %10
%62 = load i8*, i8** %10
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %90
64: ; preds = %57
%65 = load i8*, i8** %10
%66 = load i8*, i8** %9
%67 = icmp ugt i8* %65, %66
br i1 %67, label %68, label %87
68: ; preds = %64
%69 = load i8*, i8** %9
%70 = load i8*, i8** %10
%71 = load i8*, i8** %9
%72 = ptrtoint i8* %70 to i64
%73 = ptrtoint i8* %71 to i64
%74 = sub i64 %72, %73
%75 = trunc i64 %74 to i32
%76 = call i8* @string_strndup(i8* %69, i32 %75)
store i8* %76, i8** %8
%77 = load i8*, i8** %8
%78 = icmp ne i8* %77, null
br i1 %78, label %79, label %86
79: ; preds = %68
%80 = load %struct.t_gui_completion*, %struct.t_gui_completion** %4
%81 = load i8*, i8** %8
%82 = load %struct.t_weechat_plugin*, %struct.t_weechat_plugin** %6
%83 = call i32 @gui_completion_custom(%struct.t_gui_completion* %80, i8* %81, %struct.t_weechat_plugin* %82)
%84 = load i8*, i8** %8
%85 = call i32 @free(i8* %84)
br label %86
86: ; preds = %79, %68
br label %87
87: ; preds = %86, %64
%88 = load i8*, i8** %10
%89 = getelementptr inbounds i8, i8* %88, i64 1
store i8* %89, i8** %9
br label %90
90: ; preds = %87, %57
br label %91
91: ; preds = %90, %46, %56
br label %92
92: ; preds = %91, %40, %35
br label %102
93: ; preds = %18
%94 = load i8*, i8** %9
%95 = getelementptr inbounds i8, i8* %94, i64 0
%96 = load i8, i8* %95
%97 = load i8*, i8** %7
%98 = load i32, i32* %11
%99 = add nsw i32 %98, 1
store i32 %99, i32* %11
%100 = sext i32 %98 to i64
%101 = getelementptr inbounds i8, i8* %97, i64 %100
store i8 %96, i8* %101
br label %102
102: ; preds = %93, %92, %34
%103 = load i8*, i8** %9
%104 = getelementptr inbounds i8, i8* %103, i64 0
%105 = load i8, i8* %104
%106 = icmp ne i8 %105, 0
br i1 %106, label %107, label %113
107: ; preds = %102
%108 = load i8*, i8** %9
%109 = getelementptr inbounds i8, i8* %108, i64 0
%110 = load i8, i8* %109
%111 = sext i8 %110 to i32
%112 = icmp eq i32 %111, 32
br i1 %112, label %113, label %114
113: ; preds = %107, %102
store i8* null, i8** %9
br label %117
114: ; preds = %107
%115 = load i8*, i8** %9
%116 = getelementptr inbounds i8, i8* %115, i32 1
store i8* %116, i8** %9
br label %117
117: ; preds = %114, %113
br label %15
118: ; preds = %15
%119 = load i8*, i8** %7
%120 = call i32 @free(i8* %119)
br label %121
121: ; preds = %118, %51
ret void
}

LLVM IR (-O2):
%struct.t_gui_completion = type { i32 }
%struct.t_weechat_plugin = type { i32 }
@.str = [5 x i8] c"sort\00"
define void @gui_completion_build_list_template(%struct.t_gui_completion* %0, i8* %1, %struct.t_weechat_plugin* %2) {
%4 = tail call i8* @strdup(i8* %1)
%5 = icmp eq i8* %1, null
br i1 %5, label %57, label %6
6: ; preds = %3, %55
%7 = phi i32 [ %54, %55 ], [ 0, %3 ]
%8 = phi i8* [ %56, %55 ], [ %1, %3 ]
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
switch i32 %10, label %43 [
i32 0, label %11
i32 32, label %11
i32 124, label %11
i32 37, label %17
]
11: ; preds = %6, %6, %6
%12 = icmp sgt i32 %7, 0
br i1 %12, label %13, label %47
13: ; preds = %11
%14 = zext i32 %7 to i64
%15 = getelementptr inbounds i8, i8* %4, i64 %14
store i8 0, i8* %15
%16 = tail call i32 @gui_completion_list_add(%struct.t_gui_completion* %0, i8* %4, i32 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %47
17: ; preds = %6
%18 = getelementptr inbounds i8, i8* %8, i64 1
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %57, label %21
21: ; preds = %17
%22 = sext i8 %19 to i32
switch i32 %22, label %51 [
i32 45, label %23
i32 40, label %25
]
23: ; preds = %21
%24 = tail call i32 @gui_completion_stop(%struct.t_gui_completion* %0, i32 1)
br label %57
25: ; preds = %21
%26 = getelementptr inbounds i8, i8* %8, i64 2
%27 = tail call i8* @strchr(i8* %26, i8 signext 41)
%28 = icmp eq i8* %27, null
br i1 %28, label %47, label %29
29: ; preds = %25
%30 = icmp ugt i8* %27, %26
br i1 %30, label %31, label %41
31: ; preds = %29
%32 = ptrtoint i8* %27 to i64
%33 = ptrtoint i8* %26 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = tail call i8* @string_strndup(i8* %26, i32 %35)
%37 = icmp eq i8* %36, null
br i1 %37, label %41, label %38
38: ; preds = %31
%39 = tail call i32 @gui_completion_custom(%struct.t_gui_completion* %0, i8* %36, %struct.t_weechat_plugin* %2)
%40 = tail call i32 @free(i8* %36)
br label %41
41: ; preds = %31, %38, %29
%42 = getelementptr inbounds i8, i8* %27, i64 1
br label %47
43: ; preds = %6
%44 = add nsw i32 %7, 1
%45 = sext i32 %7 to i64
%46 = getelementptr inbounds i8, i8* %4, i64 %45
store i8 %9, i8* %46
br label %47
47: ; preds = %43, %41, %25, %13, %11
%48 = phi i8* [ %8, %11 ], [ %8, %13 ], [ %26, %25 ], [ %42, %41 ], [ %8, %43 ]
%49 = phi i32 [ 0, %11 ], [ 0, %13 ], [ %7, %25 ], [ %7, %41 ], [ %44, %43 ]
%50 = load i8, i8* %48
br label %51
51: ; preds = %47, %21
%52 = phi i8 [ %50, %47 ], [ %19, %21 ]
%53 = phi i8* [ %48, %47 ], [ %18, %21 ]
%54 = phi i32 [ %49, %47 ], [ %7, %21 ]
switch i8 %52, label %55 [
i8 0, label %57
i8 32, label %57
]
55: ; preds = %51
%56 = getelementptr inbounds i8, i8* %53, i64 1
br label %6
57: ; preds = %51, %51, %17, %3, %23
%58 = tail call i32 @free(i8* %4)
ret void
}