; LLVM IR from freddiev4/dailyprogrammerchallenges :: Intermediate Challenges/Challenge 0004 Intermediate/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"

%struct.AST_Node = type { ptr, ptr, i32, double }

@stdout = external global ptr, align 8
@.str.28 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: nounwind uwtable
define dso_local void @AST_print(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %6

5:                                                ; preds = %1
  br label %96

6:                                                ; preds = %1
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.AST_Node, ptr %7, i32 0, i32 2
  %9 = load i32, ptr %8, align 8, !tbaa !9
  switch i32 %9, label %96 [
    i32 0, label %10
    i32 1, label %16
    i32 2, label %24
    i32 3, label %33
    i32 4, label %42
    i32 5, label %51
    i32 6, label %60
    i32 7, label %69
    i32 8, label %78
    i32 9, label %87
  ]

10:                                               ; preds = %6
  %11 = load ptr, ptr @stdout, align 8, !tbaa !5
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.AST_Node, ptr %12, i32 0, i32 3
  %14 = load double, ptr %13, align 8, !tbaa !13
  %15 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %11, ptr noundef @.str.28, double noundef %14) #2
  br label %96

16:                                               ; preds = %6
  %17 = load ptr, ptr @stdout, align 8, !tbaa !5
  %18 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef @.str.29) #2
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.AST_Node, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  call void @AST_print(ptr noundef %21)
  %22 = load ptr, ptr @stdout, align 8, !tbaa !5
  %23 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef @.str.30) #2
  br label %96

24:                                               ; preds = %6
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.AST_Node, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  call void @AST_print(ptr noundef %27)
  %28 = load ptr, ptr @stdout, align 8, !tbaa !5
  %29 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %28, ptr noundef @.str.31, i32 noundef 94) #2
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.AST_Node, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !14
  call void @AST_print(ptr noundef %32)
  br label %96

33:                                               ; preds = %6
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.AST_Node, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  call void @AST_print(ptr noundef %36)
  %37 = load ptr, ptr @stdout, align 8, !tbaa !5
  %38 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %37, ptr noundef @.str.31, i32 noundef 124) #2
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.AST_Node, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  call void @AST_print(ptr noundef %41)
  br label %96

42:                                               ; preds = %6
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.AST_Node, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !15
  call void @AST_print(ptr noundef %45)
  %46 = load ptr, ptr @stdout, align 8, !tbaa !5
  %47 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %46, ptr noundef @.str.31, i32 noundef 37) #2
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.AST_Node, ptr %48, i32 0, i32 1
  %50 = load ptr, ptr %49, align 8, !tbaa !14
  call void @AST_print(ptr noundef %50)
  br label %96

51:                                               ; preds = %6
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.AST_Node, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !15
  call void @AST_print(ptr noundef %54)
  %55 = load ptr, ptr @stdout, align 8, !tbaa !5
  %56 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %55, ptr noundef @.str.31, i32 noundef 92) #2
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.AST_Node, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !14
  call void @AST_print(ptr noundef %59)
  br label %96

60:                                               ; preds = %6
  %61 = load ptr, ptr %2, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.AST_Node, ptr %61, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !15
  call void @AST_print(ptr noundef %63)
  %64 = load ptr, ptr @stdout, align 8, !tbaa !5
  %65 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %64, ptr noundef @.str.31, i32 noundef 42) #2
  %66 = load ptr, ptr %2, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.AST_Node, ptr %66, i32 0, i32 1
  %68 = load ptr, ptr %67, align 8, !tbaa !14
  call void @AST_print(ptr noundef %68)
  br label %96

69:                                               ; preds = %6
  %70 = load ptr, ptr %2, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.AST_Node, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !15
  call void @AST_print(ptr noundef %72)
  %73 = load ptr, ptr @stdout, align 8, !tbaa !5
  %74 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %73, ptr noundef @.str.31, i32 noundef 47) #2
  %75 = load ptr, ptr %2, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.AST_Node, ptr %75, i32 0, i32 1
  %77 = load ptr, ptr %76, align 8, !tbaa !14
  call void @AST_print(ptr noundef %77)
  br label %96

78:                                               ; preds = %6
  %79 = load ptr, ptr %2, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.AST_Node, ptr %79, i32 0, i32 0
  %81 = load ptr, ptr %80, align 8, !tbaa !15
  call void @AST_print(ptr noundef %81)
  %82 = load ptr, ptr @stdout, align 8, !tbaa !5
  %83 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %82, ptr noundef @.str.31, i32 noundef 43) #2
  %84 = load ptr, ptr %2, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.AST_Node, ptr %84, i32 0, i32 1
  %86 = load ptr, ptr %85, align 8, !tbaa !14
  call void @AST_print(ptr noundef %86)
  br label %96

87:                                               ; preds = %6
  %88 = load ptr, ptr %2, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.AST_Node, ptr %88, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !15
  call void @AST_print(ptr noundef %90)
  %91 = load ptr, ptr @stdout, align 8, !tbaa !5
  %92 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %91, ptr noundef @.str.31, i32 noundef 45) #2
  %93 = load ptr, ptr %2, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.AST_Node, ptr %93, i32 0, i32 1
  %95 = load ptr, ptr %94, align 8, !tbaa !14
  call void @AST_print(ptr noundef %95)
  br label %96

96:                                               ; preds = %87, %78, %69, %60, %51, %42, %33, %24, %16, %10, %6, %5
  ret void
}

attributes #0 = { nounwind "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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 16}
!10 = !{!"AST_Node", !6, i64 0, !6, i64 8, !11, i64 16, !12, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!10, !12, i64 24}
!14 = !{!10, !6, i64 8}
!15 = !{!10, !6, i64 0}


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

@stdout = external local_unnamed_addr global ptr, align 8
@.str.28 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nofree nounwind uwtable
define dso_local void @AST_print(ptr noundef readonly %0) local_unnamed_addr #1 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %common.ret34, label %.lr.ph

.lr.ph:                                           ; preds = %1, %tailrecurse.backedge
  %.tr22 = phi ptr [ %.tr.be, %tailrecurse.backedge ], [ %0, %1 ]
  %3 = getelementptr inbounds i8, ptr %.tr22, i64 16
  %4 = load i32, ptr %3, align 8, !tbaa !5
  switch i32 %4, label %common.ret34 [
    i32 0, label %5
    i32 1, label %10
    i32 2, label %17
    i32 3, label %tailrecurse.backedge
    i32 4, label %22
    i32 5, label %23
    i32 6, label %24
    i32 7, label %25
    i32 8, label %26
    i32 9, label %27
  ]

5:                                                ; preds = %.lr.ph
  %6 = load ptr, ptr @stdout, align 8, !tbaa !12
  %7 = getelementptr inbounds i8, ptr %.tr22, i64 24
  %8 = load double, ptr %7, align 8, !tbaa !13
  %9 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %6, ptr noundef nonnull @.str.28, double noundef %8) #2
  br label %common.ret34

common.ret34:                                     ; preds = %5, %1, %.lr.ph, %tailrecurse.backedge, %10
  ret void

10:                                               ; preds = %.lr.ph
  %11 = load ptr, ptr @stdout, align 8, !tbaa !12
  %12 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %11, ptr noundef nonnull @.str.29) #2
  %13 = getelementptr inbounds i8, ptr %.tr22, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  tail call void @AST_print(ptr noundef %14)
  %15 = load ptr, ptr @stdout, align 8, !tbaa !12
  %16 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str.30) #2
  br label %common.ret34

17:                                               ; preds = %.lr.ph
  br label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %.lr.ph, %17, %22, %23, %24, %25, %26, %27
  %.sink27 = phi i32 [ 94, %17 ], [ 37, %22 ], [ 92, %23 ], [ 42, %24 ], [ 47, %25 ], [ 43, %26 ], [ 45, %27 ], [ 124, %.lr.ph ]
  %18 = load ptr, ptr %.tr22, align 8, !tbaa !15
  tail call void @AST_print(ptr noundef %18)
  %19 = load ptr, ptr @stdout, align 8, !tbaa !12
  %20 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %19, ptr noundef nonnull @.str.31, i32 noundef %.sink27) #2
  %.tr.be.in = getelementptr inbounds i8, ptr %.tr22, i64 8
  %.tr.be = load ptr, ptr %.tr.be.in, align 8, !tbaa !14
  %21 = icmp eq ptr %.tr.be, null
  br i1 %21, label %common.ret34, label %.lr.ph

22:                                               ; preds = %.lr.ph
  br label %tailrecurse.backedge

23:                                               ; preds = %.lr.ph
  br label %tailrecurse.backedge

24:                                               ; preds = %.lr.ph
  br label %tailrecurse.backedge

25:                                               ; preds = %.lr.ph
  br label %tailrecurse.backedge

26:                                               ; preds = %.lr.ph
  br label %tailrecurse.backedge

27:                                               ; preds = %.lr.ph
  br label %tailrecurse.backedge
}

attributes #0 = { nofree nounwind "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 = { 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 #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, !10, i64 16}
!6 = !{!"AST_Node", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"double", !8, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!6, !11, i64 24}
!14 = !{!6, !7, i64 8}
!15 = !{!6, !7, i64 0}


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