; LLVM IR from freakout42/macrocalc :: mc/mex (random)

; 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.WINDOW = type { ptr, ptr, ptr, ptr, i32, ptr, i32, i32, i32, i32, i32, i32 }
%struct.LINE = type { ptr, ptr, i16, i16, [32768 x i8] }

@tabsize = external global i32, align 4
@curwp = external global ptr, align 8

; 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
define dso_local i32 @getccol(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %57, %1
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = load ptr, ptr @curwp, align 8, !tbaa !9
  %9 = getelementptr inbounds %struct.WINDOW, ptr %8, i32 0, i32 4
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = icmp slt i32 %7, %10
  br i1 %11, label %12, label %60

12:                                               ; preds = %6
  %13 = load ptr, ptr @curwp, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.WINDOW, ptr %13, i32 0, i32 3
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  %16 = getelementptr inbounds %struct.LINE, ptr %15, i32 0, i32 4
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [32768 x i8], ptr %16, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !14
  %21 = sext i8 %20 to i32
  %22 = and i32 %21, 255
  store i32 %22, ptr %3, align 4, !tbaa !5
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = icmp ne i32 %23, 32
  br i1 %24, label %25, label %32

25:                                               ; preds = %12
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = icmp ne i32 %26, 9
  br i1 %27, label %28, label %32

28:                                               ; preds = %25
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  br label %60

32:                                               ; preds = %28, %25, %12
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = icmp eq i32 %33, 9
  br i1 %34, label %35, label %44

35:                                               ; preds = %32
  %36 = load i32, ptr @tabsize, align 4, !tbaa !5
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr @tabsize, align 4, !tbaa !5
  %39 = srem i32 %37, %38
  %40 = sub nsw i32 %36, %39
  %41 = sub nsw i32 %40, 1
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = add nsw i32 %42, %41
  store i32 %43, ptr %5, align 4, !tbaa !5
  br label %54

44:                                               ; preds = %32
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = icmp slt i32 %45, 32
  br i1 %46, label %50, label %47

47:                                               ; preds = %44
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = icmp eq i32 %48, 127
  br i1 %49, label %50, label %53

50:                                               ; preds = %47, %44
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %5, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %50, %47
  br label %54

54:                                               ; preds = %53, %35
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %5, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %54
  %58 = load i32, ptr %4, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %4, align 4, !tbaa !5
  br label %6, !llvm.loop !15

60:                                               ; preds = %31, %6
  %61 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %61
}

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 32}
!12 = !{!"WINDOW", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !6, i64 52, !6, i64 56, !6, i64 60, !6, i64 64, !6, i64 68}
!13 = !{!12, !10, i64 24}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@tabsize = external local_unnamed_addr global i32, align 4
@curwp = external local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local range(i32 -2147483647, -2147483648) i32 @getccol(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @curwp, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 32
  %4 = load i32, ptr %3, align 8, !tbaa !9
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %switch.early.test._crit_edge

.lr.ph:                                           ; preds = %1
  %6 = getelementptr inbounds i8, ptr %2, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %7, i64 20
  %.not = icmp eq i32 %0, 0
  %9 = load i32, ptr @tabsize, align 4
  %wide.trip.count30 = zext nneg i32 %4 to i64
  br i1 %.not, label %.lr.ph.split.us, label %switch.early.test

.lr.ph.split.us:                                  ; preds = %.lr.ph, %21
  %indvars.iv27 = phi i64 [ %indvars.iv.next28, %21 ], [ 0, %.lr.ph ]
  %.022.us = phi i32 [ %22, %21 ], [ 0, %.lr.ph ]
  %10 = getelementptr inbounds [32768 x i8], ptr %8, i64 0, i64 %indvars.iv27
  %11 = load i8, ptr %10, align 1, !tbaa !13
  %12 = icmp eq i8 %11, 9
  br i1 %12, label %17, label %13

13:                                               ; preds = %.lr.ph.split.us
  %14 = icmp ult i8 %11, 32
  %15 = icmp eq i8 %11, 127
  %or.cond5.us = or i1 %14, %15
  %16 = zext i1 %or.cond5.us to i32
  br label %21

17:                                               ; preds = %.lr.ph.split.us
  %18 = srem i32 %.022.us, %9
  %19 = xor i32 %18, -1
  %20 = add i32 %9, %19
  br label %21

21:                                               ; preds = %17, %13
  %.pn.us = phi i32 [ %20, %17 ], [ %16, %13 ]
  %.1.us = add i32 %.022.us, 1
  %22 = add i32 %.1.us, %.pn.us
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %exitcond31.not = icmp eq i64 %indvars.iv.next28, %wide.trip.count30
  br i1 %exitcond31.not, label %switch.early.test._crit_edge, label %.lr.ph.split.us, !llvm.loop !14

switch.early.test:                                ; preds = %.lr.ph, %29
  %indvars.iv = phi i64 [ %indvars.iv.next, %29 ], [ 0, %.lr.ph ]
  %.022 = phi i32 [ %30, %29 ], [ 0, %.lr.ph ]
  %23 = getelementptr inbounds [32768 x i8], ptr %8, i64 0, i64 %indvars.iv
  %24 = load i8, ptr %23, align 1, !tbaa !13
  switch i8 %24, label %switch.early.test._crit_edge [
    i8 9, label %25
    i8 32, label %29
  ]

25:                                               ; preds = %switch.early.test
  %26 = srem i32 %.022, %9
  %27 = xor i32 %26, -1
  %28 = add i32 %9, %27
  br label %29

29:                                               ; preds = %switch.early.test, %25
  %.pn = phi i32 [ %28, %25 ], [ 0, %switch.early.test ]
  %.1 = add i32 %.022, 1
  %30 = add i32 %.1, %.pn
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count30
  br i1 %exitcond.not, label %switch.early.test._crit_edge, label %switch.early.test, !llvm.loop !14

switch.early.test._crit_edge:                     ; preds = %29, %switch.early.test, %21, %1
  %.0.lcssa = phi i32 [ 0, %1 ], [ %22, %21 ], [ %.022, %switch.early.test ], [ %30, %29 ]
  ret i32 %.0.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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 = !{!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 32}
!10 = !{!"WINDOW", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 24}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass