; 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 }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local void @linenoiseEditDeletePrevWord(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.linenoiseState, ptr %5, i32 0, i32 6
  %7 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %7, ptr %3, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  br label %8

8:                                                ; preds = %27, %1
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.linenoiseState, ptr %9, i32 0, i32 6
  %11 = load i64, ptr %10, align 8, !tbaa !9
  %12 = icmp ugt i64 %11, 0
  br i1 %12, label %13, label %25

13:                                               ; preds = %8
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.linenoiseState, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !14
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.linenoiseState, ptr %17, i32 0, i32 6
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = sub i64 %19, 1
  %21 = getelementptr inbounds i8, ptr %16, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !15
  %23 = sext i8 %22 to i32
  %24 = icmp eq i32 %23, 32
  br label %25

25:                                               ; preds = %13, %8
  %26 = phi i1 [ false, %8 ], [ %24, %13 ]
  br i1 %26, label %27, label %32

27:                                               ; preds = %25
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.linenoiseState, ptr %28, i32 0, i32 6
  %30 = load i64, ptr %29, align 8, !tbaa !9
  %31 = add i64 %30, -1
  store i64 %31, ptr %29, align 8, !tbaa !9
  br label %8, !llvm.loop !16

32:                                               ; preds = %25
  br label %33

33:                                               ; preds = %52, %32
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.linenoiseState, ptr %34, i32 0, i32 6
  %36 = load i64, ptr %35, align 8, !tbaa !9
  %37 = icmp ugt i64 %36, 0
  br i1 %37, label %38, label %50

38:                                               ; preds = %33
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.linenoiseState, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.linenoiseState, ptr %42, i32 0, i32 6
  %44 = load i64, ptr %43, align 8, !tbaa !9
  %45 = sub i64 %44, 1
  %46 = getelementptr inbounds i8, ptr %41, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !15
  %48 = sext i8 %47 to i32
  %49 = icmp ne i32 %48, 32
  br label %50

50:                                               ; preds = %38, %33
  %51 = phi i1 [ false, %33 ], [ %49, %38 ]
  br i1 %51, label %52, label %57

52:                                               ; preds = %50
  %53 = load ptr, ptr %2, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.linenoiseState, ptr %53, i32 0, i32 6
  %55 = load i64, ptr %54, align 8, !tbaa !9
  %56 = add i64 %55, -1
  store i64 %56, ptr %54, align 8, !tbaa !9
  br label %33, !llvm.loop !18

57:                                               ; preds = %50
  %58 = load i64, ptr %3, align 8, !tbaa !13
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.linenoiseState, ptr %59, i32 0, i32 6
  %61 = load i64, ptr %60, align 8, !tbaa !9
  %62 = sub i64 %58, %61
  store i64 %62, ptr %4, align 8, !tbaa !13
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.linenoiseState, ptr %63, i32 0, i32 2
  %65 = load ptr, ptr %64, align 8, !tbaa !14
  %66 = load ptr, ptr %2, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.linenoiseState, ptr %66, i32 0, i32 6
  %68 = load i64, ptr %67, align 8, !tbaa !9
  %69 = getelementptr inbounds i8, ptr %65, i64 %68
  %70 = load ptr, ptr %2, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.linenoiseState, ptr %70, i32 0, i32 2
  %72 = load ptr, ptr %71, align 8, !tbaa !14
  %73 = load i64, ptr %3, align 8, !tbaa !13
  %74 = getelementptr inbounds i8, ptr %72, i64 %73
  %75 = load ptr, ptr %2, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.linenoiseState, ptr %75, i32 0, i32 8
  %77 = load i64, ptr %76, align 8, !tbaa !19
  %78 = load i64, ptr %3, align 8, !tbaa !13
  %79 = sub i64 %77, %78
  %80 = add i64 %79, 1
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %69, ptr align 1 %74, i64 %80, i1 false)
  %81 = load i64, ptr %4, align 8, !tbaa !13
  %82 = load ptr, ptr %2, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.linenoiseState, ptr %82, i32 0, i32 8
  %84 = load i64, ptr %83, align 8, !tbaa !19
  %85 = sub i64 %84, %81
  store i64 %85, ptr %83, align 8, !tbaa !19
  %86 = load ptr, ptr %2, align 8, !tbaa !5
  call void @refreshLine(ptr noundef %86)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind }

!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, !12, i64 40}
!10 = !{!"linenoiseState", !11, i64 0, !11, i64 4, !6, i64 8, !12, i64 16, !6, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !11, i64 80}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!10, !6, i64 8}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = !{!10, !12, 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"

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @linenoiseEditDeletePrevWord(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 40
  %3 = load i64, ptr %2, align 8, !tbaa !5
  %.not25 = icmp eq i64 %3, 0
  br i1 %.not25, label %.critedge..critedge2_crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !12
  %invariant.gep = getelementptr i8, ptr %5, i64 -1
  br label %6

6:                                                ; preds = %.lr.ph, %10
  %7 = phi i64 [ %3, %.lr.ph ], [ %11, %10 ]
  %gep = getelementptr i8, ptr %invariant.gep, i64 %7
  %8 = load i8, ptr %gep, align 1, !tbaa !13
  %9 = icmp eq i8 %8, 32
  br i1 %9, label %10, label %.lr.ph28

10:                                               ; preds = %6
  %11 = add i64 %7, -1
  store i64 %11, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i64 %11, 0
  br i1 %.not, label %.critedge..critedge2_crit_edge, label %6, !llvm.loop !14

.critedge..critedge2_crit_edge:                   ; preds = %10, %1
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 8
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !12
  br label %.critedge2

.lr.ph28:                                         ; preds = %6
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %invariant.gep31 = getelementptr i8, ptr %13, i64 -1
  br label %14

14:                                               ; preds = %.lr.ph28, %17
  %15 = phi i64 [ %7, %.lr.ph28 ], [ %18, %17 ]
  %gep32 = getelementptr i8, ptr %invariant.gep31, i64 %15
  %16 = load i8, ptr %gep32, align 1, !tbaa !13
  %.not23 = icmp eq i8 %16, 32
  br i1 %.not23, label %.critedge2, label %17

17:                                               ; preds = %14
  %18 = add i64 %15, -1
  store i64 %18, ptr %2, align 8, !tbaa !5
  %.not22 = icmp eq i64 %18, 0
  br i1 %.not22, label %.critedge2, label %14, !llvm.loop !16

.critedge2:                                       ; preds = %14, %17, %.critedge..critedge2_crit_edge
  %19 = phi ptr [ %.pre, %.critedge..critedge2_crit_edge ], [ %13, %17 ], [ %13, %14 ]
  %.lcssa = phi i64 [ 0, %.critedge..critedge2_crit_edge ], [ %15, %14 ], [ 0, %17 ]
  %.neg = sub i64 %.lcssa, %3
  %20 = getelementptr inbounds i8, ptr %19, i64 %.lcssa
  %21 = getelementptr inbounds i8, ptr %19, i64 %3
  %22 = getelementptr inbounds i8, ptr %0, i64 56
  %23 = load i64, ptr %22, align 8, !tbaa !17
  %reass.sub = sub i64 %23, %3
  %24 = add i64 %reass.sub, 1
  tail call void @llvm.memmove.p0.p0.i64(ptr align 1 %20, ptr align 1 %21, i64 %24, i1 false)
  %25 = load i64, ptr %22, align 8, !tbaa !17
  %26 = add i64 %.neg, %25
  store i64 %26, ptr %22, align 8, !tbaa !17
  tail call void @refreshLine(ptr noundef nonnull %0)
  ret void
}

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !11, i64 40}
!6 = !{!"linenoiseState", !7, i64 0, !7, i64 4, !10, i64 8, !11, i64 16, !10, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !11, i64 72, !7, i64 80}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !10, i64 8}
!13 = !{!8, !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!6, !11, i64 56}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass