; LLVM IR from MaJerle/lwjson :: examples (example_traverse)

; 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.lwjson_t = type { ptr, i64, i64, %struct.lwjson_token, %struct.anon.0 }
%struct.lwjson_token = type { ptr, i32, ptr, i64, %union.anon }
%union.anon = type { %struct.anon }
%struct.anon = type { ptr, i64 }
%struct.anon.0 = type { i8 }

@lwjson = external hidden global %struct.lwjson_t, align 8
@tokens = external hidden global [128 x %struct.lwjson_token], align 16
@.str = external hidden unnamed_addr constant [53 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [79 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @example_traverse_run() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = call i32 @lwjson_init(ptr noundef @lwjson, ptr noundef @tokens, i64 noundef 128)
  %4 = call i32 @lwjson_parse(ptr noundef @lwjson, ptr noundef @.str)
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %76

6:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %7 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  store ptr getelementptr inbounds (%struct.lwjson_t, ptr @lwjson, i32 0, i32 3), ptr %1, align 8, !tbaa !5
  %8 = load ptr, ptr %1, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.lwjson_token, ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !9
  %11 = icmp eq i32 %10, 4
  br i1 %11, label %12, label %14

12:                                               ; preds = %6
  %13 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %24

14:                                               ; preds = %6
  %15 = load ptr, ptr %1, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.lwjson_token, ptr %15, i32 0, i32 1
  %17 = load i32, ptr %16, align 8, !tbaa !9
  %18 = icmp eq i32 %17, 3
  br i1 %18, label %19, label %21

19:                                               ; preds = %14
  %20 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  br label %23

21:                                               ; preds = %14
  %22 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  br label %23

23:                                               ; preds = %21, %19
  br label %24

24:                                               ; preds = %23, %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %25 = load ptr, ptr %1, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %41

27:                                               ; preds = %24
  %28 = load ptr, ptr %1, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.lwjson_token, ptr %28, i32 0, i32 1
  %30 = load i32, ptr %29, align 8, !tbaa !9
  %31 = icmp eq i32 %30, 3
  br i1 %31, label %37, label %32

32:                                               ; preds = %27
  %33 = load ptr, ptr %1, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.lwjson_token, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 8, !tbaa !9
  %36 = icmp eq i32 %35, 4
  br i1 %36, label %37, label %41

37:                                               ; preds = %32, %27
  %38 = load ptr, ptr %1, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.lwjson_token, ptr %38, i32 0, i32 4
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  br label %42

41:                                               ; preds = %32, %24
  br label %42

42:                                               ; preds = %41, %37
  %43 = phi ptr [ %40, %37 ], [ null, %41 ]
  store ptr %43, ptr %2, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %70, %42
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = icmp ne ptr %45, null
  br i1 %46, label %48, label %47

47:                                               ; preds = %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  br label %74

48:                                               ; preds = %44
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.lwjson_token, ptr %49, i32 0, i32 3
  %51 = load i64, ptr %50, align 8, !tbaa !14
  %52 = trunc i64 %51 to i32
  %53 = load ptr, ptr %2, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.lwjson_token, ptr %53, i32 0, i32 2
  %55 = load ptr, ptr %54, align 8, !tbaa !15
  %56 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, i32 noundef %52, ptr noundef %55)
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.lwjson_token, ptr %57, i32 0, i32 1
  %59 = load i32, ptr %58, align 8, !tbaa !9
  %60 = icmp eq i32 %59, 4
  br i1 %60, label %66, label %61

61:                                               ; preds = %48
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.lwjson_token, ptr %62, i32 0, i32 1
  %64 = load i32, ptr %63, align 8, !tbaa !9
  %65 = icmp eq i32 %64, 3
  br i1 %65, label %66, label %68

66:                                               ; preds = %61, %48
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.6)
  br label %68

68:                                               ; preds = %66, %61
  %69 = call i32 (ptr, ...) @printf(ptr noundef @.str.7)
  br label %70

70:                                               ; preds = %68
  %71 = load ptr, ptr %2, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.lwjson_token, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !16
  store ptr %73, ptr %2, align 8, !tbaa !5
  br label %44, !llvm.loop !17

74:                                               ; preds = %47
  %75 = call i32 @lwjson_free(ptr noundef @lwjson)
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  br label %76

76:                                               ; preds = %74, %0
  ret void
}

declare i32 @lwjson_init(ptr noundef, ptr noundef, i64 noundef) #1

declare i32 @lwjson_parse(ptr noundef, ptr noundef) #1

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

declare i32 @printf(ptr noundef, ...) #1

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

declare i32 @lwjson_free(ptr 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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 8}
!10 = !{!"lwjson_token", !6, i64 0, !11, i64 8, !6, i64 16, !12, i64 24, !7, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!10, !12, i64 24}
!15 = !{!10, !6, i64 16}
!16 = !{!10, !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.lwjson_t = type { ptr, i64, i64, %struct.lwjson_token, %struct.anon.0 }
%struct.lwjson_token = type { ptr, i32, ptr, i64, %union.anon }
%union.anon = type { %struct.anon }
%struct.anon = type { ptr, i64 }
%struct.anon.0 = type { i8 }

@lwjson = external hidden global %struct.lwjson_t, align 8
@tokens = external hidden global [128 x %struct.lwjson_token], align 16
@.str = external hidden unnamed_addr constant [53 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [79 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @example_traverse_run() local_unnamed_addr #0 {
  %1 = tail call i32 @lwjson_init(ptr noundef nonnull @lwjson, ptr noundef nonnull @tokens, i64 noundef 128) #3
  %2 = tail call i32 @lwjson_parse(ptr noundef nonnull @lwjson, ptr noundef nonnull @.str) #3
  %3 = icmp eq i32 %2, 0
  br i1 %3, label %4, label %24

4:                                                ; preds = %0
  %5 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @lwjson, i64 32), align 8, !tbaa !5
  %switch.selectcmp = icmp eq i32 %6, 3
  %switch.select = select i1 %switch.selectcmp, ptr @.str.3, ptr @.str.4
  %switch.selectcmp20 = icmp eq i32 %6, 4
  %switch.select21 = select i1 %switch.selectcmp20, ptr @.str.2, ptr %switch.select
  %7 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %switch.select21)
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @lwjson, i64 32), align 8, !tbaa !5
  %9 = add i32 %8, -5
  %switch = icmp ult i32 %9, -2
  %10 = load ptr, ptr getelementptr inbounds (i8, ptr @lwjson, i64 56), align 8
  %.not1719 = icmp eq ptr %10, null
  %.not17 = select i1 %switch, i1 true, i1 %.not1719
  br i1 %.not17, label %select.unfold._crit_edge, label %.lr.ph

select.unfold._crit_edge:                         ; preds = %select.unfold, %4
  %11 = tail call i32 @lwjson_free(ptr noundef nonnull @lwjson) #3
  br label %24

.lr.ph:                                           ; preds = %4, %select.unfold
  %.018 = phi ptr [ %23, %select.unfold ], [ %10, %4 ]
  %12 = getelementptr inbounds i8, ptr %.018, i64 24
  %13 = load i64, ptr %12, align 8, !tbaa !12
  %14 = trunc i64 %13 to i32
  %15 = getelementptr inbounds i8, ptr %.018, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, i32 noundef %14, ptr noundef %16)
  %18 = getelementptr inbounds i8, ptr %.018, i64 8
  %19 = load i32, ptr %18, align 8, !tbaa !5
  %.off15 = add i32 %19, -3
  %switch16 = icmp ult i32 %.off15, 2
  br i1 %switch16, label %20, label %select.unfold

20:                                               ; preds = %.lr.ph
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6)
  br label %select.unfold

select.unfold:                                    ; preds = %.lr.ph, %20
  %22 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  %23 = load ptr, ptr %.018, align 8, !tbaa !14
  %.not = icmp eq ptr %23, null
  br i1 %.not, label %select.unfold._crit_edge, label %.lr.ph, !llvm.loop !15

24:                                               ; preds = %select.unfold._crit_edge, %0
  ret void
}

declare i32 @lwjson_init(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare i32 @lwjson_parse(ptr noundef, ptr noundef) local_unnamed_addr #1

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

declare i32 @lwjson_free(ptr noundef) local_unnamed_addr #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 = { "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 = { 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 #3 = { 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 8}
!6 = !{!"lwjson_token", !7, i64 0, !10, i64 8, !7, i64 16, !11, i64 24, !8, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !11, i64 24}
!13 = !{!6, !7, i64 16}
!14 = !{!6, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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