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

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

@.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

; Function Attrs: nounwind uwtable
define dso_local void @example_stream_run() #0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %4 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %5 = call i32 @lwjson_stream_init(ptr noundef @stream_parser, ptr noundef @prv_example_callback_func)
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %6 = load ptr, ptr @json_str, align 8, !tbaa !5
  store ptr %6, ptr %2, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %35, %0
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = load i8, ptr %8, align 1, !tbaa !9
  %10 = sext i8 %9 to i32
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %13, label %12

12:                                               ; preds = %7
  store i32 2, ptr %3, align 4
  br label %38

13:                                               ; preds = %7
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %16 = call i32 @lwjson_stream_parse(ptr noundef @stream_parser, i8 noundef signext %15)
  store i32 %16, ptr %1, align 4, !tbaa !10
  %17 = load i32, ptr %1, align 4, !tbaa !10
  %18 = icmp eq i32 %17, 7
  br i1 %18, label %19, label %20

19:                                               ; preds = %13
  br label %34

20:                                               ; preds = %13
  %21 = load i32, ptr %1, align 4, !tbaa !10
  %22 = icmp eq i32 %21, 5
  br i1 %22, label %23, label %25

23:                                               ; preds = %20
  %24 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %33

25:                                               ; preds = %20
  %26 = load i32, ptr %1, align 4, !tbaa !10
  %27 = icmp eq i32 %26, 6
  br i1 %27, label %28, label %30

28:                                               ; preds = %25
  %29 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %32

30:                                               ; preds = %25
  %31 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  store i32 2, ptr %3, align 4
  br label %38

32:                                               ; preds = %28
  br label %33

33:                                               ; preds = %32, %23
  br label %34

34:                                               ; preds = %33, %19
  br label %35

35:                                               ; preds = %34
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i32 1
  store ptr %37, ptr %2, align 8, !tbaa !5
  br label %7, !llvm.loop !12

38:                                               ; preds = %30, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  br label %39

39:                                               ; preds = %38
  %40 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  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_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, !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 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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_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 }

@.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

; Function Attrs: nounwind uwtable
define dso_local void @example_stream_run() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %2 = tail call i32 @lwjson_stream_init(ptr noundef nonnull @stream_parser, ptr noundef nonnull @prv_example_callback_func) #3
  %3 = load ptr, ptr @json_str, align 8, !tbaa !5
  %4 = load i8, ptr %3, align 1, !tbaa !9
  %.not6 = icmp eq i8 %4, 0
  br i1 %.not6, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %0, %11
  %5 = phi i8 [ %13, %11 ], [ %4, %0 ]
  %.07 = phi ptr [ %12, %11 ], [ %3, %0 ]
  %6 = tail call i32 @lwjson_stream_parse(ptr noundef nonnull @stream_parser, i8 noundef signext %5) #3
  switch i32 %6, label %8 [
    i32 7, label %11
    i32 5, label %.sink.split
    i32 6, label %7
  ]

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

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

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

11:                                               ; preds = %.sink.split, %.lr.ph
  %12 = getelementptr inbounds i8, ptr %.07, i64 1
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %.not = icmp eq i8 %13, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !10

.loopexit:                                        ; preds = %11, %0, %8
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  ret void
}

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

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

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

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

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 = { 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 #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, !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"}


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