; LLVM IR from 0intro/wmii :: lib/libstuff/x11/keys (parsekey)

; 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.XModifierKeymap = type { i32, ptr }

@numlockmask.masks = external hidden global [8 x i32], align 16
@display = external global ptr, align 8
@.str.8 = external hidden unnamed_addr constant [9 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

; 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 i32 @numlockmask() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i8, align 1
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  %6 = load ptr, ptr @display, align 8, !tbaa !9
  %7 = call ptr @XGetModifierMapping(ptr noundef %6)
  store ptr %7, ptr %1, align 8, !tbaa !9
  %8 = call zeroext i8 @keycode(ptr noundef @.str.8)
  store i8 %8, ptr %2, align 1, !tbaa !11
  %9 = load i8, ptr %2, align 1, !tbaa !11
  %10 = zext i8 %9 to i32
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %63

12:                                               ; preds = %0
  %13 = load ptr, ptr %1, align 8, !tbaa !9
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %63

15:                                               ; preds = %12
  %16 = load ptr, ptr %1, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.XModifierKeymap, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !12
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %20, label %63

20:                                               ; preds = %15
  %21 = load ptr, ptr %1, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.XModifierKeymap, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !12
  %24 = sext i32 %23 to i64
  %25 = mul i64 8, %24
  %26 = trunc i64 %25 to i32
  store i32 %26, ptr %4, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %59, %20
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %31, label %35

31:                                               ; preds = %27
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = icmp ne i32 %32, 0
  %34 = xor i1 %33, true
  br label %35

35:                                               ; preds = %31, %27
  %36 = phi i1 [ false, %27 ], [ %34, %31 ]
  br i1 %36, label %37, label %62

37:                                               ; preds = %35
  %38 = load ptr, ptr %1, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.XModifierKeymap, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i8, ptr %40, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !11
  %45 = zext i8 %44 to i32
  %46 = load i8, ptr %2, align 1, !tbaa !11
  %47 = zext i8 %46 to i32
  %48 = icmp eq i32 %45, %47
  br i1 %48, label %49, label %58

49:                                               ; preds = %37
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = load ptr, ptr %1, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.XModifierKeymap, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !12
  %54 = sdiv i32 %50, %53
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [8 x i32], ptr @numlockmask.masks, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  store i32 %57, ptr %5, align 4, !tbaa !5
  br label %58

58:                                               ; preds = %49, %37
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %3, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %3, align 4, !tbaa !5
  br label %27, !llvm.loop !15

62:                                               ; preds = %35
  br label %63

63:                                               ; preds = %62, %15, %12, %0
  %64 = load ptr, ptr %1, align 8, !tbaa !9
  %65 = call i32 @XFreeModifiermap(ptr noundef %64)
  %66 = load i32, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret i32 %66
}

declare ptr @XGetModifierMapping(ptr noundef) #2

declare zeroext i8 @keycode(ptr noundef) #2

declare i32 @XFreeModifiermap(ptr noundef) #2

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 = !{!"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 = !{!7, !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"", !6, i64 0, !10, i64 8}
!14 = !{!13, !10, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@numlockmask.masks = external hidden local_unnamed_addr global [8 x i32], align 16
@display = external local_unnamed_addr global ptr, align 8
@.str.8 = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @numlockmask() local_unnamed_addr #0 {
  %1 = load ptr, ptr @display, align 8, !tbaa !5
  %2 = tail call ptr @XGetModifierMapping(ptr noundef %1) #2
  %3 = tail call zeroext i8 @keycode(ptr noundef nonnull @.str.8) #2
  %4 = icmp ne i8 %3, 0
  %5 = icmp ne ptr %2, null
  %or.cond = select i1 %4, i1 %5, i1 false
  br i1 %or.cond, label %6, label %.loopexit

6:                                                ; preds = %0
  %7 = load i32, ptr %2, align 8, !tbaa !9
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %9, label %.loopexit

9:                                                ; preds = %6
  %10 = shl i32 %7, 3
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %9
  %12 = getelementptr inbounds i8, ptr %2, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %14 = zext nneg i32 %10 to i64
  br label %15

15:                                               ; preds = %.lr.ph, %25
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %25 ]
  %16 = getelementptr inbounds i8, ptr %13, i64 %indvars.iv
  %17 = load i8, ptr %16, align 1, !tbaa !13
  %18 = icmp eq i8 %17, %3
  br i1 %18, label %19, label %25

19:                                               ; preds = %15
  %20 = trunc nuw nsw i64 %indvars.iv to i32
  %21 = udiv i32 %20, %7
  %22 = zext nneg i32 %21 to i64
  %23 = getelementptr inbounds [8 x i32], ptr @numlockmask.masks, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !14
  br label %25

25:                                               ; preds = %15, %19
  %.1 = phi i32 [ %24, %19 ], [ 0, %15 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = icmp ult i64 %indvars.iv.next, %14
  %.not = icmp eq i32 %.1, 0
  %27 = select i1 %26, i1 %.not, i1 false
  br i1 %27, label %15, label %.loopexit, !llvm.loop !15

.loopexit:                                        ; preds = %25, %9, %6, %0
  %.2 = phi i32 [ 0, %6 ], [ 0, %0 ], [ 0, %9 ], [ %.1, %25 ]
  %28 = tail call i32 @XFreeModifiermap(ptr noundef %2) #2
  ret i32 %.2
}

declare ptr @XGetModifierMapping(ptr noundef) local_unnamed_addr #1

declare zeroext i8 @keycode(ptr noundef) local_unnamed_addr #1

declare i32 @XFreeModifiermap(ptr noundef) local_unnamed_addr #1

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 = { 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, !11, i64 0}
!10 = !{!"", !11, i64 0, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!7, !7, i64 0}
!14 = !{!11, !11, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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