; LLVM IR from NotManyIdeasDev/double-pendulum-sim :: double-pendulum-sim/Libraries/raygui-2.8/examples/text_box_selection (GuiTextBoxPaste)

; 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.GuiTextBoxState = type { i32, i32, i32, i32 }

@guiTextBoxState = external hidden global %struct.GuiTextBoxState, align 4

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @GuiTextBoxGetByteIndex(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #3

declare i32 @GetNextCodepoint(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind uwtable
declare dso_local i32 @GuiTextBoxDelete(ptr noundef, i32 noundef, i1 noundef zeroext) #3

; 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) #0

; Function Attrs: nounwind uwtable
define dso_local void @GuiTextBoxPaste(ptr noundef %0, i32 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %14 = call ptr @GetClipboardText()
  store ptr %14, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = call i64 @strlen(ptr noundef %15) #6
  %17 = trunc i64 %16 to i32
  store i32 %17, ptr %6, align 4, !tbaa !9
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %114

20:                                               ; preds = %2
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %114

23:                                               ; preds = %20
  %24 = load i32, ptr @guiTextBoxState, align 4, !tbaa !11
  %25 = icmp ne i32 %24, -1
  br i1 %25, label %26, label %114

26:                                               ; preds = %23
  %27 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !13
  %28 = icmp ne i32 %27, -1
  br i1 %28, label %29, label %39

29:                                               ; preds = %26
  %30 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !13
  %31 = load i32, ptr @guiTextBoxState, align 4, !tbaa !11
  %32 = icmp ne i32 %30, %31
  br i1 %32, label %33, label %39

33:                                               ; preds = %29
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = call i32 @GuiTextBoxDelete(ptr noundef %34, i32 noundef %35, i1 noundef zeroext true)
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = sub nsw i32 %37, %36
  store i32 %38, ptr %6, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %33, %29, %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = call i64 @strlen(ptr noundef %40) #6
  %42 = trunc i64 %41 to i32
  store i32 %42, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = add nsw i32 %43, %44
  %46 = load i32, ptr %4, align 4, !tbaa !9
  %47 = icmp sle i32 %45, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %39
  %49 = load i32, ptr %7, align 4, !tbaa !9
  br label %54

50:                                               ; preds = %39
  %51 = load i32, ptr %4, align 4, !tbaa !9
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = sub nsw i32 %51, %52
  br label %54

54:                                               ; preds = %50, %48
  %55 = phi i32 [ %49, %48 ], [ %53, %50 ]
  store i32 %55, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = load i32, ptr @guiTextBoxState, align 4, !tbaa !11
  %58 = call i32 @GuiTextBoxGetByteIndex(ptr noundef %56, i32 noundef 0, i32 noundef 0, i32 noundef %57)
  store i32 %58, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %59 = load i32, ptr %9, align 4, !tbaa !9
  %60 = load i32, ptr %8, align 4, !tbaa !9
  %61 = add nsw i32 %59, %60
  store i32 %61, ptr %10, align 4, !tbaa !9
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = load i32, ptr %10, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds i8, ptr %62, i64 %64
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds i8, ptr %66, i64 %68
  %70 = load i32, ptr %6, align 4, !tbaa !9
  %71 = load i32, ptr %9, align 4, !tbaa !9
  %72 = sub nsw i32 %70, %71
  %73 = sext i32 %72 to i64
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %65, ptr align 1 %69, i64 %73, i1 false)
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = load i32, ptr %6, align 4, !tbaa !9
  %76 = load i32, ptr %8, align 4, !tbaa !9
  %77 = add nsw i32 %75, %76
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds i8, ptr %74, i64 %78
  store i8 0, ptr %79, align 1, !tbaa !14
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = load i32, ptr %9, align 4, !tbaa !9
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds i8, ptr %80, i64 %82
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = load i32, ptr %8, align 4, !tbaa !9
  %86 = sext i32 %85 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %83, ptr align 1 %84, i64 %86, i1 false)
  store i32 0, ptr @guiTextBoxState, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %87

87:                                               ; preds = %110, %54
  %88 = load i32, ptr %11, align 4, !tbaa !9
  %89 = load i32, ptr %9, align 4, !tbaa !9
  %90 = load i32, ptr %8, align 4, !tbaa !9
  %91 = add nsw i32 %89, %90
  %92 = icmp slt i32 %88, %91
  br i1 %92, label %94, label %93

93:                                               ; preds = %87
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  br label %113

94:                                               ; preds = %87
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  store i32 0, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %95 = load ptr, ptr %3, align 8, !tbaa !5
  %96 = load i32, ptr %11, align 4, !tbaa !9
  %97 = sext i32 %96 to i64
  %98 = getelementptr inbounds i8, ptr %95, i64 %97
  %99 = call i32 @GetNextCodepoint(ptr noundef %98, ptr noundef %12)
  store i32 %99, ptr %13, align 4, !tbaa !9
  %100 = load i32, ptr %13, align 4, !tbaa !9
  %101 = icmp ne i32 %100, 63
  br i1 %101, label %102, label %106

102:                                              ; preds = %94
  %103 = load i32, ptr %12, align 4, !tbaa !9
  %104 = load i32, ptr %11, align 4, !tbaa !9
  %105 = add nsw i32 %104, %103
  store i32 %105, ptr %11, align 4, !tbaa !9
  br label %109

106:                                              ; preds = %94
  %107 = load i32, ptr %11, align 4, !tbaa !9
  %108 = add nsw i32 %107, 1
  store i32 %108, ptr %11, align 4, !tbaa !9
  br label %109

109:                                              ; preds = %106, %102
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %110

110:                                              ; preds = %109
  %111 = load i32, ptr @guiTextBoxState, align 4, !tbaa !11
  %112 = add nsw i32 %111, 1
  store i32 %112, ptr @guiTextBoxState, align 4, !tbaa !11
  br label %87, !llvm.loop !15

113:                                              ; preds = %93
  store i32 -1, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 1), align 4, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %114

114:                                              ; preds = %113, %23, %20, %2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret void
}

declare ptr @GetClipboardText() #4

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 #4 = { "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 = !{!"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 0}
!12 = !{!"GuiTextBoxState", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12}
!13 = !{!12, !10, i64 12}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!12, !10, i64 4}


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

%struct.GuiTextBoxState = type { i32, i32, i32, i32 }

@guiTextBoxState = external hidden local_unnamed_addr global %struct.GuiTextBoxState, align 4

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @GuiTextBoxGetByteIndex(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

declare i32 @GetNextCodepoint(ptr noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
declare dso_local i32 @GuiTextBoxDelete(ptr noundef, i32 noundef, i1 noundef zeroext) local_unnamed_addr #3

; 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) #0

; Function Attrs: nounwind uwtable
define dso_local void @GuiTextBoxPaste(ptr noundef %0, i32 noundef %1) local_unnamed_addr #3 {
  %3 = alloca i32, align 4
  %4 = tail call ptr @GetClipboardText() #5
  %5 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #6
  %6 = trunc i64 %5 to i32
  %7 = icmp ne ptr %0, null
  %8 = icmp ne ptr %4, null
  %or.cond = select i1 %7, i1 %8, i1 false
  %9 = load i32, ptr @guiTextBoxState, align 4
  %10 = icmp ne i32 %9, -1
  %or.cond3 = select i1 %or.cond, i1 %10, i1 false
  br i1 %or.cond3, label %11, label %43

11:                                               ; preds = %2
  %12 = load i32, ptr getelementptr inbounds (i8, ptr @guiTextBoxState, i64 12), align 4, !tbaa !5
  %.not = icmp eq i32 %12, -1
  %.not44 = icmp eq i32 %12, %9
  %or.cond47 = select i1 %.not, i1 true, i1 %.not44
  br i1 %or.cond47, label %16, label %13

13:                                               ; preds = %11
  %14 = tail call i32 @GuiTextBoxDelete(ptr noundef nonnull %0, i32 noundef %6, i1 noundef zeroext true)
  %15 = sub nsw i32 %6, %14
  %.pre = load i32, ptr @guiTextBoxState, align 4, !tbaa !10
  br label %16

16:                                               ; preds = %13, %11
  %17 = phi i32 [ %.pre, %13 ], [ %9, %11 ]
  %.0 = phi i32 [ %15, %13 ], [ %6, %11 ]
  %18 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %4) #6
  %19 = trunc i64 %18 to i32
  %20 = add nsw i32 %.0, %19
  %.not45 = icmp sgt i32 %20, %1
  %21 = sub nsw i32 %1, %.0
  %22 = select i1 %.not45, i32 %21, i32 %19
  %23 = tail call i32 @GuiTextBoxGetByteIndex(ptr noundef nonnull %0, i32 noundef 0, i32 noundef 0, i32 noundef %17)
  %24 = add nsw i32 %22, %23
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i8, ptr %0, i64 %25
  %27 = sext i32 %23 to i64
  %28 = getelementptr inbounds i8, ptr %0, i64 %27
  %29 = sub nsw i32 %.0, %23
  %30 = sext i32 %29 to i64
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 1 %26, ptr nonnull align 1 %28, i64 %30, i1 false)
  %31 = add nsw i32 %22, %.0
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i8, ptr %0, i64 %32
  store i8 0, ptr %33, align 1, !tbaa !11
  %34 = sext i32 %22 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %28, ptr nonnull align 1 %4, i64 %34, i1 false)
  store i32 0, ptr @guiTextBoxState, align 4, !tbaa !10
  %35 = icmp sgt i32 %24, 0
  br i1 %35, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %16
  store i32 -1, ptr getelementptr inbounds (i8, ptr @guiTextBoxState, i64 4), align 4, !tbaa !12
  br label %43

.lr.ph:                                           ; preds = %16, %.lr.ph
  %.03948 = phi i32 [ %.1, %.lr.ph ], [ 0, %16 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  store i32 0, ptr %3, align 4, !tbaa !13
  %36 = sext i32 %.03948 to i64
  %37 = getelementptr inbounds i8, ptr %0, i64 %36
  %38 = call i32 @GetNextCodepoint(ptr noundef nonnull %37, ptr noundef nonnull %3) #5
  %.not46 = icmp eq i32 %38, 63
  %39 = load i32, ptr %3, align 4
  %.pn = select i1 %.not46, i32 1, i32 %39
  %.1 = add nsw i32 %.pn, %.03948
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  %40 = load i32, ptr @guiTextBoxState, align 4, !tbaa !10
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr @guiTextBoxState, align 4, !tbaa !10
  %42 = icmp slt i32 %.1, %24
  br i1 %42, label %.lr.ph, label %._crit_edge, !llvm.loop !14

43:                                               ; preds = %._crit_edge, %2
  ret void
}

declare ptr @GetClipboardText() local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 #4 = { "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, !7, i64 12}
!6 = !{!"GuiTextBoxState", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!8, !8, i64 0}
!12 = !{!6, !7, i64 4}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass