; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal (IScsiBuildKeyValueList)

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

declare ptr @InsertTailList(ptr noundef, ptr noundef) #0

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

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

declare ptr @AllocatePool(i64 noundef) #0

declare void @FreePool(ptr noundef) #0

declare i64 @AsciiStrLen(ptr noundef) #0

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

12:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %86

13:                                               ; preds = %2
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = call ptr @InitializeListHead(ptr noundef %14)
  br label %16

16:                                               ; preds = %59, %13
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = icmp ugt i32 %17, 0
  br i1 %18, label %19, label %82

19:                                               ; preds = %16
  %20 = call ptr @AllocatePool(i64 noundef 32)
  store ptr %20, ptr %7, align 8, !tbaa !5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %24

23:                                               ; preds = %19
  br label %84

24:                                               ; preds = %19
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct._ISCSI_KEY_VALUE_PAIR, ptr %25, i32 0, i32 0
  %27 = call ptr @InitializeListHead(ptr noundef %26)
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct._ISCSI_KEY_VALUE_PAIR, ptr %29, i32 0, i32 1
  store ptr %28, ptr %30, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %41, %24
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = icmp ugt i32 %32, 0
  br i1 %33, label %34, label %39

34:                                               ; preds = %31
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = load i8, ptr %35, align 1, !tbaa !14
  %37 = sext i8 %36 to i32
  %38 = icmp ne i32 %37, 61
  br label %39

39:                                               ; preds = %34, %31
  %40 = phi i1 [ false, %31 ], [ %38, %34 ]
  br i1 %40, label %41, label %46

41:                                               ; preds = %39
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = add i32 %42, -1
  store i32 %43, ptr %5, align 4, !tbaa !9
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i32 1
  store ptr %45, ptr %4, align 8, !tbaa !5
  br label %31, !llvm.loop !15

46:                                               ; preds = %39
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = load i8, ptr %47, align 1, !tbaa !14
  %49 = sext i8 %48 to i32
  %50 = icmp eq i32 %49, 61
  br i1 %50, label %51, label %57

51:                                               ; preds = %46
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  store i8 0, ptr %52, align 1, !tbaa !14
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i32 1
  store ptr %54, ptr %4, align 8, !tbaa !5
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = add i32 %55, -1
  store i32 %56, ptr %5, align 4, !tbaa !9
  br label %59

57:                                               ; preds = %46
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  call void @FreePool(ptr noundef %58)
  br label %84

59:                                               ; preds = %51
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct._ISCSI_KEY_VALUE_PAIR, ptr %61, i32 0, i32 2
  store ptr %60, ptr %62, align 8, !tbaa !17
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct._ISCSI_KEY_VALUE_PAIR, ptr %64, i32 0, i32 0
  %66 = call ptr @InsertTailList(ptr noundef %63, ptr noundef %65)
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct._ISCSI_KEY_VALUE_PAIR, ptr %67, i32 0, i32 2
  %69 = load ptr, ptr %68, align 8, !tbaa !17
  %70 = call i64 @AsciiStrLen(ptr noundef %69)
  %71 = add i64 %70, 1
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = getelementptr inbounds i8, ptr %72, i64 %71
  store ptr %73, ptr %4, align 8, !tbaa !5
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct._ISCSI_KEY_VALUE_PAIR, ptr %74, i32 0, i32 2
  %76 = load ptr, ptr %75, align 8, !tbaa !17
  %77 = call i64 @AsciiStrLen(ptr noundef %76)
  %78 = trunc i64 %77 to i32
  %79 = add i32 %78, 1
  %80 = load i32, ptr %5, align 4, !tbaa !9
  %81 = sub i32 %80, %79
  store i32 %81, ptr %5, align 4, !tbaa !9
  br label %16, !llvm.loop !18

82:                                               ; preds = %16
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %83, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %86

84:                                               ; preds = %57, %23
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  call void @IScsiFreeKeyValueList(ptr noundef %85)
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %86

86:                                               ; preds = %84, %82, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %87 = load ptr, ptr %3, align 8
  ret ptr %87
}

; Function Attrs: nounwind uwtable
declare dso_local void @IScsiFreeKeyValueList(ptr noundef) #2

declare ptr @InitializeListHead(ptr noundef) #0

attributes #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 = { 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 #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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"_ISCSI_KEY_VALUE_PAIR", !13, i64 0, !6, i64 16, !6, i64 24}
!13 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!12, !6, i64 24}
!18 = distinct !{!18, !16}


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

declare ptr @InsertTailList(ptr noundef, ptr noundef) local_unnamed_addr #0

declare ptr @AllocatePool(i64 noundef) local_unnamed_addr #0

declare void @FreePool(ptr noundef) local_unnamed_addr #0

declare i64 @AsciiStrLen(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @IScsiBuildKeyValueList(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = tail call ptr @AllocatePool(i64 noundef 16) #2
  %4 = icmp eq ptr %3, null
  br i1 %4, label %.loopexit, label %5

5:                                                ; preds = %2
  %6 = tail call ptr @InitializeListHead(ptr noundef nonnull %3) #2
  %.not41 = icmp eq i32 %1, 0
  br i1 %.not41, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %5, %.critedge.thread
  %.02643 = phi ptr [ %27, %.critedge.thread ], [ %0, %5 ]
  %.02742 = phi i32 [ %31, %.critedge.thread ], [ %1, %5 ]
  %7 = tail call ptr @AllocatePool(i64 noundef 32) #2
  %8 = icmp eq ptr %7, null
  br i1 %8, label %.loopexit33, label %9

9:                                                ; preds = %.lr.ph
  %10 = tail call ptr @InitializeListHead(ptr noundef nonnull %7) #2
  %11 = getelementptr inbounds i8, ptr %7, i64 16
  store ptr %.02643, ptr %11, align 8, !tbaa !5
  %scevgep = getelementptr i8, ptr %.02643, i64 1
  %12 = add i32 %.02742, -1
  %13 = zext i32 %12 to i64
  %scevgep50 = getelementptr i8, ptr %scevgep, i64 %13
  br label %14

14:                                               ; preds = %9, %16
  %.140 = phi ptr [ %.02643, %9 ], [ %18, %16 ]
  %.12839 = phi i32 [ %.02742, %9 ], [ %17, %16 ]
  %15 = load i8, ptr %.140, align 1, !tbaa !11
  %.not32 = icmp eq i8 %15, 61
  br i1 %.not32, label %.critedge.thread, label %16

16:                                               ; preds = %14
  %17 = add i32 %.12839, -1
  %18 = getelementptr inbounds i8, ptr %.140, i64 1
  %.not31 = icmp eq i32 %17, 0
  br i1 %.not31, label %.critedge, label %14, !llvm.loop !12

.critedge:                                        ; preds = %16
  %.pr = load i8, ptr %scevgep50, align 1, !tbaa !11
  %19 = icmp eq i8 %.pr, 61
  br i1 %19, label %.critedge.thread, label %32

.critedge.thread:                                 ; preds = %14, %.critedge
  %.12837 = phi i32 [ 0, %.critedge ], [ %.12839, %14 ]
  %.135 = phi ptr [ %scevgep50, %.critedge ], [ %.140, %14 ]
  store i8 0, ptr %.135, align 1, !tbaa !11
  %20 = getelementptr inbounds i8, ptr %.135, i64 1
  %21 = add i32 %.12837, -1
  %22 = getelementptr inbounds i8, ptr %7, i64 24
  store ptr %20, ptr %22, align 8, !tbaa !14
  %23 = tail call ptr @InsertTailList(ptr noundef nonnull %3, ptr noundef nonnull %7) #2
  %24 = load ptr, ptr %22, align 8, !tbaa !14
  %25 = tail call i64 @AsciiStrLen(ptr noundef %24) #2
  %26 = getelementptr i8, ptr %20, i64 %25
  %27 = getelementptr i8, ptr %26, i64 1
  %28 = load ptr, ptr %22, align 8, !tbaa !14
  %29 = tail call i64 @AsciiStrLen(ptr noundef %28) #2
  %30 = trunc i64 %29 to i32
  %.neg = xor i32 %30, -1
  %31 = add i32 %21, %.neg
  %.not = icmp eq i32 %31, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !15

32:                                               ; preds = %.critedge
  tail call void @FreePool(ptr noundef nonnull %7) #2
  br label %.loopexit33

.loopexit33:                                      ; preds = %.lr.ph, %32
  tail call void @IScsiFreeKeyValueList(ptr noundef nonnull %3)
  br label %.loopexit

.loopexit:                                        ; preds = %.critedge.thread, %5, %2, %.loopexit33
  %.0 = phi ptr [ null, %.loopexit33 ], [ null, %2 ], [ %3, %5 ], [ %3, %.critedge.thread ]
  ret ptr %.0
}

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

declare ptr @InitializeListHead(ptr noundef) local_unnamed_addr #0

attributes #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 = { 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, !8, i64 16}
!6 = !{!"_ISCSI_KEY_VALUE_PAIR", !7, i64 0, !8, i64 16, !8, i64 24}
!7 = !{!"_LIST_ENTRY", !8, i64 0, !8, i64 8}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!9, !9, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !8, i64 24}
!15 = distinct !{!15, !13}


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