#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 llist_head {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ llist_append(struct llist_head **, char *);
int llist_get_value_at_index(struct llist_head *, char **, unsigned int);
int llist_length(struct llist_head *);
int /*<<< orphan*/ llist_remove_index(struct llist_head **, int);
int /*<<< orphan*/ printf(char *, unsigned int, ...);
int main() {
  char *head = "Foo";
  char *one = "Bar";
  char *two = "FooBar";
  char *three = "Baz";
  char *data;
  int err;
  unsigned int len;
  struct llist_head *list = NULL;
  llist_append(&list, head);
  llist_append(&list, one);
  llist_append(&list, two);
  printf("List length: %u\n", llist_length(list));
  llist_remove_index(&list, 0);
  llist_append(&list, three);
  llist_remove_index(&list, 2);
  llist_remove_index(&list, 1);
  llist_remove_index(&list, 0);
  len = llist_length(list);
  printf("List length: %u\n", len);
  while (len > 0) {
    len--;
    err = llist_get_value_at_index(list, &data, len);
    printf("Got element %u from list, err=%d\n", len, err);
    if (!err)
      printf("Element %u is: '%s'\n", len, data);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe96(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe88(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe84(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdi
call   1040 <llist_append@plt>
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdi
call   1040 <llist_append@plt>
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdi
call   1040 <llist_append@plt>
mov    -0x40(%rbp),%rdi
call   1050 <llist_length@plt>
mov    %eax,%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
call   1070 <llist_remove_index@plt>
mov    -0x28(%rbp),%rsi
lea    -0x40(%rbp),%rdi
call   1040 <llist_append@plt>
lea    -0x40(%rbp),%rdi
mov    $0x2,%esi
call   1070 <llist_remove_index@plt>
lea    -0x40(%rbp),%rdi
mov    $0x1,%esi
call   1070 <llist_remove_index@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
call   1070 <llist_remove_index@plt>
mov    -0x40(%rbp),%rdi
call   1050 <llist_length@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x38(%rbp)
jbe    128d <main+0x13d>
mov    -0x38(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    -0x30(%rbp),%rsi
call   1060 <llist_get_value_at_index@plt>
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
jne    1288 <main+0x138>
mov    -0x38(%rbp),%esi
mov    -0x30(%rbp),%rdx
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122f <main+0xdf>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
lea    0xe95(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <llist_append@plt>
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdi
call   1040 <llist_append@plt>
lea    0xe78(%rip),%rsi
mov    %rbx,%rdi
call   1040 <llist_append@plt>
mov    (%rsp),%rdi
call   1050 <llist_length@plt>
lea    0xe6b(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <llist_remove_index@plt>
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdi
call   1040 <llist_append@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1070 <llist_remove_index@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1070 <llist_remove_index@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <llist_remove_index@plt>
mov    (%rsp),%rdi
call   1050 <llist_length@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebx,%ebx
je     125a <main+0x10a>
add    $0xffffffff,%ebx
lea    0x8(%rsp),%r14
lea    0xe09(%rip),%r15
lea    0xe24(%rip),%r12
jmp    1225 <main+0xd5>
add    $0xffffffff,%ebx
jae    125a <main+0x10a>
mov    (%rsp),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <llist_get_value_at_index@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jne    1220 <main+0xd0>
mov    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1220 <main+0xd0>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.llist_head = type { i32 }
@.str = [4 x i8] c"Foo\00"
@.str.1 = [4 x i8] c"Bar\00"
@.str.2 = [7 x i8] c"FooBar\00"
@.str.3 = [4 x i8] c"Baz\00"
@.str.4 = [17 x i8] c"List length: %u\0A\00"
@.str.5 = [34 x i8] c"Got element %u from list, err=%d\0A\00"
@.str.6 = [21 x i8] c"Element %u is: '%s'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.llist_head*
store i32 0, i32* %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %5
store %struct.llist_head* null, %struct.llist_head** %9
%10 = load i8*, i8** %2
%11 = call i32 @llist_append(%struct.llist_head** %9, i8* %10)
%12 = load i8*, i8** %3
%13 = call i32 @llist_append(%struct.llist_head** %9, i8* %12)
%14 = load i8*, i8** %4
%15 = call i32 @llist_append(%struct.llist_head** %9, i8* %14)
%16 = load %struct.llist_head*, %struct.llist_head** %9
%17 = call i32 @llist_length(%struct.llist_head* %16)
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = call i32 @llist_remove_index(%struct.llist_head** %9, i32 0)
%20 = load i8*, i8** %5
%21 = call i32 @llist_append(%struct.llist_head** %9, i8* %20)
%22 = call i32 @llist_remove_index(%struct.llist_head** %9, i32 2)
%23 = call i32 @llist_remove_index(%struct.llist_head** %9, i32 1)
%24 = call i32 @llist_remove_index(%struct.llist_head** %9, i32 0)
%25 = load %struct.llist_head*, %struct.llist_head** %9
%26 = call i32 @llist_length(%struct.llist_head* %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %47, %0
%30 = load i32, i32* %8
%31 = icmp ugt i32 %30, 0
br i1 %31, label %32, label %48
32: ; preds = %29
%33 = load i32, i32* %8
%34 = add i32 %33, -1
store i32 %34, i32* %8
%35 = load %struct.llist_head*, %struct.llist_head** %9
%36 = load i32, i32* %8
%37 = call i32 @llist_get_value_at_index(%struct.llist_head* %35, i8** %6, i32 %36)
store i32 %37, i32* %7
%38 = load i32, i32* %8
%39 = load i32, i32* %7
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 %39)
%41 = load i32, i32* %7
%42 = icmp ne i32 %41, 0
br i1 %42, label %47, label %43
43: ; preds = %32
%44 = load i32, i32* %8
%45 = load i8*, i8** %6
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %44, i8* %45)
br label %47
47: ; preds = %43, %32
br label %29
48: ; preds = %29
ret i32 0
}

LLVM IR (-O2):
%struct.llist_head = type { i32 }
@.str = [4 x i8] c"Foo\00"
@.str.1 = [4 x i8] c"Bar\00"
@.str.2 = [7 x i8] c"FooBar\00"
@.str.3 = [4 x i8] c"Baz\00"
@.str.4 = [17 x i8] c"List length: %u\0A\00"
@.str.5 = [34 x i8] c"Got element %u from list, err=%d\0A\00"
@.str.6 = [21 x i8] c"Element %u is: '%s'\0A\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca %struct.llist_head*
%3 = bitcast i8** %1 to i8*
%4 = bitcast %struct.llist_head** %2 to i8*
store %struct.llist_head* null, %struct.llist_head** %2
%5 = call i32 @llist_append(%struct.llist_head** %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @llist_append(%struct.llist_head** %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @llist_append(%struct.llist_head** %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%8 = load %struct.llist_head*, %struct.llist_head** %2
%9 = call i32 @llist_length(%struct.llist_head* %8)
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %9)
%11 = call i32 @llist_remove_index(%struct.llist_head** %2, i32 0)
%12 = call i32 @llist_append(%struct.llist_head** %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%13 = call i32 @llist_remove_index(%struct.llist_head** %2, i32 2)
%14 = call i32 @llist_remove_index(%struct.llist_head** %2, i32 1)
%15 = call i32 @llist_remove_index(%struct.llist_head** %2, i32 0)
%16 = load %struct.llist_head*, %struct.llist_head** %2
%17 = call i32 @llist_length(%struct.llist_head* %16)
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %17, 0
br i1 %19, label %32, label %20
20: ; preds = %0, %30
%21 = phi i32 [ %22, %30 ], [ %17, %0 ]
%22 = add i32 %21, -1
%23 = load %struct.llist_head*, %struct.llist_head** %2
%24 = call i32 @llist_get_value_at_index(%struct.llist_head* %23, i8** %1, i32 %22)
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %22, i32 %24)
%26 = icmp eq i32 %24, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i8*, i8** %1
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %22, i8* %28)
br label %30
30: ; preds = %27, %20
%31 = icmp eq i32 %22, 0
br i1 %31, label %32, label %20
32: ; preds = %30, %0
ret i32 0
}