; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/ShellPkg/Library (ShellCommandFindMapItem)

; 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_MAP_LIST = type { %struct._LIST_ENTRY, ptr, ptr, ptr, i64 }
%struct._LIST_ENTRY = type { ptr, ptr }
%struct._EFI_UNICODE_COLLATION_PROTOCOL = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@gShellMapList = external global %struct.SHELL_MAP_LIST, align 8
@gUnicodeCollation = external global ptr, 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 ptr @ShellCommandFindMapItem(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %6 = call ptr @GetFirstNode(ptr noundef @gShellMapList)
  store ptr %6, ptr %4, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %27, %1
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.SHELL_MAP_LIST, ptr %8, i32 0, i32 0
  %10 = call zeroext i8 @IsNull(ptr noundef @gShellMapList, ptr noundef %9)
  %11 = icmp ne i8 %10, 0
  %12 = xor i1 %11, true
  br i1 %12, label %13, label %31

13:                                               ; preds = %7
  %14 = load ptr, ptr @gUnicodeCollation, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct._EFI_UNICODE_COLLATION_PROTOCOL, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !9
  %17 = load ptr, ptr @gUnicodeCollation, align 8, !tbaa !5
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.SHELL_MAP_LIST, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !11
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = call i64 %16(ptr noundef %17, ptr noundef %20, ptr noundef %21)
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %13
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %25, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %32

26:                                               ; preds = %13
  br label %27

27:                                               ; preds = %26
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.SHELL_MAP_LIST, ptr %28, i32 0, i32 0
  %30 = call ptr @GetNextNode(ptr noundef @gShellMapList, ptr noundef %29)
  store ptr %30, ptr %4, align 8, !tbaa !5
  br label %7, !llvm.loop !15

31:                                               ; preds = %7
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %32

32:                                               ; preds = %31, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %33 = load ptr, ptr %2, align 8
  ret ptr %33
}

declare ptr @GetFirstNode(ptr noundef) #2

declare zeroext i8 @IsNull(ptr noundef, ptr noundef) #2

declare ptr @GetNextNode(ptr 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 = !{!10, !6, i64 0}
!10 = !{!"_EFI_UNICODE_COLLATION_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!11 = !{!12, !6, i64 24}
!12 = !{!"", !13, i64 0, !6, i64 16, !6, i64 24, !6, i64 32, !14, i64 40}
!13 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!14 = !{!"long long", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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.SHELL_MAP_LIST = type { %struct._LIST_ENTRY, ptr, ptr, ptr, i64 }
%struct._LIST_ENTRY = type { ptr, ptr }

@gShellMapList = external global %struct.SHELL_MAP_LIST, align 8
@gUnicodeCollation = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @ShellCommandFindMapItem(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call ptr @GetFirstNode(ptr noundef nonnull @gShellMapList) #2
  %3 = tail call zeroext i8 @IsNull(ptr noundef nonnull @gShellMapList, ptr noundef %2) #2
  %.not7 = icmp eq i8 %3, 0
  br i1 %.not7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1, %10
  %.08 = phi ptr [ %11, %10 ], [ %2, %1 ]
  %4 = load ptr, ptr @gUnicodeCollation, align 8, !tbaa !5
  %5 = load ptr, ptr %4, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %.08, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = tail call i64 %5(ptr noundef nonnull %4, ptr noundef %7, ptr noundef %0) #2
  %9 = icmp eq i64 %8, 0
  br i1 %9, label %._crit_edge, label %10

10:                                               ; preds = %.lr.ph
  %11 = tail call ptr @GetNextNode(ptr noundef nonnull @gShellMapList, ptr noundef nonnull %.08) #2
  %12 = tail call zeroext i8 @IsNull(ptr noundef nonnull @gShellMapList, ptr noundef %11) #2
  %.not = icmp eq i8 %12, 0
  br i1 %.not, label %.lr.ph, label %._crit_edge, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %10, %1
  %.06 = phi ptr [ null, %1 ], [ null, %10 ], [ %.08, %.lr.ph ]
  ret ptr %.06
}

declare ptr @GetFirstNode(ptr noundef) local_unnamed_addr #1

declare zeroext i8 @IsNull(ptr noundef, ptr noundef) local_unnamed_addr #1

declare ptr @GetNextNode(ptr 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_EFI_UNICODE_COLLATION_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!11 = !{!12, !6, i64 24}
!12 = !{!"", !13, i64 0, !6, i64 16, !6, i64 24, !6, i64 32, !14, i64 40}
!13 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!14 = !{!"long long", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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