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

; 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 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 dso_local i32 @GuiTextBoxGetByteIndex(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #1

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

8:                                                ; preds = %1
  %9 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  %10 = icmp ne i32 %9, -1
  br i1 %10, label %11, label %41

11:                                               ; preds = %8
  %12 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  %13 = icmp ne i32 %12, -1
  br i1 %13, label %14, label %41

14:                                               ; preds = %11
  %15 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  %16 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  %17 = icmp ne i32 %15, %16
  br i1 %17, label %18, label %41

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %19 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  store i32 %19, ptr %3, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %20 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  store i32 %20, ptr %4, align 4, !tbaa !13
  %21 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  %22 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  %23 = icmp sgt i32 %21, %22
  br i1 %23, label %24, label %27

24:                                               ; preds = %18
  %25 = load i32, ptr getelementptr inbounds (%struct.GuiTextBoxState, ptr @guiTextBoxState, i32 0, i32 3), align 4, !tbaa !9
  store i32 %25, ptr %3, align 4, !tbaa !13
  %26 = load i32, ptr @guiTextBoxState, align 4, !tbaa !12
  store i32 %26, ptr %4, align 4, !tbaa !13
  br label %27

27:                                               ; preds = %24, %18
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = load i32, ptr %3, align 4, !tbaa !13
  %30 = call i32 @GuiTextBoxGetByteIndex(ptr noundef %28, i32 noundef 0, i32 noundef 0, i32 noundef %29)
  store i32 %30, ptr %3, align 4, !tbaa !13
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = load i32, ptr %4, align 4, !tbaa !13
  %33 = call i32 @GuiTextBoxGetByteIndex(ptr noundef %31, i32 noundef 0, i32 noundef 0, i32 noundef %32)
  store i32 %33, ptr %4, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = load i32, ptr %3, align 4, !tbaa !13
  %36 = load i32, ptr %4, align 4, !tbaa !13
  %37 = load i32, ptr %3, align 4, !tbaa !13
  %38 = sub nsw i32 %36, %37
  %39 = call ptr @TextSubtext(ptr noundef %34, i32 noundef %35, i32 noundef %38)
  store ptr %39, ptr %5, align 8, !tbaa !5
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  call void @SetClipboardText(ptr noundef %40)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %41

41:                                               ; preds = %27, %14, %11, %8, %1
  ret void
}

declare ptr @TextSubtext(ptr noundef, i32 noundef, i32 noundef) #2

declare void @SetClipboardText(ptr noundef) #2

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 = { "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 }

!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, !11, i64 12}
!10 = !{!"GuiTextBoxState", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}


; 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: nounwind uwtable
declare dso_local i32 @GuiTextBoxGetByteIndex(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @GuiTextBoxCopy(ptr noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @guiTextBoxState, i64 12), align 4
  %4 = icmp eq i32 %3, -1
  %or.cond.not22 = select i1 %2, i1 true, i1 %4
  %5 = load i32, ptr @guiTextBoxState, align 4
  %6 = icmp eq i32 %5, -1
  %or.cond3.not19 = select i1 %or.cond.not22, i1 true, i1 %6
  %.not = icmp eq i32 %3, %5
  %or.cond16 = select i1 %or.cond3.not19, i1 true, i1 %.not
  br i1 %or.cond16, label %12, label %7

7:                                                ; preds = %1
  %spec.select = tail call i32 @llvm.smax.i32(i32 %5, i32 %3)
  %spec.select17 = tail call i32 @llvm.smin.i32(i32 %5, i32 %3)
  %8 = tail call i32 @GuiTextBoxGetByteIndex(ptr noundef nonnull %0, i32 noundef 0, i32 noundef 0, i32 noundef %spec.select17)
  %9 = tail call i32 @GuiTextBoxGetByteIndex(ptr noundef nonnull %0, i32 noundef 0, i32 noundef 0, i32 noundef %spec.select)
  %10 = sub nsw i32 %9, %8
  %11 = tail call ptr @TextSubtext(ptr noundef nonnull %0, i32 noundef %8, i32 noundef %10) #3
  tail call void @SetClipboardText(ptr noundef %11) #3
  br label %12

12:                                               ; preds = %7, %1
  ret void
}

declare ptr @TextSubtext(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @SetClipboardText(ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

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 = { "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 nosync nounwind speculatable willreturn memory(none) }
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)"}


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