; LLVM IR from moarseniy/CudaFEMproject :: FEMproject/thirdparty/segyio2/src (segy)

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

@e2a = external hidden constant [256 x i8], align 16

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

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

declare i32 @fseek(ptr noundef, i64 noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @segy_read_ext_textheader(ptr noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca [3201 x i8], align 16
  %12 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = icmp slt i32 %13, -1
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store i32 10, ptr %4, align 4
  br label %54

16:                                               ; preds = %3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %20, label %19

19:                                               ; preds = %16
  store i32 2, ptr %4, align 4
  br label %54

20:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = icmp eq i32 %21, -1
  br i1 %22, label %23, label %24

23:                                               ; preds = %20
  br label %28

24:                                               ; preds = %20
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = mul nsw i32 %25, 3200
  %27 = add nsw i32 3600, %26
  br label %28

28:                                               ; preds = %24, %23
  %29 = phi i32 [ 0, %23 ], [ %27, %24 ]
  %30 = sext i32 %29 to i64
  store i64 %30, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.segy_file_handle, ptr %31, i32 0, i32 2
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = load i64, ptr %8, align 8, !tbaa !11
  %35 = call i32 @fseek(ptr noundef %33, i64 noundef %34, i32 noundef 0)
  store i32 %35, ptr %9, align 4, !tbaa !9
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %28
  store i32 2, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %53

39:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 3201, ptr %11) #4
  call void @llvm.memset.p0.i64(ptr align 16 %11, i8 0, i64 3201, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %40 = getelementptr inbounds [3201 x i8], ptr %11, i64 0, i64 0
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.segy_file_handle, ptr %41, i32 0, i32 2
  %43 = load ptr, ptr %42, align 8, !tbaa !13
  %44 = call i64 @fread(ptr noundef %40, i64 noundef 1, i64 noundef 3200, ptr noundef %43)
  store i64 %44, ptr %12, align 8, !tbaa !11
  %45 = load i64, ptr %12, align 8, !tbaa !11
  %46 = icmp ne i64 %45, 3200
  br i1 %46, label %47, label %48

47:                                               ; preds = %39
  store i32 3, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %52

48:                                               ; preds = %39
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds [3201 x i8], ptr %11, i64 0, i64 0
  %51 = call i32 @encode(ptr noundef %49, ptr noundef %50, ptr noundef @e2a, i64 noundef 3200)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %52

52:                                               ; preds = %48, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 3201, ptr %11) #4
  br label %53

53:                                               ; preds = %52, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %54

54:                                               ; preds = %53, %19, %15
  %55 = load i32, ptr %4, align 4
  ret i32 %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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { "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 }

!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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 16}
!14 = !{!"segy_file_handle", !6, i64 0, !6, i64 8, !6, i64 16, !12, i64 24, !7, i64 32, !10, i64 40, !10, i64 44, !10, i64 48}


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

@e2a = external hidden constant [256 x i8], align 16

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

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @fseek(ptr nocapture noundef, i64 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 11) i32 @segy_read_ext_textheader(ptr noundef readonly %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = alloca [3201 x i8], align 16
  %5 = icmp slt i32 %1, -1
  br i1 %5, label %22, label %6

6:                                                ; preds = %3
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %22, label %7

7:                                                ; preds = %6
  %8 = icmp eq i32 %1, -1
  %9 = mul nsw i32 %1, 3200
  %10 = add nsw i32 %9, 3600
  %11 = select i1 %8, i32 0, i32 %10
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  %15 = tail call i32 @fseek(ptr noundef %14, i64 noundef %12, i32 noundef 0)
  %.not12 = icmp eq i32 %15, 0
  br i1 %.not12, label %16, label %22

16:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 3201, ptr nonnull %4) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(3201) %4, i8 0, i64 3201, i1 false)
  %17 = load ptr, ptr %13, align 8, !tbaa !5
  %18 = call i64 @fread(ptr noundef nonnull %4, i64 noundef 1, i64 noundef 3200, ptr noundef %17)
  %.not13 = icmp eq i64 %18, 3200
  br i1 %.not13, label %19, label %21

19:                                               ; preds = %16
  %20 = call i32 @encode(ptr noundef %2, ptr noundef nonnull %4, ptr noundef nonnull @e2a, i64 noundef 3200)
  br label %21

21:                                               ; preds = %16, %19
  %.0 = phi i32 [ 0, %19 ], [ 3, %16 ]
  call void @llvm.lifetime.end.p0(i64 3201, ptr nonnull %4) #4
  br label %22

22:                                               ; preds = %21, %7, %6, %3
  %.2 = phi i32 [ 10, %3 ], [ 2, %6 ], [ %.0, %21 ], [ 2, %7 ]
  ret i32 %.2
}

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nofree nounwind "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 }

!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 16}
!6 = !{!"segy_file_handle", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !8, i64 32, !11, i64 40, !11, i64 44, !11, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass