; LLVM IR from steps3d/graphics-book :: Code/Chapter-7/line.ll (Z9drawLine5R8TgaImageiiiij)

; 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: mustprogress nounwind uwtable
declare dso_local void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef, i32 noundef, i32 noundef) #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z9drawLine5R8TgaImageiiiij(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) #2 {
  %7 = alloca ptr, align 8
  %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
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  %19 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store i32 %2, ptr %9, align 4, !tbaa !9
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  store i32 %5, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = sub nsw i32 %20, %21
  store i32 %22, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %23 = load i32, ptr %11, align 4, !tbaa !9
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = sub nsw i32 %23, %24
  store i32 %25, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %26 = load i32, ptr %14, align 4, !tbaa !9
  %27 = shl i32 %26, 1
  %28 = load i32, ptr %13, align 4, !tbaa !9
  %29 = sub nsw i32 %27, %28
  store i32 %29, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  %30 = load i32, ptr %14, align 4, !tbaa !9
  %31 = shl i32 %30, 1
  store i32 %31, ptr %16, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #3
  %32 = load i32, ptr %14, align 4, !tbaa !9
  %33 = load i32, ptr %13, align 4, !tbaa !9
  %34 = sub nsw i32 %32, %33
  %35 = shl i32 %34, 1
  store i32 %35, ptr %17, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #3
  %36 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %36, ptr %18, align 4, !tbaa !9
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = load i32, ptr %12, align 4, !tbaa !9
  call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %37, i32 noundef %38, i32 noundef %39, i32 noundef %40)
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #3
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %19, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %66, %6
  %44 = load i32, ptr %19, align 4, !tbaa !9
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = icmp sle i32 %44, %45
  br i1 %46, label %48, label %47

47:                                               ; preds = %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #3
  br label %69

48:                                               ; preds = %43
  %49 = load i32, ptr %15, align 4, !tbaa !9
  %50 = icmp sgt i32 %49, 0
  br i1 %50, label %51, label %57

51:                                               ; preds = %48
  %52 = load i32, ptr %17, align 4, !tbaa !9
  %53 = load i32, ptr %15, align 4, !tbaa !9
  %54 = add nsw i32 %53, %52
  store i32 %54, ptr %15, align 4, !tbaa !9
  %55 = load i32, ptr %18, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %18, align 4, !tbaa !9
  br label %61

57:                                               ; preds = %48
  %58 = load i32, ptr %16, align 4, !tbaa !9
  %59 = load i32, ptr %15, align 4, !tbaa !9
  %60 = add nsw i32 %59, %58
  store i32 %60, ptr %15, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %57, %51
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = load i32, ptr %19, align 4, !tbaa !9
  %64 = load i32, ptr %18, align 4, !tbaa !9
  %65 = load i32, ptr %12, align 4, !tbaa !9
  call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %62, i32 noundef %63, i32 noundef %64, i32 noundef %65)
  br label %66

66:                                               ; preds = %61
  %67 = load i32, ptr %19, align 4, !tbaa !9
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %19, align 4, !tbaa !9
  br label %43, !llvm.loop !11

69:                                               ; preds = %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  ret void
}

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


; 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 nounwind uwtable
declare dso_local void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z9drawLine5R8TgaImageiiiij(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  %7 = sub nsw i32 %3, %1
  %8 = sub nsw i32 %4, %2
  %9 = shl i32 %8, 1
  %10 = sub nsw i32 %8, %7
  %11 = shl i32 %10, 1
  tail call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2, i32 noundef %5)
  %.not.not30 = icmp slt i32 %1, %3
  br i1 %.not.not30, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %6
  %12 = sub nsw i32 %9, %7
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %6
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.0.in33 = phi i32 [ %.0, %.lr.ph ], [ %1, %.lr.ph.preheader ]
  %.02732 = phi i32 [ %.1, %.lr.ph ], [ %2, %.lr.ph.preheader ]
  %.02831 = phi i32 [ %.129, %.lr.ph ], [ %12, %.lr.ph.preheader ]
  %.0 = add nsw i32 %.0.in33, 1
  %13 = icmp sgt i32 %.02831, 0
  %.pn = select i1 %13, i32 %11, i32 %9
  %14 = zext i1 %13 to i32
  %.1 = add nsw i32 %.02732, %14
  %.129 = add nsw i32 %.pn, %.02831
  tail call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %.0, i32 noundef %.1, i32 noundef %5)
  %exitcond.not = icmp eq i32 %.0, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5
}

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

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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