; LLVM IR from attractivechaos/klib :: knetfile (knet_read)

; 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.knetFile_s = type { i32, i32, i64, ptr, ptr, i32, [4 x i32], i32, i32, i32, i32, ptr, ptr, ptr, i64, i64, ptr, ptr }

; Function Attrs: nounwind uwtable
declare dso_local i32 @kftp_reconnect(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

; Function Attrs: nounwind uwtable
declare dso_local i32 @kftp_connect_file(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @khttp_connect_file(ptr noundef) #0

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i64 @my_netread(i32 noundef, ptr noundef, i64 noundef) #0

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

16:                                               ; preds = %3
  store i64 0, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %116

17:                                               ; preds = %3
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.knetFile_s, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !14
  %21 = icmp eq i32 %20, 2
  br i1 %21, label %22, label %39

22:                                               ; preds = %17
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.knetFile_s, ptr %23, i32 0, i32 10
  %25 = load i32, ptr %24, align 8, !tbaa !15
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %27, label %38

27:                                               ; preds = %22
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.knetFile_s, ptr %28, i32 0, i32 9
  %30 = load i32, ptr %29, align 4, !tbaa !16
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %35, label %32

32:                                               ; preds = %27
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = call i32 @kftp_reconnect(ptr noundef %33)
  br label %35

35:                                               ; preds = %32, %27
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = call i32 @kftp_connect_file(ptr noundef %36)
  br label %38

38:                                               ; preds = %35, %22
  br label %54

39:                                               ; preds = %17
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.knetFile_s, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !14
  %43 = icmp eq i32 %42, 3
  br i1 %43, label %44, label %53

44:                                               ; preds = %39
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.knetFile_s, ptr %45, i32 0, i32 10
  %47 = load i32, ptr %46, align 8, !tbaa !15
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %49, label %52

49:                                               ; preds = %44
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = call i32 @khttp_connect_file(ptr noundef %50)
  br label %52

52:                                               ; preds = %49, %44
  br label %53

53:                                               ; preds = %52, %39
  br label %54

54:                                               ; preds = %53, %38
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.knetFile_s, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 8, !tbaa !14
  %58 = icmp eq i32 %57, 1
  br i1 %58, label %59, label %102

59:                                               ; preds = %54
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %60 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %60, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  br label %61

61:                                               ; preds = %91, %59
  %62 = load i64, ptr %10, align 8, !tbaa !9
  %63 = icmp ne i64 %62, 0
  br i1 %63, label %64, label %98

64:                                               ; preds = %61
  br label %65

65:                                               ; preds = %81, %64
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.knetFile_s, ptr %66, i32 0, i32 1
  %68 = load i32, ptr %67, align 4, !tbaa !11
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = load i64, ptr %8, align 8, !tbaa !9
  %71 = getelementptr inbounds i8, ptr %69, i64 %70
  %72 = load i64, ptr %10, align 8, !tbaa !9
  %73 = call i64 @read(i32 noundef %68, ptr noundef %71, i64 noundef %72)
  store i64 %73, ptr %11, align 8, !tbaa !9
  br label %74

74:                                               ; preds = %65
  %75 = load i64, ptr %11, align 8, !tbaa !9
  %76 = icmp slt i64 %75, 0
  br i1 %76, label %77, label %81

77:                                               ; preds = %74
  %78 = call ptr @__errno_location() #5
  %79 = load i32, ptr %78, align 4, !tbaa !17
  %80 = icmp eq i32 4, %79
  br label %81

81:                                               ; preds = %77, %74
  %82 = phi i1 [ false, %74 ], [ %80, %77 ]
  br i1 %82, label %65, label %83, !llvm.loop !18

83:                                               ; preds = %81
  %84 = load i64, ptr %11, align 8, !tbaa !9
  %85 = icmp slt i64 %84, 0
  br i1 %85, label %86, label %87

86:                                               ; preds = %83
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %99

87:                                               ; preds = %83
  %88 = load i64, ptr %11, align 8, !tbaa !9
  %89 = icmp eq i64 %88, 0
  br i1 %89, label %90, label %91

90:                                               ; preds = %87
  br label %98

91:                                               ; preds = %87
  %92 = load i64, ptr %11, align 8, !tbaa !9
  %93 = load i64, ptr %8, align 8, !tbaa !9
  %94 = add nsw i64 %93, %92
  store i64 %94, ptr %8, align 8, !tbaa !9
  %95 = load i64, ptr %11, align 8, !tbaa !9
  %96 = load i64, ptr %10, align 8, !tbaa !9
  %97 = sub nsw i64 %96, %95
  store i64 %97, ptr %10, align 8, !tbaa !9
  br label %61, !llvm.loop !20

98:                                               ; preds = %90, %61
  store i32 0, ptr %9, align 4
  br label %99

99:                                               ; preds = %98, %86
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %100 = load i32, ptr %9, align 4
  switch i32 %100, label %116 [
    i32 0, label %101
  ]

101:                                              ; preds = %99
  br label %109

102:                                              ; preds = %54
  %103 = load ptr, ptr %5, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.knetFile_s, ptr %103, i32 0, i32 1
  %105 = load i32, ptr %104, align 4, !tbaa !11
  %106 = load ptr, ptr %6, align 8, !tbaa !5
  %107 = load i64, ptr %7, align 8, !tbaa !9
  %108 = call i64 @my_netread(i32 noundef %105, ptr noundef %106, i64 noundef %107)
  store i64 %108, ptr %8, align 8, !tbaa !9
  br label %109

109:                                              ; preds = %102, %101
  %110 = load i64, ptr %8, align 8, !tbaa !9
  %111 = load ptr, ptr %5, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.knetFile_s, ptr %111, i32 0, i32 2
  %113 = load i64, ptr %112, align 8, !tbaa !21
  %114 = add nsw i64 %113, %110
  store i64 %114, ptr %112, align 8, !tbaa !21
  %115 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %115, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %116

116:                                              ; preds = %109, %99, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %117 = load i64, ptr %4, align 8
  ret i64 %117
}

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 willreturn memory(none) "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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(none) }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 4}
!12 = !{!"knetFile_s", !13, i64 0, !13, i64 4, !10, i64 8, !6, i64 16, !6, i64 24, !13, i64 32, !7, i64 36, !13, i64 52, !13, i64 56, !13, i64 60, !13, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !10, i64 96, !10, i64 104, !6, i64 112, !6, i64 120}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 0}
!15 = !{!12, !13, i64 64}
!16 = !{!12, !13, i64 60}
!17 = !{!13, !13, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}
!21 = !{!12, !10, i64 8}


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

; Function Attrs: nounwind uwtable
declare dso_local i32 @kftp_reconnect(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @kftp_connect_file(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @khttp_connect_file(ptr noundef) local_unnamed_addr #0

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i64 @my_netread(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @knet_read(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp eq i32 %5, -1
  br i1 %6, label %.critedge38, label %7

7:                                                ; preds = %3
  %8 = load i32, ptr %0, align 8, !tbaa !12
  switch i32 %8, label %26 [
    i32 2, label %9
    i32 3, label %20
  ]

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %0, i64 64
  %11 = load i32, ptr %10, align 8, !tbaa !13
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %.thread

13:                                               ; preds = %9
  %14 = getelementptr inbounds i8, ptr %0, i64 60
  %15 = load i32, ptr %14, align 4, !tbaa !14
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %16, label %18

16:                                               ; preds = %13
  %17 = tail call i32 @kftp_reconnect(ptr noundef nonnull %0)
  br label %18

18:                                               ; preds = %16, %13
  %19 = tail call i32 @kftp_connect_file(ptr noundef nonnull %0)
  br label %thread-pre-split

20:                                               ; preds = %7
  %21 = getelementptr inbounds i8, ptr %0, i64 64
  %22 = load i32, ptr %21, align 8, !tbaa !13
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %.thread

24:                                               ; preds = %20
  %25 = tail call i32 @khttp_connect_file(ptr noundef nonnull %0)
  br label %thread-pre-split

thread-pre-split:                                 ; preds = %18, %24
  %.pr = load i32, ptr %0, align 8, !tbaa !12
  br label %26

26:                                               ; preds = %thread-pre-split, %7
  %27 = phi i32 [ %.pr, %thread-pre-split ], [ %8, %7 ]
  %28 = icmp eq i32 %27, 1
  br i1 %28, label %.preheader39, label %..thread_crit_edge

..thread_crit_edge:                               ; preds = %26
  %.pre = load i32, ptr %4, align 4, !tbaa !5
  br label %.thread

.preheader39:                                     ; preds = %26
  %.not3542 = icmp eq i64 %2, 0
  br i1 %.not3542, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %.preheader39, %39
  %.03044 = phi i64 [ %41, %39 ], [ %2, %.preheader39 ]
  %.03243 = phi i64 [ %40, %39 ], [ 0, %.preheader39 ]
  %29 = getelementptr inbounds i8, ptr %1, i64 %.03243
  br label %30

30:                                               ; preds = %.preheader, %34
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = tail call i64 @read(i32 noundef %31, ptr noundef %29, i64 noundef %.03044) #3
  %33 = icmp slt i64 %32, 0
  br i1 %33, label %34, label %.critedge36

34:                                               ; preds = %30
  %35 = tail call ptr @__errno_location() #4
  %36 = load i32, ptr %35, align 4, !tbaa !15
  %37 = icmp eq i32 %36, 4
  br i1 %37, label %30, label %.critedge38, !llvm.loop !16

.critedge36:                                      ; preds = %30
  %38 = icmp eq i64 %32, 0
  br i1 %38, label %.loopexit, label %39

39:                                               ; preds = %.critedge36
  %40 = add nuw nsw i64 %32, %.03243
  %41 = sub nsw i64 %.03044, %32
  %.not35 = icmp eq i64 %41, 0
  br i1 %.not35, label %.loopexit, label %.preheader, !llvm.loop !18

.thread:                                          ; preds = %..thread_crit_edge, %9, %20
  %42 = phi i32 [ %.pre, %..thread_crit_edge ], [ %5, %9 ], [ %5, %20 ]
  %43 = tail call i64 @my_netread(i32 noundef %42, ptr noundef %1, i64 noundef %2)
  br label %.loopexit

.loopexit:                                        ; preds = %39, %.critedge36, %.preheader39, %.thread
  %.133 = phi i64 [ %43, %.thread ], [ 0, %.preheader39 ], [ %40, %39 ], [ %.03243, %.critedge36 ]
  %44 = getelementptr inbounds i8, ptr %0, i64 8
  %45 = load i64, ptr %44, align 8, !tbaa !19
  %46 = add nsw i64 %45, %.133
  store i64 %46, ptr %44, align 8, !tbaa !19
  br label %.critedge38

.critedge38:                                      ; preds = %34, %3, %.loopexit
  %.1 = phi i64 [ %.133, %.loopexit ], [ 0, %3 ], [ -1, %34 ]
  ret i64 %.1
}

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #2

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 = { nofree "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 }
attributes #4 = { nounwind willreturn memory(none) }

!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 4}
!6 = !{!"knetFile_s", !7, i64 0, !7, i64 4, !10, i64 8, !11, i64 16, !11, i64 24, !7, i64 32, !8, i64 36, !7, i64 52, !7, i64 56, !7, i64 60, !7, i64 64, !11, i64 72, !11, i64 80, !11, i64 88, !10, i64 96, !10, i64 104, !11, i64 112, !11, i64 120}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !7, i64 64}
!14 = !{!6, !7, i64 60}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = !{!6, !10, i64 8}


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