; LLVM IR from aagdev/mlimgsynth :: src/ccommon (stream)

; 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.StreamClass = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.Stream = type { ptr, ptr, ptr, ptr, ptr, %struct.StreamInt, i64, i32 }
%struct.StreamInt = type { [3 x ptr] }

@stream_class_posix_mmap = external dso_local constant %struct.StreamClass, 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 i32 @stream_open_end(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @stream_buffer_free(ptr noundef) #1

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

16:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %89

17:                                               ; preds = %3
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = call i64 @lseek(i32 noundef %18, i64 noundef 0, i32 noundef 0) #4
  %20 = icmp ne i64 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %17
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %89

22:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %10, align 4, !tbaa !9
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = and i32 %23, 1
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %29

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

29:                                               ; preds = %26, %22
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = and i32 %30, 2
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %36

33:                                               ; preds = %29
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = or i32 %34, 2
  store i32 %35, ptr %10, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %33, %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %37 = load i64, ptr %8, align 8, !tbaa !11
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = call ptr @mmap(ptr noundef null, i64 noundef %37, i32 noundef %38, i32 noundef 2, i32 noundef %39, i64 noundef 0) #4
  store ptr %40, ptr %11, align 8, !tbaa !5
  %41 = load ptr, ptr %11, align 8, !tbaa !5
  %42 = icmp eq ptr %41, inttoptr (i64 -1 to ptr)
  br i1 %42, label %43, label %44

43:                                               ; preds = %36
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %88

44:                                               ; preds = %36
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Stream, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %51

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

51:                                               ; preds = %49, %44
  %52 = load ptr, ptr %11, align 8, !tbaa !5
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.Stream, ptr %53, i32 0, i32 2
  store ptr %52, ptr %54, align 8, !tbaa !13
  %55 = load ptr, ptr %11, align 8, !tbaa !5
  %56 = load i64, ptr %8, align 8, !tbaa !11
  %57 = getelementptr inbounds i8, ptr %55, i64 %56
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Stream, ptr %58, i32 0, i32 3
  store ptr %57, ptr %59, align 8, !tbaa !16
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Stream, ptr %60, i32 0, i32 2
  %62 = load ptr, ptr %61, align 8, !tbaa !13
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.Stream, ptr %63, i32 0, i32 0
  store ptr %62, ptr %64, align 8, !tbaa !17
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.Stream, ptr %65, i32 0, i32 3
  %67 = load ptr, ptr %66, align 8, !tbaa !16
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.Stream, ptr %68, i32 0, i32 1
  store ptr %67, ptr %69, align 8, !tbaa !18
  %70 = load ptr, ptr %11, align 8, !tbaa !5
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Stream, ptr %71, i32 0, i32 5
  %73 = getelementptr inbounds %struct.StreamInt, ptr %72, i32 0, i32 0
  %74 = getelementptr inbounds [3 x ptr], ptr %73, i64 0, i64 0
  store ptr %70, ptr %74, align 8, !tbaa !5
  %75 = load i64, ptr %8, align 8, !tbaa !11
  %76 = inttoptr i64 %75 to ptr
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.Stream, ptr %77, i32 0, i32 5
  %79 = getelementptr inbounds %struct.StreamInt, ptr %78, i32 0, i32 0
  %80 = getelementptr inbounds [3 x ptr], ptr %79, i64 0, i64 1
  store ptr %76, ptr %80, align 8, !tbaa !5
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Stream, ptr %81, i32 0, i32 4
  store ptr @stream_class_posix_mmap, ptr %82, align 8, !tbaa !19
  %83 = load i32, ptr %6, align 4, !tbaa !9
  %84 = call i32 @close(i32 noundef %83)
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = load i32, ptr %7, align 4, !tbaa !9
  %87 = call i32 @stream_open_end(ptr noundef %85, i32 noundef %86)
  store i32 %87, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %88

88:                                               ; preds = %51, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %89

89:                                               ; preds = %88, %21, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %90 = load i32, ptr %4, align 4
  ret i32 %90
}

; Function Attrs: nounwind
declare i64 @lseek(i32 noundef, i64 noundef, i32 noundef) #2

; Function Attrs: nounwind
declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef) #2

declare i32 @close(i32 noundef) #3

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 "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 = { "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 = !{!"Stream", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !15, i64 40, !12, i64 64, !10, i64 72}
!15 = !{!"StreamInt", !7, i64 0}
!16 = !{!14, !6, i64 24}
!17 = !{!14, !6, i64 0}
!18 = !{!14, !6, i64 8}
!19 = !{!14, !6, i64 32}


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

%struct.StreamClass = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@stream_class_posix_mmap = external dso_local constant %struct.StreamClass, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @stream_posix_mmap_open_handle(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = tail call i64 @lseek(i32 noundef %1, i64 noundef 0, i32 noundef 2) #3
  %5 = icmp slt i64 %4, 0
  br i1 %5, label %25, label %6

6:                                                ; preds = %3
  %7 = tail call i64 @lseek(i32 noundef %1, i64 noundef 0, i32 noundef 0) #3
  %.not = icmp eq i64 %7, 0
  br i1 %.not, label %8, label %25

8:                                                ; preds = %6
  %.131 = and i32 %2, 3
  %9 = tail call ptr @mmap(ptr noundef null, i64 noundef %4, i32 noundef %.131, i32 noundef 2, i32 noundef %1, i64 noundef 0) #3
  %10 = icmp eq ptr %9, inttoptr (i64 -1 to ptr)
  br i1 %10, label %25, label %11

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %.not36 = icmp eq ptr %13, null
  br i1 %.not36, label %15, label %14

14:                                               ; preds = %11
  tail call void @stream_buffer_free(ptr noundef nonnull %0)
  br label %15

15:                                               ; preds = %14, %11
  store ptr %9, ptr %12, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %9, i64 %4
  %17 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr %16, ptr %17, align 8, !tbaa !13
  store ptr %9, ptr %0, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %0, i64 8
  store ptr %16, ptr %18, align 8, !tbaa !15
  %19 = getelementptr inbounds i8, ptr %0, i64 40
  store ptr %9, ptr %19, align 8, !tbaa !16
  %20 = inttoptr i64 %4 to ptr
  %21 = getelementptr inbounds i8, ptr %0, i64 48
  store ptr %20, ptr %21, align 8, !tbaa !16
  %22 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr @stream_class_posix_mmap, ptr %22, align 8, !tbaa !17
  %23 = tail call i32 @close(i32 noundef %1) #3
  %24 = tail call i32 @stream_open_end(ptr noundef nonnull %0, i32 noundef %2)
  br label %25

25:                                               ; preds = %15, %8, %6, %3
  %.1 = phi i32 [ -1, %3 ], [ -1, %6 ], [ %24, %15 ], [ -1, %8 ]
  ret i32 %.1
}

; Function Attrs: nounwind
declare i64 @lseek(i32 noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

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

declare i32 @close(i32 noundef) 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 = { 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 #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, !7, i64 16}
!6 = !{!"Stream", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !11, i64 64, !12, i64 72}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"StreamInt", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !7, i64 24}
!14 = !{!6, !7, i64 0}
!15 = !{!6, !7, i64 8}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !7, i64 32}


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