; LLVM IR from vectozavr/3dzavr :: engine/io/Image.ll (Z11mirrorClamplt)

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

; 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: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3minItERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2), ptr noundef nonnull align 2 dereferenceable(2)) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i16 @_Z11mirrorClamplt(i64 noundef %0, i16 noundef zeroext %1) #2 {
  %3 = alloca i16, align 2
  %4 = alloca i64, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !9
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = icmp sge i64 %10, 0
  br i1 %11, label %12, label %20

12:                                               ; preds = %2
  %13 = load i64, ptr %4, align 8, !tbaa !5
  %14 = load i16, ptr %5, align 2, !tbaa !9
  %15 = zext i16 %14 to i64
  %16 = icmp slt i64 %13, %15
  br i1 %16, label %17, label %20

17:                                               ; preds = %12
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = trunc i64 %18 to i16
  store i16 %19, ptr %3, align 2
  br label %61

20:                                               ; preds = %12, %2
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  %21 = load i16, ptr %5, align 2, !tbaa !9
  %22 = zext i16 %21 to i32
  %23 = mul nsw i32 %22, 2
  %24 = trunc i32 %23 to i16
  store i16 %24, ptr %6, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #3
  %25 = load i64, ptr %4, align 8, !tbaa !5
  %26 = load i16, ptr %6, align 2, !tbaa !9
  %27 = zext i16 %26 to i64
  %28 = srem i64 %25, %27
  %29 = trunc i64 %28 to i16
  store i16 %29, ptr %7, align 2, !tbaa !9
  %30 = load i16, ptr %7, align 2, !tbaa !9
  %31 = sext i16 %30 to i32
  %32 = icmp slt i32 %31, 0
  br i1 %32, label %33, label %40

33:                                               ; preds = %20
  %34 = load i16, ptr %6, align 2, !tbaa !9
  %35 = zext i16 %34 to i32
  %36 = load i16, ptr %7, align 2, !tbaa !9
  %37 = sext i16 %36 to i32
  %38 = add nsw i32 %37, %35
  %39 = trunc i32 %38 to i16
  store i16 %39, ptr %7, align 2, !tbaa !9
  br label %40

40:                                               ; preds = %33, %20
  %41 = load i16, ptr %7, align 2, !tbaa !9
  %42 = sext i16 %41 to i32
  %43 = load i16, ptr %5, align 2, !tbaa !9
  %44 = zext i16 %43 to i32
  %45 = icmp sgt i32 %42, %44
  br i1 %45, label %46, label %53

46:                                               ; preds = %40
  %47 = load i16, ptr %6, align 2, !tbaa !9
  %48 = zext i16 %47 to i32
  %49 = load i16, ptr %7, align 2, !tbaa !9
  %50 = sext i16 %49 to i32
  %51 = sub nsw i32 %48, %50
  %52 = trunc i32 %51 to i16
  store i16 %52, ptr %7, align 2, !tbaa !9
  br label %53

53:                                               ; preds = %46, %40
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  %54 = load i16, ptr %7, align 2, !tbaa !9
  store i16 %54, ptr %8, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #3
  %55 = load i16, ptr %5, align 2, !tbaa !9
  %56 = zext i16 %55 to i32
  %57 = sub nsw i32 %56, 1
  %58 = trunc i32 %57 to i16
  store i16 %58, ptr %9, align 2, !tbaa !9
  %59 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3minItERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2) %8, ptr noundef nonnull align 2 dereferenceable(2) %9)
  %60 = load i16, ptr %59, align 2, !tbaa !9
  store i16 %60, ptr %3, align 2
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  br label %61

61:                                               ; preds = %53, %17
  %62 = load i16, ptr %3, align 2
  ret i16 %62
}

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

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, 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"

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3minItERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2), ptr noundef nonnull align 2 dereferenceable(2)) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i16 @_Z11mirrorClamplt(i64 noundef %0, i16 noundef zeroext %1) local_unnamed_addr #2 {
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = zext i16 %1 to i64
  %or.cond = icmp ugt i64 %5, %0
  br i1 %or.cond, label %6, label %8

6:                                                ; preds = %2
  %7 = trunc nuw i64 %0 to i16
  br label %22

8:                                                ; preds = %2
  %9 = zext i16 %1 to i32
  %10 = shl i16 %1, 1
  %11 = zext i16 %10 to i64
  %12 = srem i64 %0, %11
  %13 = trunc i64 %12 to i16
  %14 = icmp slt i16 %13, 0
  %15 = select i1 %14, i16 %10, i16 0
  %spec.select = add i16 %15, %13
  %16 = sext i16 %spec.select to i32
  %17 = icmp sgt i32 %16, %9
  %18 = sub i16 %10, %spec.select
  %.1 = select i1 %17, i16 %18, i16 %spec.select
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %3) #3
  store i16 %.1, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %4) #3
  %19 = add i16 %1, -1
  store i16 %19, ptr %4, align 2, !tbaa !5
  %20 = call noundef nonnull align 2 dereferenceable(2) ptr @_ZSt3minItERKT_S2_S2_(ptr noundef nonnull align 2 dereferenceable(2) %3, ptr noundef nonnull align 2 dereferenceable(2) %4)
  %21 = load i16, ptr %20, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %3) #3
  br label %22

22:                                               ; preds = %8, %6
  %.017 = phi i16 [ %7, %6 ], [ %21, %8 ]
  ret i16 %.017
}

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

!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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


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