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

@a2e = 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

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

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @segy_write_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 i32, align 4
  %9 = alloca [3200 x i8], align 16
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %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 ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.segy_file_handle, ptr %13, i32 0, i32 5
  %15 = load i32, ptr %14, align 8, !tbaa !11
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %18, label %17

17:                                               ; preds = %3
  store i32 13, ptr %4, align 4
  br label %63

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 3200, ptr %9) #3
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = icmp slt i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  store i32 10, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %62

22:                                               ; preds = %18
  %23 = getelementptr inbounds [3200 x i8], ptr %9, i64 0, i64 0
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = call i32 @encode(ptr noundef %23, ptr noundef %24, ptr noundef @a2e, i64 noundef 3200)
  store i32 %25, ptr %8, align 4, !tbaa !9
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %22
  %29 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %29, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %62

30:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %34

33:                                               ; preds = %30
  br label %39

34:                                               ; preds = %30
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = sub nsw i32 %35, 1
  %37 = mul nsw i32 %36, 3200
  %38 = add nsw i32 3600, %37
  br label %39

39:                                               ; preds = %34, %33
  %40 = phi i32 [ 0, %33 ], [ %38, %34 ]
  %41 = sext i32 %40 to i64
  store i64 %41, ptr %11, align 8, !tbaa !14
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.segy_file_handle, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = load i64, ptr %11, align 8, !tbaa !14
  %46 = call i32 @fseek(ptr noundef %44, i64 noundef %45, i32 noundef 0)
  store i32 %46, ptr %8, align 4, !tbaa !9
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %50

49:                                               ; preds = %39
  store i32 2, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %61

50:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %51 = getelementptr inbounds [3200 x i8], ptr %9, i64 0, i64 0
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.segy_file_handle, ptr %52, i32 0, i32 2
  %54 = load ptr, ptr %53, align 8, !tbaa !15
  %55 = call i64 @fwrite(ptr noundef %51, i64 noundef 1, i64 noundef 3200, ptr noundef %54)
  store i64 %55, ptr %12, align 8, !tbaa !14
  %56 = load i64, ptr %12, align 8, !tbaa !14
  %57 = icmp ne i64 %56, 3200
  br i1 %57, label %58, label %59

58:                                               ; preds = %50
  store i32 4, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %60

59:                                               ; preds = %50
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %60

60:                                               ; preds = %59, %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %61

61:                                               ; preds = %60, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %62

62:                                               ; preds = %61, %28, %21
  call void @llvm.lifetime.end.p0(i64 3200, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %63

63:                                               ; preds = %62, %17
  %64 = load i32, ptr %4, align 4
  ret i32 %64
}

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

@a2e = 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: nofree nounwind
declare noundef i32 @fseek(ptr nocapture noundef, i64 noundef, i32 noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @segy_write_textheader(ptr nocapture noundef readonly %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = alloca [3200 x i8], align 16
  %5 = getelementptr inbounds i8, ptr %0, i64 40
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %24, label %7

7:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 3200, ptr nonnull %4) #3
  %8 = icmp slt i32 %1, 0
  br i1 %8, label %23, label %9

9:                                                ; preds = %7
  %10 = call i32 @encode(ptr noundef nonnull %4, ptr noundef %2, ptr noundef nonnull @a2e, i64 noundef 3200)
  %.not14 = icmp eq i32 %10, 0
  br i1 %.not14, label %11, label %23

11:                                               ; preds = %9
  %12 = icmp eq i32 %1, 0
  %13 = mul i32 %1, 3200
  %14 = add i32 %13, 400
  %15 = select i1 %12, i32 0, i32 %14
  %16 = zext nneg i32 %15 to i64
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !12
  %19 = call i32 @fseek(ptr noundef %18, i64 noundef %16, i32 noundef 0)
  %.not15 = icmp eq i32 %19, 0
  br i1 %.not15, label %20, label %23

20:                                               ; preds = %11
  %21 = load ptr, ptr %17, align 8, !tbaa !12
  %22 = call i64 @fwrite(ptr noundef nonnull %4, i64 noundef 1, i64 noundef 3200, ptr noundef %21)
  %.not16 = icmp eq i64 %22, 3200
  %. = select i1 %.not16, i32 0, i32 4
  br label %23

23:                                               ; preds = %20, %11, %9, %7
  %.2 = phi i32 [ 10, %7 ], [ %10, %9 ], [ %., %20 ], [ 2, %11 ]
  call void @llvm.lifetime.end.p0(i64 3200, ptr nonnull %4) #3
  br label %24

24:                                               ; preds = %3, %23
  %.3 = phi i32 [ %.2, %23 ], [ 13, %3 ]
  ret i32 %.3
}

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 = { 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 #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, !11, i64 40}
!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}
!12 = !{!6, !7, i64 16}


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