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

/* Variables and functions */
int /*<<< orphan*/ *addFront(struct List *, void *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ popFront(struct List *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ reverseList(struct List *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
void addToHistory(struct List *historyList, char *userInput, int numOfHistory) {
  char *command = (char *)malloc(strlen(userInput) + 1);
  strncpy(command, userInput, strlen(userInput) + 1);
  if (numOfHistory == 101) {
    reverseList(historyList);
    popFront(historyList);
    reverseList(historyList);
  }
  if (addFront(historyList, (void *)command) == NULL)
    printf("error: Unable to add history\n");
}


x86 assembly (clang, -O0):
<addToHistory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <strncpy@plt>
cmpl   $0x65,-0x14(%rbp)
jne    11ee <addToHistory+0x7e>
mov    -0x8(%rbp),%rdi
call   1070 <reverseList@plt>
mov    -0x8(%rbp),%rdi
call   1030 <popFront@plt>
mov    -0x8(%rbp),%rdi
call   1070 <reverseList@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <addFront@plt>
cmp    $0x0,%rax
jne    1211 <addToHistory+0xa1>
lea    0xdf4(%rip),%rdi
call   1060 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addToHistory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
mov    %rsi,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <strncpy@plt>
cmp    $0x65,%r15d
jne    11c9 <addToHistory+0x59>
mov    %r14,%rdi
call   1070 <reverseList@plt>
mov    %r14,%rdi
call   1030 <popFront@plt>
mov    %r14,%rdi
call   1070 <reverseList@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <addFront@plt>
test   %rax,%rax
je     11e4 <addToHistory+0x74>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe15(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <puts@plt>

LLVM IR (-O0):
%struct.List = type { i32 }
@.str = [30 x i8] c"error: Unable to add history\0A\00"
define void @addToHistory(%struct.List* %0, i8* %1, i32 %2) {
%4 = alloca %struct.List*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store %struct.List* %0, %struct.List** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %5
%9 = call i64 @strlen(i8* %8)
%10 = add nsw i64 %9, 1
%11 = call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = load i8*, i8** %5
%15 = load i8*, i8** %5
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %16, 1
%18 = call i32 @strncpy(i8* %13, i8* %14, i64 %17)
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 101
br i1 %20, label %21, label %28
21: ; preds = %3
%22 = load %struct.List*, %struct.List** %4
%23 = call i32 @reverseList(%struct.List* %22)
%24 = load %struct.List*, %struct.List** %4
%25 = call i32 @popFront(%struct.List* %24)
%26 = load %struct.List*, %struct.List** %4
%27 = call i32 @reverseList(%struct.List* %26)
br label %28
28: ; preds = %21, %3
%29 = load %struct.List*, %struct.List** %4
%30 = load i8*, i8** %7
%31 = call i32* @addFront(%struct.List* %29, i8* %30)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
ret void
}

LLVM IR (-O2):
%struct.List = type { i32 }
@str = [29 x i8] c"error: Unable to add history\00"
define void @addToHistory(%struct.List* %0, i8* %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add nsw i64 %4, 1
%6 = tail call i64 @malloc(i64 %5)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%9 = add nsw i64 %8, 1
%10 = tail call i32 @strncpy(i8* %7, i8* %1, i64 %9)
%11 = icmp eq i32 %2, 101
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = tail call i32 @reverseList(%struct.List* %0)
%14 = tail call i32 @popFront(%struct.List* %0)
%15 = tail call i32 @reverseList(%struct.List* %0)
br label %16
16: ; preds = %12, %3
%17 = tail call i32* @addFront(%struct.List* %0, i8* %7)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
ret void
}