; LLVM IR from lyg09270/TinyCmd :: Demo/Arduino/ATMEGA328P (TinyCmd)

; 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.TinyCmd_inuput = type { [53 x i8], [3 x ptr], i8 }
%struct.TinyCmd_List = type { [6 x ptr], i8 }
%struct.TinyCmd_Command = type { ptr, ptr }

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@TinyCmd_buf = external dso_local global %struct.TinyCmd_inuput, align 8
@token_count = external dso_local global i8, align 1
@strtok_next = external hidden global ptr, align 8
@TinyCmdRunning_Cmd = external dso_local global %struct.TinyCmd_List, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @TinyCmd_Handler() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i8, align 1
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #2
  store i8 0, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store ptr @.str, ptr %3, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @TinyCmd_trim(ptr noundef @TinyCmd_buf)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !8
  %9 = call ptr @TinyCmd_strtok_s(ptr noundef @TinyCmd_buf, ptr noundef %8, ptr noundef %4)
  store ptr %9, ptr %5, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %10 = load ptr, ptr %5, align 8, !tbaa !8
  store ptr %10, ptr %6, align 8, !tbaa !8
  %11 = load ptr, ptr %3, align 8, !tbaa !8
  %12 = call ptr @TinyCmd_strtok_s(ptr noundef null, ptr noundef %11, ptr noundef %4)
  store ptr %12, ptr %5, align 8, !tbaa !8
  br label %13

13:                                               ; preds = %27, %0
  %14 = load ptr, ptr %5, align 8, !tbaa !8
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %30

16:                                               ; preds = %13
  %17 = load i8, ptr @token_count, align 1, !tbaa !5
  %18 = zext i8 %17 to i32
  %19 = icmp slt i32 %18, 4
  br i1 %19, label %20, label %26

20:                                               ; preds = %16
  %21 = load ptr, ptr %5, align 8, !tbaa !8
  %22 = load i8, ptr %2, align 1, !tbaa !5
  %23 = add i8 %22, 1
  store i8 %23, ptr %2, align 1, !tbaa !5
  %24 = zext i8 %22 to i64
  %25 = getelementptr inbounds [3 x ptr], ptr getelementptr inbounds (%struct.TinyCmd_inuput, ptr @TinyCmd_buf, i32 0, i32 1), i64 0, i64 %24
  store ptr %21, ptr %25, align 8, !tbaa !8
  br label %27

26:                                               ; preds = %16
  store ptr null, ptr @strtok_next, align 8, !tbaa !8
  br label %30

27:                                               ; preds = %20
  %28 = load ptr, ptr %3, align 8, !tbaa !8
  %29 = call ptr @TinyCmd_strtok_s(ptr noundef null, ptr noundef %28, ptr noundef %4)
  store ptr %29, ptr %5, align 8, !tbaa !8
  br label %13, !llvm.loop !10

30:                                               ; preds = %26, %13
  store i8 0, ptr %2, align 1, !tbaa !5
  br label %31

31:                                               ; preds = %57, %30
  %32 = load i8, ptr %2, align 1, !tbaa !5
  %33 = zext i8 %32 to i32
  %34 = load i8, ptr getelementptr inbounds (%struct.TinyCmd_List, ptr @TinyCmdRunning_Cmd, i32 0, i32 1), align 8, !tbaa !12
  %35 = zext i8 %34 to i32
  %36 = icmp slt i32 %33, %35
  br i1 %36, label %37, label %60

37:                                               ; preds = %31
  %38 = load ptr, ptr %6, align 8, !tbaa !8
  %39 = load i8, ptr %2, align 1, !tbaa !5
  %40 = zext i8 %39 to i64
  %41 = getelementptr inbounds [6 x ptr], ptr @TinyCmdRunning_Cmd, i64 0, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !8
  %43 = getelementptr inbounds %struct.TinyCmd_Command, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !14
  %45 = call i32 @TinyCmd_strcmp(ptr noundef %38, ptr noundef %44)
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %56, label %47

47:                                               ; preds = %37
  %48 = load i8, ptr %2, align 1, !tbaa !5
  %49 = zext i8 %48 to i64
  %50 = getelementptr inbounds [6 x ptr], ptr @TinyCmdRunning_Cmd, i64 0, i64 %49
  %51 = load ptr, ptr %50, align 8, !tbaa !8
  %52 = getelementptr inbounds %struct.TinyCmd_Command, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = call zeroext i8 %53()
  %55 = call i32 @TinyCmd_Buf_Clear()
  store i32 1, ptr %1, align 4
  store i32 1, ptr %7, align 4
  br label %62

56:                                               ; preds = %37
  br label %57

57:                                               ; preds = %56
  %58 = load i8, ptr %2, align 1, !tbaa !5
  %59 = add i8 %58, 1
  store i8 %59, ptr %2, align 1, !tbaa !5
  br label %31, !llvm.loop !17

60:                                               ; preds = %31
  %61 = call i32 @TinyCmd_Buf_Clear()
  store i32 0, ptr %1, align 4
  store i32 1, ptr %7, align 4
  br label %62

62:                                               ; preds = %60, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #2
  %63 = load i32, ptr %1, align 4
  ret i32 %63
}

; Function Attrs: nounwind uwtable
declare hidden void @TinyCmd_trim(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @TinyCmd_strtok_s(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @TinyCmd_strcmp(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @TinyCmd_Buf_Clear() #1

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

%struct.TinyCmd_inuput = type { [53 x i8], [3 x ptr], i8 }
%struct.TinyCmd_List = type { [6 x ptr], i8 }

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@TinyCmd_buf = external dso_local global %struct.TinyCmd_inuput, align 8
@token_count = external dso_local local_unnamed_addr global i8, align 1
@strtok_next = external hidden local_unnamed_addr global ptr, align 8
@TinyCmdRunning_Cmd = external dso_local local_unnamed_addr global %struct.TinyCmd_List, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @TinyCmd_Handler() local_unnamed_addr #1 {
  %1 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #2
  tail call void @TinyCmd_trim(ptr noundef nonnull @TinyCmd_buf)
  %2 = call ptr @TinyCmd_strtok_s(ptr noundef nonnull @TinyCmd_buf, ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %3 = call ptr @TinyCmd_strtok_s(ptr noundef null, ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %.not15 = icmp eq ptr %3, null
  br i1 %.not15, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %0, %6
  %.01217 = phi ptr [ %10, %6 ], [ %3, %0 ]
  %.01316 = phi i8 [ %7, %6 ], [ 0, %0 ]
  %4 = load i8, ptr @token_count, align 1, !tbaa !5
  %5 = icmp ult i8 %4, 4
  br i1 %5, label %6, label %11

6:                                                ; preds = %.lr.ph
  %7 = add i8 %.01316, 1
  %8 = zext i8 %.01316 to i64
  %9 = getelementptr inbounds [3 x ptr], ptr getelementptr inbounds (i8, ptr @TinyCmd_buf, i64 56), i64 0, i64 %8
  store ptr %.01217, ptr %9, align 8, !tbaa !8
  %10 = call ptr @TinyCmd_strtok_s(ptr noundef null, ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !10

11:                                               ; preds = %.lr.ph
  store ptr null, ptr @strtok_next, align 8, !tbaa !8
  br label %.loopexit

.loopexit:                                        ; preds = %6, %0, %11
  %12 = load i8, ptr getelementptr inbounds (i8, ptr @TinyCmdRunning_Cmd, i64 48), align 8, !tbaa !12
  %.not21 = icmp eq i8 %12, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph20

13:                                               ; preds = %.lr.ph20
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = load i8, ptr getelementptr inbounds (i8, ptr @TinyCmdRunning_Cmd, i64 48), align 8, !tbaa !12
  %15 = zext i8 %14 to i64
  %16 = icmp ult i64 %indvars.iv.next, %15
  br i1 %16, label %.lr.ph20, label %._crit_edge, !llvm.loop !14

.lr.ph20:                                         ; preds = %.loopexit, %13
  %indvars.iv = phi i64 [ %indvars.iv.next, %13 ], [ 0, %.loopexit ]
  %17 = getelementptr inbounds [6 x ptr], ptr @TinyCmdRunning_Cmd, i64 0, i64 %indvars.iv
  %18 = load ptr, ptr %17, align 8, !tbaa !8
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %20 = call i32 @TinyCmd_strcmp(ptr noundef %2, ptr noundef %19)
  %.not14 = icmp eq i32 %20, 0
  br i1 %.not14, label %21, label %13

21:                                               ; preds = %.lr.ph20
  %22 = getelementptr inbounds [6 x ptr], ptr @TinyCmdRunning_Cmd, i64 0, i64 %indvars.iv
  %23 = load ptr, ptr %22, align 8, !tbaa !8
  %24 = getelementptr inbounds i8, ptr %23, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !17
  %26 = call zeroext i8 %25() #2
  br label %._crit_edge

._crit_edge:                                      ; preds = %13, %.loopexit, %21
  %.0 = phi i32 [ 1, %21 ], [ 0, %.loopexit ], [ 0, %13 ]
  %27 = call i32 @TinyCmd_Buf_Clear()
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #2
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden void @TinyCmd_trim(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @TinyCmd_strtok_s(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @TinyCmd_strcmp(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @TinyCmd_Buf_Clear() local_unnamed_addr #1

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


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