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

; 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 @_Z8drawLineR8TgaImageiiiij(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
  %20 = alloca i32, align 4
  %21 = alloca i32, align 4
  %22 = alloca i32, align 4
  %23 = alloca i32, align 4
  %24 = alloca i32, align 4
  %25 = alloca i32, align 4
  %26 = alloca i32, align 4
  %27 = alloca i32, align 4
  %28 = 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) #4
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = sub nsw i32 %29, %30
  %32 = call i32 @llvm.abs.i32(i32 %31, i1 true)
  store i32 %32, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = sub nsw i32 %33, %34
  %36 = call i32 @llvm.abs.i32(i32 %35, i1 true)
  store i32 %36, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  %37 = load i32, ptr %10, align 4, !tbaa !9
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = icmp sge i32 %37, %38
  %40 = zext i1 %39 to i64
  %41 = select i1 %39, i32 1, i32 -1
  store i32 %41, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = icmp sge i32 %42, %43
  %45 = zext i1 %44 to i64
  %46 = select i1 %44, i32 1, i32 -1
  store i32 %46, ptr %16, align 4, !tbaa !9
  %47 = load i32, ptr %14, align 4, !tbaa !9
  %48 = load i32, ptr %13, align 4, !tbaa !9
  %49 = icmp sle i32 %47, %48
  br i1 %49, label %50, label %100

50:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  %51 = load i32, ptr %14, align 4, !tbaa !9
  %52 = shl i32 %51, 1
  %53 = load i32, ptr %13, align 4, !tbaa !9
  %54 = sub nsw i32 %52, %53
  store i32 %54, ptr %17, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #4
  %55 = load i32, ptr %14, align 4, !tbaa !9
  %56 = shl i32 %55, 1
  store i32 %56, ptr %18, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #4
  %57 = load i32, ptr %14, align 4, !tbaa !9
  %58 = load i32, ptr %13, align 4, !tbaa !9
  %59 = sub nsw i32 %57, %58
  %60 = shl i32 %59, 1
  store i32 %60, ptr %19, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #4
  %61 = load i32, ptr %8, align 4, !tbaa !9
  %62 = load i32, ptr %15, align 4, !tbaa !9
  %63 = add nsw i32 %61, %62
  store i32 %63, ptr %20, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %21) #4
  %64 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %64, ptr %21, align 4, !tbaa !9
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = load i32, ptr %8, align 4, !tbaa !9
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = load i32, ptr %12, align 4, !tbaa !9
  call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %65, i32 noundef %66, i32 noundef %67, i32 noundef %68)
  call void @llvm.lifetime.start.p0(i64 4, ptr %22) #4
  store i32 1, ptr %22, align 4, !tbaa !9
  br label %69

69:                                               ; preds = %93, %50
  %70 = load i32, ptr %22, align 4, !tbaa !9
  %71 = load i32, ptr %13, align 4, !tbaa !9
  %72 = icmp sle i32 %70, %71
  br i1 %72, label %74, label %73

73:                                               ; preds = %69
  call void @llvm.lifetime.end.p0(i64 4, ptr %22) #4
  br label %99

74:                                               ; preds = %69
  %75 = load i32, ptr %17, align 4, !tbaa !9
  %76 = icmp sgt i32 %75, 0
  br i1 %76, label %77, label %84

77:                                               ; preds = %74
  %78 = load i32, ptr %19, align 4, !tbaa !9
  %79 = load i32, ptr %17, align 4, !tbaa !9
  %80 = add nsw i32 %79, %78
  store i32 %80, ptr %17, align 4, !tbaa !9
  %81 = load i32, ptr %16, align 4, !tbaa !9
  %82 = load i32, ptr %21, align 4, !tbaa !9
  %83 = add nsw i32 %82, %81
  store i32 %83, ptr %21, align 4, !tbaa !9
  br label %88

84:                                               ; preds = %74
  %85 = load i32, ptr %18, align 4, !tbaa !9
  %86 = load i32, ptr %17, align 4, !tbaa !9
  %87 = add nsw i32 %86, %85
  store i32 %87, ptr %17, align 4, !tbaa !9
  br label %88

88:                                               ; preds = %84, %77
  %89 = load ptr, ptr %7, align 8, !tbaa !5
  %90 = load i32, ptr %20, align 4, !tbaa !9
  %91 = load i32, ptr %21, align 4, !tbaa !9
  %92 = load i32, ptr %12, align 4, !tbaa !9
  call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %89, i32 noundef %90, i32 noundef %91, i32 noundef %92)
  br label %93

93:                                               ; preds = %88
  %94 = load i32, ptr %22, align 4, !tbaa !9
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %22, align 4, !tbaa !9
  %96 = load i32, ptr %15, align 4, !tbaa !9
  %97 = load i32, ptr %20, align 4, !tbaa !9
  %98 = add nsw i32 %97, %96
  store i32 %98, ptr %20, align 4, !tbaa !9
  br label %69, !llvm.loop !11

99:                                               ; preds = %73
  call void @llvm.lifetime.end.p0(i64 4, ptr %21) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  br label %150

100:                                              ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %23) #4
  %101 = load i32, ptr %13, align 4, !tbaa !9
  %102 = shl i32 %101, 1
  %103 = load i32, ptr %14, align 4, !tbaa !9
  %104 = sub nsw i32 %102, %103
  store i32 %104, ptr %23, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %24) #4
  %105 = load i32, ptr %13, align 4, !tbaa !9
  %106 = shl i32 %105, 1
  store i32 %106, ptr %24, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %25) #4
  %107 = load i32, ptr %13, align 4, !tbaa !9
  %108 = load i32, ptr %14, align 4, !tbaa !9
  %109 = sub nsw i32 %107, %108
  %110 = shl i32 %109, 1
  store i32 %110, ptr %25, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %26) #4
  %111 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %111, ptr %26, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %27) #4
  %112 = load i32, ptr %9, align 4, !tbaa !9
  %113 = load i32, ptr %16, align 4, !tbaa !9
  %114 = add nsw i32 %112, %113
  store i32 %114, ptr %27, align 4, !tbaa !9
  %115 = load ptr, ptr %7, align 8, !tbaa !5
  %116 = load i32, ptr %8, align 4, !tbaa !9
  %117 = load i32, ptr %9, align 4, !tbaa !9
  %118 = load i32, ptr %12, align 4, !tbaa !9
  call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %115, i32 noundef %116, i32 noundef %117, i32 noundef %118)
  call void @llvm.lifetime.start.p0(i64 4, ptr %28) #4
  store i32 1, ptr %28, align 4, !tbaa !9
  br label %119

119:                                              ; preds = %143, %100
  %120 = load i32, ptr %28, align 4, !tbaa !9
  %121 = load i32, ptr %14, align 4, !tbaa !9
  %122 = icmp sle i32 %120, %121
  br i1 %122, label %124, label %123

123:                                              ; preds = %119
  call void @llvm.lifetime.end.p0(i64 4, ptr %28) #4
  br label %149

124:                                              ; preds = %119
  %125 = load i32, ptr %23, align 4, !tbaa !9
  %126 = icmp sgt i32 %125, 0
  br i1 %126, label %127, label %134

127:                                              ; preds = %124
  %128 = load i32, ptr %25, align 4, !tbaa !9
  %129 = load i32, ptr %23, align 4, !tbaa !9
  %130 = add nsw i32 %129, %128
  store i32 %130, ptr %23, align 4, !tbaa !9
  %131 = load i32, ptr %15, align 4, !tbaa !9
  %132 = load i32, ptr %26, align 4, !tbaa !9
  %133 = add nsw i32 %132, %131
  store i32 %133, ptr %26, align 4, !tbaa !9
  br label %138

134:                                              ; preds = %124
  %135 = load i32, ptr %24, align 4, !tbaa !9
  %136 = load i32, ptr %23, align 4, !tbaa !9
  %137 = add nsw i32 %136, %135
  store i32 %137, ptr %23, align 4, !tbaa !9
  br label %138

138:                                              ; preds = %134, %127
  %139 = load ptr, ptr %7, align 8, !tbaa !5
  %140 = load i32, ptr %26, align 4, !tbaa !9
  %141 = load i32, ptr %27, align 4, !tbaa !9
  %142 = load i32, ptr %12, align 4, !tbaa !9
  call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %139, i32 noundef %140, i32 noundef %141, i32 noundef %142)
  br label %143

143:                                              ; preds = %138
  %144 = load i32, ptr %28, align 4, !tbaa !9
  %145 = add nsw i32 %144, 1
  store i32 %145, ptr %28, align 4, !tbaa !9
  %146 = load i32, ptr %16, align 4, !tbaa !9
  %147 = load i32, ptr %27, align 4, !tbaa !9
  %148 = add nsw i32 %147, %146
  store i32 %148, ptr %27, align 4, !tbaa !9
  br label %119, !llvm.loop !13

149:                                              ; preds = %123
  call void @llvm.lifetime.end.p0(i64 4, ptr %27) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %26) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %25) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %24) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %23) #4
  br label %150

150:                                              ; preds = %149, %99
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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"}
!13 = distinct !{!13, !12}


; 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 @_Z8drawLineR8TgaImageiiiij(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 = tail call i32 @llvm.abs.i32(i32 %7, i1 true)
  %9 = sub nsw i32 %4, %2
  %10 = tail call i32 @llvm.abs.i32(i32 %9, i1 true)
  %.not = icmp slt i32 %3, %1
  %11 = select i1 %.not, i32 -1, i32 1
  %.not79 = icmp slt i32 %4, %2
  %12 = select i1 %.not79, i32 -1, i32 1
  %.not80 = icmp ugt i32 %10, %8
  br i1 %.not80, label %23, label %13

13:                                               ; preds = %6
  %14 = shl nuw i32 %10, 1
  %15 = sub nsw i32 %10, %8
  %16 = shl i32 %15, 1
  tail call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2, i32 noundef %5)
  %.not8285 = icmp eq i32 %3, %1
  br i1 %.not8285, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %13
  %17 = add nsw i32 %11, %1
  %18 = sub nsw i32 %14, %8
  %umax = tail call i32 @llvm.umax.i32(i32 %8, i32 1)
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.06789 = phi i32 [ %.168, %.lr.ph ], [ %18, %.lr.ph.preheader ]
  %.06988 = phi i32 [ %22, %.lr.ph ], [ %17, %.lr.ph.preheader ]
  %.07287 = phi i32 [ %21, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %.07386 = phi i32 [ %.174, %.lr.ph ], [ %2, %.lr.ph.preheader ]
  %19 = icmp sgt i32 %.06789, 0
  %20 = select i1 %19, i32 %12, i32 0
  %.174 = add nsw i32 %20, %.07386
  %.pn83 = select i1 %19, i32 %16, i32 %14
  %.168 = add nsw i32 %.pn83, %.06789
  tail call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %.06988, i32 noundef %.174, i32 noundef %5)
  %21 = add nuw i32 %.07287, 1
  %22 = add nsw i32 %.06988, %11
  %exitcond.not = icmp eq i32 %.07287, %umax
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !5

23:                                               ; preds = %6
  %24 = shl nuw i32 %8, 1
  %25 = sub nsw i32 %24, %10
  %26 = sub nsw i32 %8, %10
  %27 = shl i32 %26, 1
  %28 = add nsw i32 %12, %2
  tail call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2, i32 noundef %5)
  br label %29

29:                                               ; preds = %23, %29
  %.093 = phi i32 [ 1, %23 ], [ %32, %29 ]
  %.06592 = phi i32 [ %28, %23 ], [ %33, %29 ]
  %.06691 = phi i32 [ %1, %23 ], [ %.1, %29 ]
  %.07090 = phi i32 [ %25, %23 ], [ %.171, %29 ]
  %30 = icmp sgt i32 %.07090, 0
  %.pn = select i1 %30, i32 %27, i32 %24
  %31 = select i1 %30, i32 %11, i32 0
  %.1 = add nsw i32 %31, %.06691
  %.171 = add nsw i32 %.pn, %.07090
  tail call void @_ZN8TgaImage8putPixelEiij(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %.1, i32 noundef %.06592, i32 noundef %5)
  %32 = add nuw i32 %.093, 1
  %33 = add nsw i32 %.06592, %12
  %exitcond95.not = icmp eq i32 %.093, %10
  br i1 %exitcond95.not, label %.loopexit, label %29, !llvm.loop !7

.loopexit:                                        ; preds = %.lr.ph, %29, %13
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

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

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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"}
!7 = distinct !{!7, !6}


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