; LLVM IR from freakout42/macrocalc :: mc/src (ibm2lics)

; 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.lics = type { [2 x i8], [2 x i8], ptr, i8, i8 }

@tab = external global [128 x ptr], align 16
@lics = external global [0 x %struct.lics], align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @ibm2lics(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %13, %1
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 128
  br i1 %8, label %9, label %16

9:                                                ; preds = %6
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [128 x ptr], ptr @tab, i64 0, i64 %11
  store ptr null, ptr %12, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %9
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %4, align 4, !tbaa !5
  br label %6, !llvm.loop !11

16:                                               ; preds = %6
  store ptr @lics, ptr %5, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %70, %16
  %18 = load ptr, ptr %5, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.lics, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %73

22:                                               ; preds = %17
  %23 = load i32, ptr %3, align 4, !tbaa !5
  switch i32 %23, label %69 [
    i32 7, label %24
    i32 8, label %41
  ]

24:                                               ; preds = %22
  %25 = load ptr, ptr %5, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.lics, ptr %25, i32 0, i32 4
  %27 = load i8, ptr %26, align 1, !tbaa !15
  %28 = icmp ne i8 %27, 0
  br i1 %28, label %29, label %40

29:                                               ; preds = %24
  %30 = load ptr, ptr %5, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.lics, ptr %30, i32 0, i32 2
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.lics, ptr %33, i32 0, i32 4
  %35 = load i8, ptr %34, align 1, !tbaa !15
  %36 = zext i8 %35 to i32
  %37 = sub nsw i32 %36, 128
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [128 x ptr], ptr @tab, i64 0, i64 %38
  store ptr %32, ptr %39, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %29, %24
  br label %69

41:                                               ; preds = %22
  %42 = load ptr, ptr %5, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.lics, ptr %42, i32 0, i32 4
  %44 = load i8, ptr %43, align 1, !tbaa !15
  %45 = icmp ne i8 %44, 0
  br i1 %45, label %46, label %68

46:                                               ; preds = %41
  %47 = load ptr, ptr %5, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.lics, ptr %47, i32 0, i32 3
  %49 = load i8, ptr %48, align 8, !tbaa !16
  %50 = zext i8 %49 to i32
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %55

52:                                               ; preds = %46
  %53 = load ptr, ptr %5, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.lics, ptr %53, i32 0, i32 3
  br label %59

55:                                               ; preds = %46
  %56 = load ptr, ptr %5, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.lics, ptr %56, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !13
  br label %59

59:                                               ; preds = %55, %52
  %60 = phi ptr [ %54, %52 ], [ %58, %55 ]
  %61 = load ptr, ptr %5, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.lics, ptr %61, i32 0, i32 4
  %63 = load i8, ptr %62, align 1, !tbaa !15
  %64 = zext i8 %63 to i32
  %65 = sub nsw i32 %64, 128
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [128 x ptr], ptr @tab, i64 0, i64 %66
  store ptr %60, ptr %67, align 8, !tbaa !9
  br label %68

68:                                               ; preds = %59, %41
  br label %69

69:                                               ; preds = %68, %40, %22
  br label %70

70:                                               ; preds = %69
  %71 = load ptr, ptr %5, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.lics, ptr %71, i32 1
  store ptr %72, ptr %5, align 8, !tbaa !9
  br label %17, !llvm.loop !17

73:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %74 = load i32, ptr %2, align 4
  ret i32 %74
}

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !10, i64 8}
!14 = !{!"lics", !7, i64 0, !7, i64 2, !10, i64 8, !7, i64 16, !7, i64 17}
!15 = !{!14, !7, i64 17}
!16 = !{!14, !7, i64 16}
!17 = distinct !{!17, !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"

%struct.lics = type { [2 x i8], [2 x i8], ptr, i8, i8 }

@tab = external local_unnamed_addr global [128 x ptr], align 16
@lics = external global [0 x %struct.lics], align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local i32 @ibm2lics(i32 noundef %0) local_unnamed_addr #0 {
.preheader:
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1024) @tab, i8 0, i64 1024, i1 false), !tbaa !5
  %1 = load ptr, ptr getelementptr inbounds (i8, ptr @lics, i64 8), align 8, !tbaa !9
  %.not21 = icmp eq ptr %1, null
  br i1 %.not21, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  switch i32 %0, label %._crit_edge [
    i32 7, label %.lr.ph.split.us
    i32 8, label %.lr.ph.split.us23
  ]

.lr.ph.split.us:                                  ; preds = %.lr.ph, %9
  %2 = phi ptr [ %12, %9 ], [ %1, %.lr.ph ]
  %.022.us = phi ptr [ %10, %9 ], [ @lics, %.lr.ph ]
  %3 = getelementptr inbounds i8, ptr %.022.us, i64 17
  %4 = load i8, ptr %3, align 1, !tbaa !11
  %.not19.us = icmp eq i8 %4, 0
  br i1 %.not19.us, label %9, label %5

5:                                                ; preds = %.lr.ph.split.us
  %6 = zext i8 %4 to i64
  %7 = add nsw i64 %6, -128
  %8 = getelementptr inbounds [128 x ptr], ptr @tab, i64 0, i64 %7
  store ptr %2, ptr %8, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %5, %.lr.ph.split.us
  %10 = getelementptr inbounds i8, ptr %.022.us, i64 24
  %11 = getelementptr inbounds i8, ptr %.022.us, i64 32
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %.not.us = icmp eq ptr %12, null
  br i1 %.not.us, label %._crit_edge, label %.lr.ph.split.us, !llvm.loop !12

.lr.ph.split.us23:                                ; preds = %.lr.ph, %22
  %13 = phi ptr [ %25, %22 ], [ %1, %.lr.ph ]
  %.022.us24 = phi ptr [ %23, %22 ], [ @lics, %.lr.ph ]
  %14 = getelementptr inbounds i8, ptr %.022.us24, i64 17
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %.not17.us = icmp eq i8 %15, 0
  br i1 %.not17.us, label %22, label %16

16:                                               ; preds = %.lr.ph.split.us23
  %17 = getelementptr inbounds i8, ptr %.022.us24, i64 16
  %18 = load i8, ptr %17, align 8, !tbaa !14
  %.not18.us = icmp eq i8 %18, 0
  %..us = select i1 %.not18.us, ptr %13, ptr %17
  %19 = zext i8 %15 to i64
  %20 = add nsw i64 %19, -128
  %21 = getelementptr inbounds [128 x ptr], ptr @tab, i64 0, i64 %20
  store ptr %..us, ptr %21, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %16, %.lr.ph.split.us23
  %23 = getelementptr inbounds i8, ptr %.022.us24, i64 24
  %24 = getelementptr inbounds i8, ptr %.022.us24, i64 32
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %.not.us25 = icmp eq ptr %25, null
  br i1 %.not.us25, label %._crit_edge, label %.lr.ph.split.us23, !llvm.loop !12

._crit_edge:                                      ; preds = %22, %9, %.lr.ph, %.preheader
  ret i32 undef
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

!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, !6, i64 8}
!10 = !{!"lics", !7, i64 0, !7, i64 2, !6, i64 8, !7, i64 16, !7, i64 17}
!11 = !{!10, !7, i64 17}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!10, !7, i64 16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass