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

/* Variables and functions */
int /*<<< orphan*/ destroy_seq(int **, int);
int **new_sequence(int /*<<< orphan*/, int);
int /*<<< orphan*/ print_list(struct slist_node *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ slist_destroy(struct slist_node **);
int slist_insert(struct slist_node **, int *, int);
size_t slist_length(struct slist_node *);
void test_insert(void) {
  size_t length = 0;
  int **seq = new_sequence(0, 100);
  int i;
  struct slist_node *head = NULL;
  for (i = 0; i < 100; i++) {
    if (-1 == slist_insert(&head, seq[i], sizeof(int))) {
      printf("[!] failed to insert %d\n", *seq[i]);
    }
  }
  length = slist_length(head);
  if (length != 100) {
    printf("[!] expected a length of 100, actual is %lu\n",
           (long unsigned)length);
  }
  printf("[+] list length matches expected value\n");
  print_list(head);
  slist_destroy(&head);
  destroy_seq(seq, 100);
  printf("[+] insert complete\n");
}


x86 assembly (clang, -O0):
<test_insert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x8(%rbp)
xor    %edi,%edi
mov    $0x64,%esi
call   1090 <new_sequence@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x64,-0x14(%rbp)
jge    1201 <test_insert+0x91>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x4,%edx
call   1080 <slist_insert@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11ee <test_insert+0x7e>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11f3 <test_insert+0x83>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119f <test_insert+0x2f>
mov    -0x20(%rbp),%rdi
call   1040 <slist_length@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x64,-0x8(%rbp)
je     122b <test_insert+0xbb>
mov    -0x8(%rbp),%rsi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <print_list@plt>
lea    -0x20(%rbp),%rdi
call   1060 <slist_destroy@plt>
mov    -0x10(%rbp),%rdi
mov    $0x64,%esi
call   1070 <destroy_seq@plt>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_insert>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
mov    $0x64,%esi
call   10a0 <new_sequence@plt>
mov    %rax,%r12
movq   $0x0,(%rsp)
mov    %rsp,%r14
lea    0xe57(%rip),%r15
xor    %ebx,%ebx
jmp    11ba <test_insert+0x3a>
nopl   (%rax)
add    $0x1,%rbx
cmp    $0x64,%rbx
je     11e2 <test_insert+0x62>
mov    (%r12,%rbx,8),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1090 <slist_insert@plt>
cmp    $0xffffffff,%eax
jne    11b0 <test_insert+0x30>
mov    (%r12,%rbx,8),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11b0 <test_insert+0x30>
mov    (%rsp),%rdi
call   1050 <slist_length@plt>
cmp    $0x64,%rax
je     1202 <test_insert+0x82>
lea    0xe21(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    (%rsp),%rdi
call   1040 <print_list@plt>
mov    %rsp,%rdi
call   1070 <slist_destroy@plt>
mov    %r12,%rdi
mov    $0x64,%esi
call   1080 <destroy_seq@plt>
lea    0xe3a(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.slist_node = type { i32 }
@.str = [25 x i8] c"[!] failed to insert %d\0A\00"
@.str.1 = [45 x i8] c"[!] expected a length of 100, actual is %lu\0A\00"
@.str.2 = [40 x i8] c"[+] list length matches expected value\0A\00"
@.str.3 = [21 x i8] c"[+] insert complete\0A\00"
define void @test_insert() {
%1 = alloca i64
%2 = alloca i32**
%3 = alloca i32
%4 = alloca %struct.slist_node*
store i64 0, i64* %1
%5 = call i32** @new_sequence(i32 0, i32 100)
store i32** %5, i32*** %2
store %struct.slist_node* null, %struct.slist_node** %4
store i32 0, i32* %3
br label %6
6: ; preds = %26, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 100
br i1 %8, label %9, label %29
9: ; preds = %6
%10 = load i32**, i32*** %2
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i32*, i32** %10, i64 %12
%14 = load i32*, i32** %13
%15 = call i32 @slist_insert(%struct.slist_node** %4, i32* %14, i32 4)
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %25
17: ; preds = %9
%18 = load i32**, i32*** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32*, i32** %18, i64 %20
%22 = load i32*, i32** %21
%23 = load i32, i32* %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %17, %9
br label %26
26: ; preds = %25
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %6
29: ; preds = %6
%30 = load %struct.slist_node*, %struct.slist_node** %4
%31 = call i64 @slist_length(%struct.slist_node* %30)
store i64 %31, i64* %1
%32 = load i64, i64* %1
%33 = icmp ne i64 %32, 100
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i64, i64* %1
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %35)
br label %37
37: ; preds = %34, %29
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%39 = load %struct.slist_node*, %struct.slist_node** %4
%40 = call i32 @print_list(%struct.slist_node* %39)
%41 = call i32 @slist_destroy(%struct.slist_node** %4)
%42 = load i32**, i32*** %2
%43 = call i32 @destroy_seq(i32** %42, i32 100)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.slist_node = type { i32 }
@.str = [25 x i8] c"[!] failed to insert %d\0A\00"
@.str.1 = [45 x i8] c"[!] expected a length of 100, actual is %lu\0A\00"
@str = [39 x i8] c"[+] list length matches expected value\00"
@str.4 = [20 x i8] c"[+] insert complete\00"
define void @test_insert() {
%1 = alloca %struct.slist_node*
%2 = tail call i32** @new_sequence(i32 0, i32 100)
%3 = bitcast %struct.slist_node** %1 to i8*
store %struct.slist_node* null, %struct.slist_node** %1
br label %4
4: ; preds = %0, %14
%5 = phi i64 [ 0, %0 ], [ %15, %14 ]
%6 = getelementptr inbounds i32*, i32** %2, i64 %5
%7 = load i32*, i32** %6
%8 = call i32 @slist_insert(%struct.slist_node** %1, i32* %7, i32 4)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = load i32*, i32** %6
%12 = load i32, i32* %11
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %4, %10
%15 = add nuw nsw i64 %5, 1
%16 = icmp eq i64 %15, 100
br i1 %16, label %17, label %4
17: ; preds = %14
%18 = load %struct.slist_node*, %struct.slist_node** %1
%19 = call i64 @slist_length(%struct.slist_node* %18)
%20 = icmp eq i64 %19, 100
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %19)
br label %23
23: ; preds = %21, %17
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%25 = load %struct.slist_node*, %struct.slist_node** %1
%26 = call i32 @print_list(%struct.slist_node* %25)
%27 = call i32 @slist_destroy(%struct.slist_node** %1)
%28 = call i32 @destroy_seq(i32** %2, i32 100)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
ret void
}