; LLVM IR from devicehive/esp8266-firmware :: firmware-src/sources (dhterminal_commandline)

; 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.DHCOMMAND = type { ptr, ptr, ptr }

@mCommands = external hidden constant [12 x %struct.DHCOMMAND], align 16
@dhterminal_commandline_autocompleter.mLastIndex = external hidden global i32, align 4
@dhterminal_commandline_autocompleter.mLastReturned = external hidden global ptr, align 8
@dhterminal_commandline_autocompleter.mLastPatter = external hidden global [81 x i8], align 16
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1

; 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
declare dso_local i32 @line_containt_command(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @dhterminal_commandline_autocompleter(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 0
  %8 = load i8, ptr %7, align 1, !tbaa !9
  %9 = sext i8 %8 to i32
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  br label %58

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = load ptr, ptr @dhterminal_commandline_autocompleter.mLastReturned, align 8, !tbaa !5
  %15 = call i32 @ets_strcmp(ptr noundef %13, ptr noundef %14)
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %12
  store i32 0, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  br label %21

18:                                               ; preds = %12
  %19 = load i32, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  store ptr @dhterminal_commandline_autocompleter.mLastPatter, ptr %3, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %18, %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %22 = load i32, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  store i32 %22, ptr %4, align 4, !tbaa !10
  br label %23

23:                                               ; preds = %47, %21
  %24 = load i32, ptr %4, align 4, !tbaa !10
  %25 = sext i32 %24 to i64
  %26 = icmp ult i64 %25, 12
  br i1 %26, label %27, label %50

27:                                               ; preds = %23
  %28 = load i32, ptr %4, align 4, !tbaa !10
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [12 x %struct.DHCOMMAND], ptr @mCommands, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.DHCOMMAND, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !12
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = call i32 @line_containt_command(ptr noundef %32, ptr noundef %33, i32 noundef 0)
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %46

36:                                               ; preds = %27
  %37 = load i32, ptr %4, align 4, !tbaa !10
  store i32 %37, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  %38 = load i32, ptr %4, align 4, !tbaa !10
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [12 x %struct.DHCOMMAND], ptr @mCommands, i64 0, i64 %39
  %41 = getelementptr inbounds %struct.DHCOMMAND, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !12
  store ptr %42, ptr @dhterminal_commandline_autocompleter.mLastReturned, align 8, !tbaa !5
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = call i32 (ptr, i32, ptr, ...) @snprintf(ptr noundef @dhterminal_commandline_autocompleter.mLastPatter, i32 noundef 81, ptr noundef @.str.4, ptr noundef %43)
  %45 = load ptr, ptr @dhterminal_commandline_autocompleter.mLastReturned, align 8, !tbaa !5
  store ptr %45, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %57

46:                                               ; preds = %27
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %4, align 4, !tbaa !10
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %4, align 4, !tbaa !10
  br label %23, !llvm.loop !14

50:                                               ; preds = %23
  %51 = load i32, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %53, label %56

53:                                               ; preds = %50
  store i32 0, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = call ptr @dhterminal_commandline_autocompleter(ptr noundef %54)
  store ptr %55, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %57

56:                                               ; preds = %50
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %57

57:                                               ; preds = %56, %53, %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %58

58:                                               ; preds = %57, %11
  %59 = load ptr, ptr %2, align 8
  ret ptr %59
}

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

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

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 = { "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 = !{!13, !6, i64 0}
!13 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"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.DHCOMMAND = type { ptr, ptr, ptr }

@mCommands = external hidden local_unnamed_addr constant [12 x %struct.DHCOMMAND], align 16
@dhterminal_commandline_autocompleter.mLastIndex = external hidden local_unnamed_addr global i32, align 4
@dhterminal_commandline_autocompleter.mLastReturned = external hidden local_unnamed_addr global ptr, align 8
@dhterminal_commandline_autocompleter.mLastPatter = external hidden global [81 x i8], align 16
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @line_containt_command(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @dhterminal_commandline_autocompleter(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr %0, align 1, !tbaa !5
  %3 = icmp eq i8 %2, 0
  br i1 %3, label %.loopexit, label %.lr.ph19

.lr.ph19:                                         ; preds = %1, %tailrecurse
  %.tr17 = phi ptr [ %.010, %tailrecurse ], [ %0, %1 ]
  %4 = load ptr, ptr @dhterminal_commandline_autocompleter.mLastReturned, align 8, !tbaa !8
  %5 = tail call i32 @ets_strcmp(ptr noundef nonnull %.tr17, ptr noundef %4) #2
  %.not = icmp eq i32 %5, 0
  %6 = load i32, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4
  %7 = add nsw i32 %6, 1
  %storemerge = select i1 %.not, i32 %7, i32 0
  %.010 = select i1 %.not, ptr @dhterminal_commandline_autocompleter.mLastPatter, ptr %.tr17
  store i32 %storemerge, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  %8 = icmp ult i32 %storemerge, 12
  br i1 %8, label %.lr.ph.preheader, label %tailrecurse

.lr.ph.preheader:                                 ; preds = %.lr.ph19
  %9 = zext nneg i32 %storemerge to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %17
  %indvars.iv = phi i64 [ %9, %.lr.ph.preheader ], [ %indvars.iv.next, %17 ]
  %10 = getelementptr inbounds [12 x %struct.DHCOMMAND], ptr @mCommands, i64 0, i64 %indvars.iv
  %11 = load ptr, ptr %10, align 8, !tbaa !12
  %12 = tail call i32 @line_containt_command(ptr noundef %11, ptr noundef nonnull %.010, i32 noundef 0)
  %.not14 = icmp eq i32 %12, 0
  br i1 %.not14, label %17, label %13

13:                                               ; preds = %.lr.ph
  %14 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %14, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  store ptr %11, ptr @dhterminal_commandline_autocompleter.mLastReturned, align 8, !tbaa !8
  %15 = tail call i32 (ptr, i32, ptr, ...) @snprintf(ptr noundef nonnull @dhterminal_commandline_autocompleter.mLastPatter, i32 noundef 81, ptr noundef nonnull @.str.4, ptr noundef nonnull %.010) #2
  %16 = load ptr, ptr @dhterminal_commandline_autocompleter.mLastReturned, align 8, !tbaa !8
  br label %.loopexit

17:                                               ; preds = %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = and i64 %indvars.iv.next, 4294967295
  %exitcond.not = icmp eq i64 %18, 12
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %17
  %.pre = load i32, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  %.not13 = icmp eq i32 %.pre, 0
  br i1 %.not13, label %.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph19, %._crit_edge
  store i32 0, ptr @dhterminal_commandline_autocompleter.mLastIndex, align 4, !tbaa !10
  %19 = load i8, ptr %.010, align 1, !tbaa !5
  %20 = icmp eq i8 %19, 0
  br i1 %20, label %.loopexit, label %.lr.ph19

.loopexit:                                        ; preds = %tailrecurse, %._crit_edge, %1, %13
  %.1 = phi ptr [ %16, %13 ], [ null, %1 ], [ null, %._crit_edge ], [ null, %tailrecurse ]
  ret ptr %.1
}

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

declare i32 @snprintf(ptr noundef, i32 noundef, 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 = { 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 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!13, !9, i64 0}
!13 = !{!"", !9, i64 0, !9, i64 8, !9, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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