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

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

; 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

declare zeroext i8 @IsListEmpty(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @Mtftp4SetLastBlockNum(ptr noundef %0, i16 noundef zeroext %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i16 %1, ptr %4, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  br label %7

7:                                                ; preds = %25, %2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call zeroext i8 @IsListEmpty(ptr noundef %8)
  %10 = icmp ne i8 %9, 0
  %11 = xor i1 %10, true
  br i1 %11, label %12, label %43

12:                                               ; preds = %7
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct._LIST_ENTRY, ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %16 = ptrtoint ptr %15 to i64
  %17 = sub i64 %16, 0
  %18 = inttoptr i64 %17 to ptr
  store ptr %18, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.MTFTP4_BLOCK_RANGE, ptr %19, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !13
  %22 = load i16, ptr %4, align 2, !tbaa !9
  %23 = zext i16 %22 to i64
  %24 = icmp sgt i64 %21, %23
  br i1 %24, label %25, label %30

25:                                               ; preds = %12
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.MTFTP4_BLOCK_RANGE, ptr %26, i32 0, i32 0
  %28 = call ptr @RemoveEntryList(ptr noundef %27)
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  call void @FreePool(ptr noundef %29)
  br label %7, !llvm.loop !16

30:                                               ; preds = %12
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.MTFTP4_BLOCK_RANGE, ptr %31, i32 0, i32 2
  %33 = load i64, ptr %32, align 8, !tbaa !18
  %34 = load i16, ptr %4, align 2, !tbaa !9
  %35 = zext i16 %34 to i64
  %36 = icmp sgt i64 %33, %35
  br i1 %36, label %37, label %42

37:                                               ; preds = %30
  %38 = load i16, ptr %4, align 2, !tbaa !9
  %39 = zext i16 %38 to i64
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MTFTP4_BLOCK_RANGE, ptr %40, i32 0, i32 2
  store i64 %39, ptr %41, align 8, !tbaa !18
  br label %42

42:                                               ; preds = %37, %30
  store i32 1, ptr %6, align 4
  br label %44

43:                                               ; preds = %7
  store i32 0, ptr %6, align 4
  br label %44

44:                                               ; preds = %43, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %45 = load i32, ptr %6, align 4
  switch i32 %45, label %47 [
    i32 0, label %46
    i32 1, label %46
  ]

46:                                               ; preds = %44, %44
  ret void

47:                                               ; preds = %44
  unreachable
}

declare ptr @RemoveEntryList(ptr noundef) #1

declare void @FreePool(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!"short", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!13 = !{!14, !15, i64 16}
!14 = !{!"", !12, i64 0, !15, i64 16, !15, i64 24, !15, i64 32, !15, i64 40}
!15 = !{!"long long", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!14, !15, i64 24}


; 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 zeroext i8 @IsListEmpty(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @Mtftp4SetLastBlockNum(ptr noundef %0, i16 noundef zeroext %1) local_unnamed_addr #1 {
  %3 = tail call zeroext i8 @IsListEmpty(ptr noundef %0) #2
  %.not10 = icmp eq i8 %3, 0
  br i1 %.not10, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = zext i16 %1 to i64
  br label %6

6:                                                ; preds = %.lr.ph, %11
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !10
  %10 = icmp sgt i64 %9, %5
  br i1 %10, label %11, label %14

11:                                               ; preds = %6
  %12 = tail call ptr @RemoveEntryList(ptr noundef nonnull %7) #2
  tail call void @FreePool(ptr noundef nonnull %7) #2
  %13 = tail call zeroext i8 @IsListEmpty(ptr noundef nonnull %0) #2
  %.not = icmp eq i8 %13, 0
  br i1 %.not, label %6, label %.loopexit, !llvm.loop !13

14:                                               ; preds = %6
  %15 = getelementptr inbounds i8, ptr %7, i64 24
  %16 = load i64, ptr %15, align 8, !tbaa !15
  %17 = icmp sgt i64 %16, %5
  br i1 %17, label %18, label %.loopexit

18:                                               ; preds = %14
  store i64 %5, ptr %15, align 8, !tbaa !15
  br label %.loopexit

.loopexit:                                        ; preds = %11, %2, %14, %18
  ret void
}

declare ptr @RemoveEntryList(ptr noundef) local_unnamed_addr #0

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


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