; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/NetworkPkg/IScsiDxe (IScsiSessionReLogin)

; 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.EFI_BOOT_SERVICES = type { %struct.EFI_TABLE_HEADER, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.EFI_TABLE_HEADER = type { i64, i32, i32, i32, i32 }

@gBS = 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
declare dso_local i64 @IScsiSessionLogin(ptr noundef) #1

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

14:                                               ; preds = %1
  %15 = load i64, ptr %4, align 8, !tbaa !14
  store i64 %15, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %54

16:                                               ; preds = %1
  %17 = load ptr, ptr @gBS, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %17, i32 0, i32 9
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = call i64 %19(ptr noundef %20, i32 noundef 2, i64 noundef 30000000)
  store i64 %21, ptr %4, align 8, !tbaa !14
  %22 = load i64, ptr %4, align 8, !tbaa !14
  %23 = icmp slt i64 %22, 0
  br i1 %23, label %24, label %31

24:                                               ; preds = %16
  %25 = load ptr, ptr @gBS, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %25, i32 0, i32 12
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = call i64 %27(ptr noundef %28)
  %30 = load i64, ptr %4, align 8, !tbaa !14
  store i64 %30, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %54

31:                                               ; preds = %16
  br label %32

32:                                               ; preds = %44, %31
  %33 = load ptr, ptr @gBS, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %33, i32 0, i32 13
  %35 = load ptr, ptr %34, align 8, !tbaa !17
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = call i64 %35(ptr noundef %36)
  store i64 %37, ptr %5, align 8, !tbaa !14
  %38 = load i64, ptr %5, align 8, !tbaa !14
  %39 = icmp slt i64 %38, 0
  br i1 %39, label %43, label %40

40:                                               ; preds = %32
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = call i64 @IScsiSessionLogin(ptr noundef %41)
  store i64 %42, ptr %4, align 8, !tbaa !14
  br label %43

43:                                               ; preds = %40, %32
  br label %44

44:                                               ; preds = %43
  %45 = load i64, ptr %5, align 8, !tbaa !14
  %46 = icmp eq i64 %45, -9223372036854775802
  br i1 %46, label %32, label %47, !llvm.loop !18

47:                                               ; preds = %44
  %48 = load ptr, ptr @gBS, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %48, i32 0, i32 12
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = call i64 %50(ptr noundef %51)
  %53 = load i64, ptr %4, align 8, !tbaa !14
  store i64 %53, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %54

54:                                               ; preds = %47, %24, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %55 = load i64, ptr %2, align 8
  ret i64 %55
}

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 }

!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 80}
!10 = !{!"", !11, i64 0, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !6, i64 368}
!11 = !{!"", !12, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !12, i64 0}
!15 = !{!10, !6, i64 88}
!16 = !{!10, !6, i64 112}
!17 = !{!10, !6, i64 120}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@gBS = external local_unnamed_addr global ptr, align 8

; 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 i64 @IScsiSessionLogin(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @IScsiSessionReLogin(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #2
  %3 = load ptr, ptr @gBS, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 80
  %5 = load ptr, ptr %4, align 8, !tbaa !9
  %6 = call i64 %5(i32 noundef -2147483648, i64 noundef 8, ptr noundef null, ptr noundef null, ptr noundef nonnull %2) #2
  %7 = icmp slt i64 %6, 0
  br i1 %7, label %35, label %8

8:                                                ; preds = %1
  %9 = load ptr, ptr @gBS, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %9, i64 88
  %11 = load ptr, ptr %10, align 8, !tbaa !14
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = call i64 %11(ptr noundef %12, i32 noundef 2, i64 noundef 30000000) #2
  %14 = icmp slt i64 %13, 0
  br i1 %14, label %15, label %.preheader

15:                                               ; preds = %8
  %16 = load ptr, ptr @gBS, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 112
  %18 = load ptr, ptr %17, align 8, !tbaa !15
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = call i64 %18(ptr noundef %19) #2
  br label %35

.preheader:                                       ; preds = %8, %28
  %21 = load ptr, ptr @gBS, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i64 120
  %23 = load ptr, ptr %22, align 8, !tbaa !16
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = call i64 %23(ptr noundef %24) #2
  %26 = icmp slt i64 %25, 0
  br i1 %26, label %28, label %.thread

.thread:                                          ; preds = %.preheader
  %27 = call i64 @IScsiSessionLogin(ptr noundef %0)
  br label %.loopexit

28:                                               ; preds = %.preheader
  %29 = icmp eq i64 %25, -9223372036854775802
  br i1 %29, label %.preheader, label %.loopexit, !llvm.loop !17

.loopexit:                                        ; preds = %28, %.thread
  %.113 = phi i64 [ %27, %.thread ], [ %13, %28 ]
  %30 = load ptr, ptr @gBS, align 8, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %30, i64 112
  %32 = load ptr, ptr %31, align 8, !tbaa !15
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = call i64 %32(ptr noundef %33) #2
  br label %35

35:                                               ; preds = %1, %.loopexit, %15
  %.0 = phi i64 [ %13, %15 ], [ %.113, %.loopexit ], [ %6, %1 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #2
  ret i64 %.0
}

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 = { 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 80}
!10 = !{!"", !11, i64 0, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !6, i64 368}
!11 = !{!"", !12, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!10, !6, i64 88}
!15 = !{!10, !6, i64 112}
!16 = !{!10, !6, i64 120}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass