; LLVM IR from adventure-db/adventure :: lib/linenoise (linenoise)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.linenoiseState = type { i32, i32, ptr, i64, ptr, i64, i64, i64, i64, i64, i64, i32 }

@history_len = external hidden global i32, align 4
@history = external hidden global ptr, align 8

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @refreshLine(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @linenoiseEditHistoryNext(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %5 = load i32, ptr @history_len, align 4, !tbaa !9
  %6 = icmp sgt i32 %5, 1
  br i1 %6, label %7, label %93

7:                                                ; preds = %2
  %8 = load ptr, ptr @history, align 8, !tbaa !5
  %9 = load i32, ptr @history_len, align 4, !tbaa !9
  %10 = sub nsw i32 %9, 1
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.linenoiseState, ptr %11, i32 0, i32 11
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = sub nsw i32 %10, %13
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds ptr, ptr %8, i64 %15
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  call void @free(ptr noundef %17) #3
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.linenoiseState, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = call noalias ptr @strdup(ptr noundef %20) #3
  %22 = load ptr, ptr @history, align 8, !tbaa !5
  %23 = load i32, ptr @history_len, align 4, !tbaa !9
  %24 = sub nsw i32 %23, 1
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.linenoiseState, ptr %25, i32 0, i32 11
  %27 = load i32, ptr %26, align 8, !tbaa !11
  %28 = sub nsw i32 %24, %27
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds ptr, ptr %22, i64 %29
  store ptr %21, ptr %30, align 8, !tbaa !5
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = icmp eq i32 %31, 1
  %33 = zext i1 %32 to i64
  %34 = select i1 %32, i32 1, i32 -1
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.linenoiseState, ptr %35, i32 0, i32 11
  %37 = load i32, ptr %36, align 8, !tbaa !11
  %38 = add nsw i32 %37, %34
  store i32 %38, ptr %36, align 8, !tbaa !11
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.linenoiseState, ptr %39, i32 0, i32 11
  %41 = load i32, ptr %40, align 8, !tbaa !11
  %42 = icmp slt i32 %41, 0
  br i1 %42, label %43, label %46

43:                                               ; preds = %7
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.linenoiseState, ptr %44, i32 0, i32 11
  store i32 0, ptr %45, align 8, !tbaa !11
  br label %93

46:                                               ; preds = %7
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.linenoiseState, ptr %47, i32 0, i32 11
  %49 = load i32, ptr %48, align 8, !tbaa !11
  %50 = load i32, ptr @history_len, align 4, !tbaa !9
  %51 = icmp sge i32 %49, %50
  br i1 %51, label %52, label %57

52:                                               ; preds = %46
  %53 = load i32, ptr @history_len, align 4, !tbaa !9
  %54 = sub nsw i32 %53, 1
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.linenoiseState, ptr %55, i32 0, i32 11
  store i32 %54, ptr %56, align 8, !tbaa !11
  br label %93

57:                                               ; preds = %46
  br label %58

58:                                               ; preds = %57
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.linenoiseState, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 8, !tbaa !14
  %62 = load ptr, ptr @history, align 8, !tbaa !5
  %63 = load i32, ptr @history_len, align 4, !tbaa !9
  %64 = sub nsw i32 %63, 1
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.linenoiseState, ptr %65, i32 0, i32 11
  %67 = load i32, ptr %66, align 8, !tbaa !11
  %68 = sub nsw i32 %64, %67
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds ptr, ptr %62, i64 %69
  %71 = load ptr, ptr %70, align 8, !tbaa !5
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.linenoiseState, ptr %72, i32 0, i32 3
  %74 = load i64, ptr %73, align 8, !tbaa !15
  %75 = call ptr @strncpy(ptr noundef %61, ptr noundef %71, i64 noundef %74) #3
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.linenoiseState, ptr %76, i32 0, i32 2
  %78 = load ptr, ptr %77, align 8, !tbaa !14
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.linenoiseState, ptr %79, i32 0, i32 3
  %81 = load i64, ptr %80, align 8, !tbaa !15
  %82 = sub i64 %81, 1
  %83 = getelementptr inbounds i8, ptr %78, i64 %82
  store i8 0, ptr %83, align 1, !tbaa !16
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.linenoiseState, ptr %84, i32 0, i32 2
  %86 = load ptr, ptr %85, align 8, !tbaa !14
  %87 = call i64 @strlen(ptr noundef %86) #4
  %88 = load ptr, ptr %3, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.linenoiseState, ptr %88, i32 0, i32 6
  store i64 %87, ptr %89, align 8, !tbaa !17
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.linenoiseState, ptr %90, i32 0, i32 8
  store i64 %87, ptr %91, align 8, !tbaa !18
  %92 = load ptr, ptr %3, align 8, !tbaa !5
  call void @refreshLine(ptr noundef %92)
  br label %93

93:                                               ; preds = %58, %52, %43, %2
  ret void
}

; Function Attrs: nounwind
declare noalias ptr @strdup(ptr noundef) #1

; Function Attrs: nounwind
declare ptr @strncpy(ptr noundef, ptr noundef, i64 noundef) #1

attributes #0 = { nounwind willreturn memory(read) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind }
attributes #4 = { nounwind willreturn memory(read) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 80}
!12 = !{!"linenoiseState", !10, i64 0, !10, i64 4, !6, i64 8, !13, i64 16, !6, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !13, i64 72, !10, i64 80}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !13, i64 16}
!16 = !{!7, !7, i64 0}
!17 = !{!12, !13, i64 40}
!18 = !{!12, !13, i64 56}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@history_len = external hidden local_unnamed_addr global i32, align 4
@history = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @refreshLine(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @linenoiseEditHistoryNext(ptr noundef %0, i32 noundef %1) local_unnamed_addr #2 {
  %3 = load i32, ptr @history_len, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 1
  br i1 %4, label %5, label %51

5:                                                ; preds = %2
  %6 = load ptr, ptr @history, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %0, i64 80
  %8 = load i32, ptr %7, align 8, !tbaa !11
  %9 = xor i32 %8, -1
  %10 = add i32 %3, %9
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds ptr, ptr %6, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  tail call void @free(ptr noundef %13) #5
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  %16 = tail call noalias ptr @strdup(ptr noundef %15) #5
  %17 = load ptr, ptr @history, align 8, !tbaa !9
  %18 = load i32, ptr @history_len, align 4, !tbaa !5
  %19 = load i32, ptr %7, align 8, !tbaa !11
  %20 = xor i32 %19, -1
  %21 = add i32 %18, %20
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds ptr, ptr %17, i64 %22
  store ptr %16, ptr %23, align 8, !tbaa !9
  %24 = icmp eq i32 %1, 1
  %25 = select i1 %24, i32 1, i32 -1
  %26 = add nsw i32 %19, %25
  store i32 %26, ptr %7, align 8, !tbaa !11
  %27 = icmp slt i32 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %5
  store i32 0, ptr %7, align 8, !tbaa !11
  br label %51

29:                                               ; preds = %5
  %.not = icmp slt i32 %26, %18
  br i1 %.not, label %32, label %30

30:                                               ; preds = %29
  %31 = add nsw i32 %18, -1
  store i32 %31, ptr %7, align 8, !tbaa !11
  br label %51

32:                                               ; preds = %29
  %33 = load ptr, ptr %14, align 8, !tbaa !14
  %34 = load ptr, ptr @history, align 8, !tbaa !9
  %35 = xor i32 %26, -1
  %36 = add nsw i32 %18, %35
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds ptr, ptr %34, i64 %37
  %39 = load ptr, ptr %38, align 8, !tbaa !9
  %40 = getelementptr inbounds i8, ptr %0, i64 16
  %41 = load i64, ptr %40, align 8, !tbaa !15
  %42 = tail call ptr @strncpy(ptr noundef %33, ptr noundef %39, i64 noundef %41) #5
  %43 = load ptr, ptr %14, align 8, !tbaa !14
  %44 = load i64, ptr %40, align 8, !tbaa !15
  %45 = getelementptr i8, ptr %43, i64 %44
  %46 = getelementptr i8, ptr %45, i64 -1
  store i8 0, ptr %46, align 1, !tbaa !16
  %47 = load ptr, ptr %14, align 8, !tbaa !14
  %48 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %47) #6
  %49 = getelementptr inbounds i8, ptr %0, i64 40
  store i64 %48, ptr %49, align 8, !tbaa !17
  %50 = getelementptr inbounds i8, ptr %0, i64 56
  store i64 %48, ptr %50, align 8, !tbaa !18
  tail call void @refreshLine(ptr noundef nonnull %0)
  br label %51

51:                                               ; preds = %32, %30, %28, %2
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias ptr @strdup(ptr nocapture noundef readonly) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strncpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly, i64 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nofree nounwind willreturn memory(argmem: read) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 80}
!12 = !{!"linenoiseState", !6, i64 0, !6, i64 4, !10, i64 8, !13, i64 16, !10, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !13, i64 72, !6, i64 80}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!12, !13, i64 16}
!16 = !{!7, !7, i64 0}
!17 = !{!12, !13, i64 40}
!18 = !{!12, !13, i64 56}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass