; LLVM IR from erwincoumans/tiny-differentiable-simulator :: src/visualizer/opengl (tiny_x11_opengl_window)

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

%class.TinyX11OpenGLWindow = type <{ %class.TinyWindowInterface, ptr, i8, i8, [6 x i8] }>
%class.TinyWindowInterface = type { ptr }
%struct.InternalData2 = type { ptr, i64, ptr, i64, %struct.XSetWindowAttributes, i64, ptr, %struct.XWindowAttributes, %union._XEvent, ptr, i32, i32, i32, ptr, ptr, ptr, ptr, ptr }
%struct.XSetWindowAttributes = type { i64, i64, i64, i64, i32, i32, i32, i64, i64, i32, i64, i64, i32, i64, i64 }
%struct.XWindowAttributes = type { i32, i32, i32, i32, i32, i32, ptr, i64, i32, i32, i32, i32, i64, i64, i32, i64, i32, i32, i64, i64, i64, i32, ptr }
%union._XEvent = type { [24 x i64] }

@.str.25 = external hidden unnamed_addr constant [113 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.27 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [64 x i8], align 1

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

declare i32 @printf(ptr 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: mustprogress uwtable
define dso_local noundef i32 @_ZN19TinyX11OpenGLWindow16file_open_dialogEPci(ptr noundef nonnull align 8 dereferenceable(18) %0, ptr noundef %1, i32 noundef %2) unnamed_addr #2 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %10 = call noalias ptr @popen(ptr noundef @.str.25, ptr noundef @.str.26)
  store ptr %10, ptr %8, align 8, !tbaa !5
  %11 = load ptr, ptr %8, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %40

13:                                               ; preds = %3
  br label %14

14:                                               ; preds = %36, %13
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = sub nsw i32 %16, 1
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = call ptr @fgets(ptr noundef %15, i32 noundef %17, ptr noundef %18)
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %37

21:                                               ; preds = %14
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = call i64 @strlen(ptr noundef %22) #5
  %24 = trunc i64 %23 to i32
  store i32 %24, ptr %7, align 4, !tbaa !9
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %27, label %36

27:                                               ; preds = %21
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = sub nsw i32 %29, 1
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i8, ptr %28, i64 %31
  store i8 0, ptr %32, align 1, !tbaa !11
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = call i32 (ptr, ...) @printf(ptr noundef @.str.27, i32 noundef %33, ptr noundef %34)
  br label %36

36:                                               ; preds = %27, %21
  br label %14, !llvm.loop !12

37:                                               ; preds = %14
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = call i32 @pclose(ptr noundef %38)
  br label %42

40:                                               ; preds = %3
  %41 = call i32 (ptr, ...) @printf(ptr noundef @.str.28)
  br label %42

42:                                               ; preds = %40, %37
  %43 = getelementptr inbounds %class.TinyX11OpenGLWindow, ptr %9, i32 0, i32 1
  %44 = load ptr, ptr %43, align 8, !tbaa !14
  %45 = getelementptr inbounds %struct.InternalData2, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !18
  %47 = getelementptr inbounds %class.TinyX11OpenGLWindow, ptr %9, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  %49 = getelementptr inbounds %struct.InternalData2, ptr %48, i32 0, i32 5
  %50 = load i64, ptr %49, align 8, !tbaa !23
  %51 = call i32 @XRaiseWindow(ptr noundef %46, i64 noundef %50)
  %52 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret i32 %52
}

declare noalias ptr @popen(ptr noundef, ptr noundef) #1

declare ptr @fgets(ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

declare i32 @pclose(ptr noundef) #1

declare i32 @XRaiseWindow(ptr noundef, i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { nounwind willreturn memory(read) "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(read) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 8}
!15 = !{!"_ZTS19TinyX11OpenGLWindow", !16, i64 0, !6, i64 8, !17, i64 16, !17, i64 17}
!16 = !{!"_ZTS19TinyWindowInterface"}
!17 = !{!"bool", !7, i64 0}
!18 = !{!19, !6, i64 0}
!19 = !{!"_ZTS13InternalData2", !6, i64 0, !20, i64 8, !6, i64 16, !20, i64 24, !21, i64 32, !20, i64 144, !6, i64 152, !22, i64 160, !7, i64 296, !6, i64 488, !10, i64 496, !10, i64 500, !10, i64 504, !6, i64 512, !6, i64 520, !6, i64 528, !6, i64 536, !6, i64 544}
!20 = !{!"long", !7, i64 0}
!21 = !{!"_ZTS20XSetWindowAttributes", !20, i64 0, !20, i64 8, !20, i64 16, !20, i64 24, !10, i64 32, !10, i64 36, !10, i64 40, !20, i64 48, !20, i64 56, !10, i64 64, !20, i64 72, !20, i64 80, !10, i64 88, !20, i64 96, !20, i64 104}
!22 = !{!"_ZTS17XWindowAttributes", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !6, i64 24, !20, i64 32, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !20, i64 56, !20, i64 64, !10, i64 72, !20, i64 80, !10, i64 88, !10, i64 92, !20, i64 96, !20, i64 104, !20, i64 112, !10, i64 120, !6, i64 128}
!23 = !{!19, !20, i64 144}


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

@.str.25 = external hidden unnamed_addr constant [113 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.27 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [64 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN19TinyX11OpenGLWindow16file_open_dialogEPci(ptr nocapture noundef nonnull readonly align 8 dereferenceable(18) %0, ptr noundef %1, i32 noundef %2) unnamed_addr #1 align 2 {
  %4 = tail call noalias ptr @popen(ptr noundef nonnull @.str.25, ptr noundef nonnull @.str.26)
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %18, label %.preheader

.preheader:                                       ; preds = %3
  %5 = add nsw i32 %2, -1
  %6 = tail call ptr @fgets(ptr noundef %1, i32 noundef %5, ptr noundef nonnull %4)
  %.not1314 = icmp eq ptr %6, null
  br i1 %.not1314, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %15
  %7 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #4
  %8 = trunc i64 %7 to i32
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %10, label %15

10:                                               ; preds = %.lr.ph
  %11 = add i64 %7, 4294967295
  %12 = and i64 %11, 4294967295
  %13 = getelementptr inbounds i8, ptr %1, i64 %12
  store i8 0, ptr %13, align 1, !tbaa !5
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.27, i32 noundef %8, ptr noundef %1)
  br label %15

15:                                               ; preds = %10, %.lr.ph
  %16 = tail call ptr @fgets(ptr noundef %1, i32 noundef %5, ptr noundef nonnull %4)
  %.not13 = icmp eq ptr %16, null
  br i1 %.not13, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %15, %.preheader
  %.0.lcssa = phi i32 [ 0, %.preheader ], [ %8, %15 ]
  %17 = tail call i32 @pclose(ptr noundef nonnull %4)
  br label %20

18:                                               ; preds = %3
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.28)
  br label %20

20:                                               ; preds = %18, %._crit_edge
  %.1 = phi i32 [ %.0.lcssa, %._crit_edge ], [ 0, %18 ]
  %21 = getelementptr inbounds i8, ptr %0, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !10
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %22, i64 144
  %25 = load i64, ptr %24, align 8, !tbaa !21
  %26 = tail call i32 @XRaiseWindow(ptr noundef %23, i64 noundef %25)
  ret i32 %.1
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @popen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef ptr @fgets(ptr noundef, i32 noundef, ptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @pclose(ptr nocapture noundef) local_unnamed_addr #0

declare i32 @XRaiseWindow(ptr noundef, i64 noundef) local_unnamed_addr #3

attributes #0 = { 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 #1 = { mustprogress 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 nofree nounwind willreturn memory(argmem: read) "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 willreturn memory(read) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !13, i64 8}
!11 = !{!"_ZTS19TinyX11OpenGLWindow", !12, i64 0, !13, i64 8, !14, i64 16, !14, i64 17}
!12 = !{!"_ZTS19TinyWindowInterface"}
!13 = !{!"any pointer", !6, i64 0}
!14 = !{!"bool", !6, i64 0}
!15 = !{!16, !13, i64 0}
!16 = !{!"_ZTS13InternalData2", !13, i64 0, !17, i64 8, !13, i64 16, !17, i64 24, !18, i64 32, !17, i64 144, !13, i64 152, !20, i64 160, !6, i64 296, !13, i64 488, !19, i64 496, !19, i64 500, !19, i64 504, !13, i64 512, !13, i64 520, !13, i64 528, !13, i64 536, !13, i64 544}
!17 = !{!"long", !6, i64 0}
!18 = !{!"_ZTS20XSetWindowAttributes", !17, i64 0, !17, i64 8, !17, i64 16, !17, i64 24, !19, i64 32, !19, i64 36, !19, i64 40, !17, i64 48, !17, i64 56, !19, i64 64, !17, i64 72, !17, i64 80, !19, i64 88, !17, i64 96, !17, i64 104}
!19 = !{!"int", !6, i64 0}
!20 = !{!"_ZTS17XWindowAttributes", !19, i64 0, !19, i64 4, !19, i64 8, !19, i64 12, !19, i64 16, !19, i64 20, !13, i64 24, !17, i64 32, !19, i64 40, !19, i64 44, !19, i64 48, !19, i64 52, !17, i64 56, !17, i64 64, !19, i64 72, !17, i64 80, !19, i64 88, !19, i64 92, !17, i64 96, !17, i64 104, !17, i64 112, !19, i64 120, !13, i64 128}
!21 = !{!16, !17, i64 144}


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