; LLVM IR from tenok-rtos/tenok :: user/shell (shell)

; 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.shell = type { i32, i32, i32, [50 x i8], [50 x i8], [50 x i8], i8, i32, i32, i32, ptr, i32, i32, i8, ptr, %struct.list_head, ptr, ptr, i32 }
%struct.list_head = type { ptr, ptr }
%struct.shell_history = type { [50 x i8], %struct.list_head }

@.str.2 = external hidden unnamed_addr constant [8 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 void @shell_puts(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @shell_print_history(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca [150 x i8], align 16
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.shell, ptr %7, i32 0, i32 14
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  br label %38

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 150, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !14
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.shell, ptr %13, i32 0, i32 15
  %15 = getelementptr inbounds %struct.list_head, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  store ptr %16, ptr %4, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %33, %12
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.shell, ptr %19, i32 0, i32 15
  %21 = icmp ne ptr %18, %20
  br i1 %21, label %22, label %37

22:                                               ; preds = %17
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i64 -56
  store ptr %24, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds [150 x i8], ptr %3, i64 0, i64 0
  %26 = load i32, ptr %6, align 4, !tbaa !14
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %6, align 4, !tbaa !14
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.shell_history, ptr %28, i32 0, i32 0
  %30 = getelementptr inbounds [50 x i8], ptr %29, i64 0, i64 0
  %31 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %25, i64 noundef 150, ptr noundef @.str.2, i32 noundef %27, ptr noundef %30) #3
  %32 = getelementptr inbounds [150 x i8], ptr %3, i64 0, i64 0
  call void @shell_puts(ptr noundef %32)
  br label %33

33:                                               ; preds = %22
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.list_head, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  store ptr %36, ptr %4, align 8, !tbaa !5
  br label %17, !llvm.loop !17

37:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 150, ptr %3) #3
  br label %38

38:                                               ; preds = %37, %11
  ret void
}

; Function Attrs: nounwind
declare i32 @snprintf(ptr noundef, i64 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 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 200}
!10 = !{!"shell", !11, i64 0, !11, i64 4, !11, i64 8, !7, i64 12, !7, i64 62, !7, i64 112, !12, i64 162, !11, i64 164, !11, i64 168, !11, i64 172, !6, i64 176, !11, i64 184, !11, i64 188, !12, i64 192, !6, i64 200, !13, i64 208, !6, i64 224, !6, i64 232, !11, i64 240}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!"list_head", !6, i64 0, !6, i64 8}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !6, i64 208}
!16 = !{!13, !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"

@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1

; 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
declare dso_local void @shell_puts(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @shell_print_history(ptr noundef readonly %0) local_unnamed_addr #1 {
  %2 = alloca [150 x i8], align 16
  %3 = getelementptr inbounds i8, ptr %0, i64 200
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %11, label %6

6:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 150, ptr nonnull %2) #3
  %7 = getelementptr inbounds i8, ptr %0, i64 208
  %.078 = load ptr, ptr %7, align 8, !tbaa !13
  %.not9 = icmp eq ptr %.078, %7
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %.lr.ph
  %.0711 = phi ptr [ %.07, %.lr.ph ], [ %.078, %6 ]
  %.010 = phi i32 [ %9, %.lr.ph ], [ 0, %6 ]
  %8 = getelementptr inbounds i8, ptr %.0711, i64 -56
  %9 = add nuw nsw i32 %.010, 1
  %10 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %2, i64 noundef 150, ptr noundef nonnull @.str.2, i32 noundef %9, ptr noundef nonnull %8) #3
  call void @shell_puts(ptr noundef nonnull %2)
  %.07 = load ptr, ptr %.0711, align 8, !tbaa !13
  %.not = icmp eq ptr %.07, %7
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %6
  call void @llvm.lifetime.end.p0(i64 150, ptr nonnull %2) #3
  br label %11

11:                                               ; preds = %1, %._crit_edge
  ret void
}

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

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 = { 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, !11, i64 200}
!6 = !{!"shell", !7, i64 0, !7, i64 4, !7, i64 8, !8, i64 12, !8, i64 62, !8, i64 112, !10, i64 162, !7, i64 164, !7, i64 168, !7, i64 172, !11, i64 176, !7, i64 184, !7, i64 188, !10, i64 192, !11, i64 200, !12, i64 208, !11, i64 224, !11, i64 232, !7, i64 240}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_Bool", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"list_head", !11, i64 0, !11, i64 8}
!13 = !{!12, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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