; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0156 Easy - Simple Decoder/solutions (solution)

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

@text = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  br label %2

2:                                                ; preds = %13, %12, %0
  %3 = load ptr, ptr @text, align 8, !tbaa !5
  %4 = load i8, ptr %3, align 1, !tbaa !9
  %5 = icmp ne i8 %4, 0
  br i1 %5, label %6, label %21

6:                                                ; preds = %2
  %7 = load ptr, ptr @text, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i32 1
  store ptr %8, ptr @text, align 8, !tbaa !5
  %9 = load i8, ptr %7, align 1, !tbaa !9
  %10 = sext i8 %9 to i32
  %11 = icmp eq i32 %10, 10
  br i1 %11, label %12, label %13

12:                                               ; preds = %6
  br label %2, !llvm.loop !10

13:                                               ; preds = %6
  %14 = load ptr, ptr @text, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i32 1
  store ptr %15, ptr @text, align 8, !tbaa !5
  %16 = load i8, ptr %14, align 1, !tbaa !9
  %17 = sext i8 %16 to i32
  %18 = sub nsw i32 %17, 4
  %19 = call i32 @putchar(i32 noundef %18)
  %20 = call i32 @putchar(i32 noundef 8)
  br label %2, !llvm.loop !10

21:                                               ; preds = %2
  %22 = call i32 @putchar(i32 noundef 32)
  %23 = call i32 @putchar(i32 noundef 8)
  br label %24

24:                                               ; preds = %29, %21
  %25 = load ptr, ptr @text, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i32 1
  store ptr %26, ptr @text, align 8, !tbaa !5
  %27 = load i8, ptr %26, align 1, !tbaa !9
  %28 = icmp ne i8 %27, 0
  br i1 %28, label %29, label %35

29:                                               ; preds = %24
  %30 = load ptr, ptr @text, align 8, !tbaa !5
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = sext i8 %31 to i32
  %33 = sub nsw i32 %32, 4
  %34 = call i32 @putchar(i32 noundef %33)
  br label %24, !llvm.loop !12

35:                                               ; preds = %24
  %36 = load i32, ptr %1, align 4
  ret i32 %36
}

; Function Attrs: inlinehint nounwind uwtable
declare i32 @putchar(i32 noundef) #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 = { inlinehint 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 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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

@text = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = load ptr, ptr @text, align 8, !tbaa !5
  %2 = load i8, ptr %1, align 1, !tbaa !9
  %.not2 = icmp eq i8 %2, 0
  br i1 %.not2, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %.backedge
  %3 = phi ptr [ %14, %.backedge ], [ %1, %0 ]
  %4 = getelementptr inbounds i8, ptr %3, i64 1
  store ptr %4, ptr @text, align 8, !tbaa !5
  %5 = load i8, ptr %3, align 1, !tbaa !9
  %6 = icmp eq i8 %5, 10
  br i1 %6, label %.backedge, label %7

7:                                                ; preds = %.lr.ph
  %8 = getelementptr inbounds i8, ptr %3, i64 2
  store ptr %8, ptr @text, align 8, !tbaa !5
  %9 = load i8, ptr %4, align 1, !tbaa !9
  %10 = sext i8 %9 to i32
  %11 = add nsw i32 %10, -4
  %12 = tail call i32 @putchar(i32 noundef %11)
  %13 = tail call i32 @putchar(i32 noundef 8)
  %.pre = load ptr, ptr @text, align 8, !tbaa !5
  br label %.backedge

.backedge:                                        ; preds = %7, %.lr.ph
  %14 = phi ptr [ %.pre, %7 ], [ %4, %.lr.ph ]
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %.not = icmp eq i8 %15, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.backedge, %0
  %16 = tail call i32 @putchar(i32 noundef 32)
  %17 = tail call i32 @putchar(i32 noundef 8)
  %18 = load ptr, ptr @text, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 1
  store ptr %19, ptr @text, align 8, !tbaa !5
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %.not13 = icmp eq i8 %20, 0
  br i1 %.not13, label %._crit_edge6, label %.lr.ph5

.lr.ph5:                                          ; preds = %._crit_edge, %.lr.ph5
  %21 = phi i8 [ %27, %.lr.ph5 ], [ %20, %._crit_edge ]
  %22 = sext i8 %21 to i32
  %23 = add nsw i32 %22, -4
  %24 = tail call i32 @putchar(i32 noundef %23)
  %25 = load ptr, ptr @text, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 1
  store ptr %26, ptr @text, align 8, !tbaa !5
  %27 = load i8, ptr %26, align 1, !tbaa !9
  %.not1 = icmp eq i8 %27, 0
  br i1 %.not1, label %._crit_edge6, label %.lr.ph5, !llvm.loop !12

._crit_edge6:                                     ; preds = %.lr.ph5, %._crit_edge
  ret i32 0
}

; Function Attrs: inlinehint nofree nounwind uwtable
declare noundef i32 @putchar(i32 noundef) local_unnamed_addr #1

attributes #0 = { nofree 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 = { inlinehint nofree 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 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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