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

; 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_stream_parser = type { [16 x %struct.lwjson_stream_stack_t], i64, i32, ptr, ptr, %union.anon.0, i8 }
%struct.lwjson_stream_stack_t = type { i32, %union.anon }
%union.anon = type { i16, [32 x i8] }
%union.anon.0 = type { %struct.anon }
%struct.anon = type { [257 x i8], i64, i64, i8 }
%struct.example_data_struct_t = type { [10 x i8], ptr, i32, i32 }

@.str = external hidden unnamed_addr constant [21 x i8], align 1
@stream_parser = external hidden global %struct.lwjson_stream_parser, align 8
@json_str = external hidden global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [20 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @example_stream_run() #0 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.example_data_struct_t, align 8
  %3 = alloca [10 x i8], align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %7 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  call void @llvm.lifetime.start.p0(i64 32, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 10, ptr %3) #3
  %8 = getelementptr inbounds [10 x i8], ptr %3, i64 0, i64 0
  %9 = getelementptr inbounds %struct.example_data_struct_t, ptr %2, i32 0, i32 1
  store ptr %8, ptr %9, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.example_data_struct_t, ptr %2, i32 0, i32 2
  store i32 10, ptr %10, align 8, !tbaa !11
  %11 = getelementptr inbounds %struct.example_data_struct_t, ptr %2, i32 0, i32 3
  store i32 0, ptr %11, align 4, !tbaa !12
  %12 = call i32 @lwjson_stream_init(ptr noundef @stream_parser, ptr noundef @prv_example_callback_func)
  %13 = call i32 @lwjson_stream_set_user_data(ptr noundef @stream_parser, ptr noundef %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %14 = load ptr, ptr @json_str, align 8, !tbaa !13
  store ptr %14, ptr %4, align 8, !tbaa !13
  br label %15

15:                                               ; preds = %43, %0
  %16 = load ptr, ptr %4, align 8, !tbaa !13
  %17 = load i8, ptr %16, align 1, !tbaa !14
  %18 = sext i8 %17 to i32
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %21, label %20

20:                                               ; preds = %15
  store i32 2, ptr %5, align 4
  br label %46

21:                                               ; preds = %15
  %22 = load ptr, ptr %4, align 8, !tbaa !13
  %23 = load i8, ptr %22, align 1, !tbaa !14
  %24 = call i32 @lwjson_stream_parse(ptr noundef @stream_parser, i8 noundef signext %23)
  store i32 %24, ptr %1, align 4, !tbaa !15
  %25 = load i32, ptr %1, align 4, !tbaa !15
  %26 = icmp eq i32 %25, 7
  br i1 %26, label %27, label %28

27:                                               ; preds = %21
  br label %42

28:                                               ; preds = %21
  %29 = load i32, ptr %1, align 4, !tbaa !15
  %30 = icmp eq i32 %29, 5
  br i1 %30, label %31, label %33

31:                                               ; preds = %28
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %41

33:                                               ; preds = %28
  %34 = load i32, ptr %1, align 4, !tbaa !15
  %35 = icmp eq i32 %34, 6
  br i1 %35, label %36, label %38

36:                                               ; preds = %33
  %37 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %40

38:                                               ; preds = %33
  %39 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  store i32 2, ptr %5, align 4
  br label %46

40:                                               ; preds = %36
  br label %41

41:                                               ; preds = %40, %31
  br label %42

42:                                               ; preds = %41, %27
  br label %43

43:                                               ; preds = %42
  %44 = load ptr, ptr %4, align 8, !tbaa !13
  %45 = getelementptr inbounds i8, ptr %44, i32 1
  store ptr %45, ptr %4, align 8, !tbaa !13
  br label %15, !llvm.loop !16

46:                                               ; preds = %38, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %47

47:                                               ; preds = %46
  %48 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  %49 = getelementptr inbounds %struct.example_data_struct_t, ptr %2, i32 0, i32 0
  %50 = getelementptr inbounds [10 x i8], ptr %49, i64 0, i64 0
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, ptr noundef %50)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !15
  br label %52

52:                                               ; preds = %68, %47
  %53 = load i32, ptr %6, align 4, !tbaa !15
  %54 = getelementptr inbounds %struct.example_data_struct_t, ptr %2, i32 0, i32 3
  %55 = load i32, ptr %54, align 4, !tbaa !12
  %56 = icmp slt i32 %53, %55
  br i1 %56, label %58, label %57

57:                                               ; preds = %52
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %71

58:                                               ; preds = %52
  %59 = load i32, ptr %6, align 4, !tbaa !15
  %60 = getelementptr inbounds %struct.example_data_struct_t, ptr %2, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  %62 = load i32, ptr %6, align 4, !tbaa !15
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i8, ptr %61, i64 %63
  %65 = load i8, ptr %64, align 1, !tbaa !14
  %66 = sext i8 %65 to i32
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i32 noundef %59, i32 noundef %66)
  br label %68

68:                                               ; preds = %58
  %69 = load i32, ptr %6, align 4, !tbaa !15
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %6, align 4, !tbaa !15
  br label %52, !llvm.loop !18

71:                                               ; preds = %57
  call void @llvm.lifetime.end.p0(i64 10, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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

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

declare i32 @lwjson_stream_init(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @prv_example_callback_func(ptr noundef, i32 noundef) #0

declare i32 @lwjson_stream_set_user_data(ptr noundef, ptr noundef) #2

declare i32 @lwjson_stream_parse(ptr noundef, i8 noundef signext) #2

; 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 = { "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, !9, i64 16}
!6 = !{!"example_data_struct_t", !7, i64 0, !9, i64 16, !10, i64 24, !10, i64 28}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!6, !10, i64 24}
!12 = !{!6, !10, i64 28}
!13 = !{!9, !9, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!10, !10, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


; 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_stream_parser = type { [16 x %struct.lwjson_stream_stack_t], i64, i32, ptr, ptr, %union.anon.0, i8 }
%struct.lwjson_stream_stack_t = type { i32, %union.anon }
%union.anon = type { i16, [32 x i8] }
%union.anon.0 = type { %struct.anon }
%struct.anon = type { [257 x i8], i64, i64, i8 }
%struct.example_data_struct_t = type { [10 x i8], ptr, i32, i32 }

@.str = external hidden unnamed_addr constant [21 x i8], align 1
@stream_parser = external hidden global %struct.lwjson_stream_parser, align 8
@json_str = external hidden local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [20 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @example_stream_run() local_unnamed_addr #0 {
  %1 = alloca %struct.example_data_struct_t, align 8
  %2 = alloca [10 x i8], align 1
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 10, ptr nonnull %2) #4
  %4 = getelementptr inbounds i8, ptr %1, i64 16
  store ptr %2, ptr %4, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %1, i64 24
  store i32 10, ptr %5, align 8, !tbaa !11
  %6 = getelementptr inbounds i8, ptr %1, i64 28
  store i32 0, ptr %6, align 4, !tbaa !12
  %7 = call i32 @lwjson_stream_init(ptr noundef nonnull @stream_parser, ptr noundef nonnull @prv_example_callback_func) #4
  %8 = call i32 @lwjson_stream_set_user_data(ptr noundef nonnull @stream_parser, ptr noundef nonnull %1) #4
  %9 = load ptr, ptr @json_str, align 8, !tbaa !13
  %10 = load i8, ptr %9, align 1, !tbaa !14
  %.not11 = icmp eq i8 %10, 0
  br i1 %.not11, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %0, %17
  %11 = phi i8 [ %19, %17 ], [ %10, %0 ]
  %.0912 = phi ptr [ %18, %17 ], [ %9, %0 ]
  %12 = call i32 @lwjson_stream_parse(ptr noundef nonnull @stream_parser, i8 noundef signext %11) #4
  switch i32 %12, label %14 [
    i32 7, label %17
    i32 5, label %.sink.split
    i32 6, label %13
  ]

13:                                               ; preds = %.lr.ph
  br label %.sink.split

14:                                               ; preds = %.lr.ph
  %15 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  br label %.loopexit

.sink.split:                                      ; preds = %.lr.ph, %13
  %.str.2.sink = phi ptr [ @.str.2, %13 ], [ @.str.1, %.lr.ph ]
  %16 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str.2.sink)
  br label %17

17:                                               ; preds = %.sink.split, %.lr.ph
  %18 = getelementptr inbounds i8, ptr %.0912, i64 1
  %19 = load i8, ptr %18, align 1, !tbaa !14
  %.not = icmp eq i8 %19, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !15

.loopexit:                                        ; preds = %17, %0, %14
  %20 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  %21 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, ptr noundef nonnull %1)
  %22 = load i32, ptr %6, align 4, !tbaa !12
  %23 = icmp sgt i32 %22, 0
  br i1 %23, label %.lr.ph14, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph14, %.loopexit
  call void @llvm.lifetime.end.p0(i64 10, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %1) #4
  ret void

.lr.ph14:                                         ; preds = %.loopexit, %.lr.ph14
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph14 ], [ 0, %.loopexit ]
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 %indvars.iv
  %26 = load i8, ptr %25, align 1, !tbaa !14
  %27 = sext i8 %26 to i32
  %28 = trunc nuw nsw i64 %indvars.iv to i32
  %29 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, i32 noundef %28, i32 noundef %27)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %30 = load i32, ptr %6, align 4, !tbaa !12
  %31 = sext i32 %30 to i64
  %32 = icmp slt i64 %indvars.iv.next, %31
  br i1 %32, label %.lr.ph14, label %._crit_edge, !llvm.loop !17
}

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

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

declare i32 @lwjson_stream_init(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden void @prv_example_callback_func(ptr noundef, i32 noundef) #0

declare i32 @lwjson_stream_set_user_data(ptr noundef, ptr noundef) local_unnamed_addr #3

declare i32 @lwjson_stream_parse(ptr noundef, i8 noundef signext) local_unnamed_addr #3

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 #4 = { 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, !9, i64 16}
!6 = !{!"example_data_struct_t", !7, i64 0, !9, i64 16, !10, i64 24, !10, i64 28}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!6, !10, i64 24}
!12 = !{!6, !10, i64 28}
!13 = !{!9, !9, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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