; LLVM IR from niessner/Matterport :: code/gaps/pkgs/fglut (freeglut_joystick)

; 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.tagSFG_Joystick = type { %struct.js_event, i32, [16 x float], [128 x i8], i32, i32, i8, [128 x i8], i32, i32, [16 x float], [16 x float], [16 x float], [16 x float], [16 x float] }
%struct.js_event = type { i32, i16, i8, i8 }

@fgJoystick = external hidden global [2 x ptr], 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @fgJoystickClose() #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %2

2:                                                ; preds = %36, %0
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = icmp slt i32 %3, 2
  br i1 %4, label %5, label %39

5:                                                ; preds = %2
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [2 x ptr], ptr @fgJoystick, i64 0, i64 %7
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %35

11:                                               ; preds = %5
  %12 = load i32, ptr %1, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [2 x ptr], ptr @fgJoystick, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.tagSFG_Joystick, ptr %15, i32 0, i32 6
  %17 = load i8, ptr %16, align 4, !tbaa !11
  %18 = icmp ne i8 %17, 0
  br i1 %18, label %27, label %19

19:                                               ; preds = %11
  %20 = load i32, ptr %1, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [2 x ptr], ptr @fgJoystick, i64 0, i64 %21
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.tagSFG_Joystick, ptr %23, i32 0, i32 4
  %25 = load i32, ptr %24, align 4, !tbaa !15
  %26 = call i32 @close(i32 noundef %25)
  br label %27

27:                                               ; preds = %19, %11
  %28 = load i32, ptr %1, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [2 x ptr], ptr @fgJoystick, i64 0, i64 %29
  %31 = load ptr, ptr %30, align 8, !tbaa !9
  call void @free(ptr noundef %31) #4
  %32 = load i32, ptr %1, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [2 x ptr], ptr @fgJoystick, i64 0, i64 %33
  store ptr null, ptr %34, align 8, !tbaa !9
  br label %35

35:                                               ; preds = %27, %5
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %1, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %1, align 4, !tbaa !5
  br label %2, !llvm.loop !16

39:                                               ; preds = %2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret void
}

declare i32 @close(i32 noundef) #2

; Function Attrs: nounwind
declare void @free(ptr 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 = { "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 "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 212}
!12 = !{!"tagSFG_Joystick", !13, i64 0, !6, i64 8, !7, i64 12, !7, i64 76, !6, i64 204, !6, i64 208, !7, i64 212, !7, i64 213, !6, i64 344, !6, i64 348, !7, i64 352, !7, i64 416, !7, i64 480, !7, i64 544, !7, i64 608}
!13 = !{!"js_event", !6, i64 0, !14, i64 4, !7, i64 6, !7, i64 7}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !6, i64 204}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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

@fgJoystick = external hidden local_unnamed_addr global [2 x ptr], align 16

; Function Attrs: nounwind uwtable
define dso_local void @fgJoystickClose() local_unnamed_addr #0 {
  %1 = load ptr, ptr @fgJoystick, align 16, !tbaa !5
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %11, label %2

2:                                                ; preds = %0
  %3 = getelementptr inbounds i8, ptr %1, i64 212
  %4 = load i8, ptr %3, align 4, !tbaa !9
  %.not8 = icmp eq i8 %4, 0
  br i1 %.not8, label %5, label %9

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %1, i64 204
  %7 = load i32, ptr %6, align 4, !tbaa !14
  %8 = tail call i32 @close(i32 noundef %7) #3
  %.pre = load ptr, ptr @fgJoystick, align 16, !tbaa !5
  br label %9

9:                                                ; preds = %5, %2
  %10 = phi ptr [ %.pre, %5 ], [ %1, %2 ]
  tail call void @free(ptr noundef %10) #3
  store ptr null, ptr @fgJoystick, align 16, !tbaa !5
  br label %11

11:                                               ; preds = %0, %9
  %12 = load ptr, ptr getelementptr inbounds (i8, ptr @fgJoystick, i64 8), align 8, !tbaa !5
  %.not.1 = icmp eq ptr %12, null
  br i1 %.not.1, label %22, label %13

13:                                               ; preds = %11
  %14 = getelementptr inbounds i8, ptr %12, i64 212
  %15 = load i8, ptr %14, align 4, !tbaa !9
  %.not8.1 = icmp eq i8 %15, 0
  br i1 %.not8.1, label %16, label %20

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %12, i64 204
  %18 = load i32, ptr %17, align 4, !tbaa !14
  %19 = tail call i32 @close(i32 noundef %18) #3
  %.pre11 = load ptr, ptr getelementptr inbounds (i8, ptr @fgJoystick, i64 8), align 8, !tbaa !5
  br label %20

20:                                               ; preds = %16, %13
  %21 = phi ptr [ %.pre11, %16 ], [ %12, %13 ]
  tail call void @free(ptr noundef %21) #3
  store ptr null, ptr getelementptr inbounds (i8, ptr @fgJoystick, i64 8), align 8, !tbaa !5
  br label %22

22:                                               ; preds = %20, %11
  ret void
}

declare i32 @close(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture 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 = { "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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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, !7, i64 212}
!10 = !{!"tagSFG_Joystick", !11, i64 0, !12, i64 8, !7, i64 12, !7, i64 76, !12, i64 204, !12, i64 208, !7, i64 212, !7, i64 213, !12, i64 344, !12, i64 348, !7, i64 352, !7, i64 416, !7, i64 480, !7, i64 544, !7, i64 608}
!11 = !{!"js_event", !12, i64 0, !13, i64 4, !7, i64 6, !7, i64 7}
!12 = !{!"int", !7, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!10, !12, i64 204}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass