; 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.Fmt = type { i8, ptr, ptr, ptr, ptr, ptr, i32, [1 x %struct.__va_list_tag], i32, i32, i32, i64, ptr, ptr, ptr }
%struct.__va_list_tag = type { i32, i32, ptr, ptr }
%struct.XKeyEvent = type { i32, i64, i32, ptr, i64, i64, i64, i64, i32, i32, i32, i32, i32, i32, i32 }

@modkey_names = external dso_local global [9 x ptr], align 16
@display = external global ptr, 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
define dso_local i32 @fmtkey(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.Fmt, ptr %6, i32 0, i32 7
  %8 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  %9 = getelementptr inbounds %struct.__va_list_tag, ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 8
  %11 = icmp ule i32 %10, 40
  br i1 %11, label %12, label %17

12:                                               ; preds = %1
  %13 = getelementptr inbounds %struct.__va_list_tag, ptr %8, i32 0, i32 3
  %14 = load ptr, ptr %13, align 8
  %15 = getelementptr i8, ptr %14, i32 %10
  %16 = add i32 %10, 8
  store i32 %16, ptr %9, align 8
  br label %21

17:                                               ; preds = %1
  %18 = getelementptr inbounds %struct.__va_list_tag, ptr %8, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8
  %20 = getelementptr i8, ptr %19, i32 8
  store ptr %20, ptr %18, align 8
  br label %21

21:                                               ; preds = %17, %12
  %22 = phi ptr [ %15, %12 ], [ %19, %17 ]
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  store ptr %23, ptr %3, align 8, !tbaa !5
  %24 = load ptr, ptr @display, align 8, !tbaa !5
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.XKeyEvent, ptr %25, i32 0, i32 13
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = trunc i32 %27 to i8
  %29 = call i64 @XkbKeycodeToKeysym(ptr noundef %24, i8 noundef zeroext %28, i32 noundef 0, i32 noundef 0)
  %30 = call ptr @XKeysymToString(i64 noundef %29)
  store ptr %30, ptr %4, align 8, !tbaa !5
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Fmt, ptr %31, i32 0, i32 6
  %33 = load i32, ptr %32, align 8, !tbaa !13
  store i32 %33, ptr %5, align 4, !tbaa !15
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.XKeyEvent, ptr %35, i32 0, i32 12
  %37 = load i32, ptr %36, align 8, !tbaa !16
  %38 = zext i32 %37 to i64
  %39 = call i32 @unmask(ptr noundef %34, i64 noundef %38, ptr noundef @modkey_names, i64 noundef 45)
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Fmt, ptr %40, i32 0, i32 6
  %42 = load i32, ptr %41, align 8, !tbaa !13
  %43 = load i32, ptr %5, align 4, !tbaa !15
  %44 = icmp sgt i32 %42, %43
  br i1 %44, label %45, label %48

45:                                               ; preds = %21
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = call i32 @fmtrune(ptr noundef %46, i32 noundef 45)
  br label %48

48:                                               ; preds = %45, %21
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = call i32 @fmtstrcpy(ptr noundef %49, ptr noundef %50)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i32 %51
}

declare ptr @XKeysymToString(i64 noundef) #2

declare i64 @XkbKeycodeToKeysym(ptr noundef, i8 noundef zeroext, i32 noundef, i32 noundef) #2

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

declare i32 @fmtrune(ptr noundef, i32 noundef) #2

declare i32 @fmtstrcpy(ptr noundef, 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 84}
!10 = !{!"", !11, i64 0, !12, i64 8, !11, i64 16, !6, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !11, i64 64, !11, i64 68, !11, i64 72, !11, i64 76, !11, i64 80, !11, i64 84, !11, i64 88}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !11, i64 48}
!14 = !{!"Fmt", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !11, i64 48, !7, i64 56, !11, i64 80, !11, i64 84, !11, i64 88, !12, i64 96, !6, i64 104, !6, i64 112, !6, i64 120}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 80}


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

@modkey_names = external dso_local global [9 x ptr], align 16
@display = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @fmtkey(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 56
  %3 = load i32, ptr %2, align 8
  %4 = icmp ult i32 %3, 41
  br i1 %4, label %5, label %11

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 72
  %7 = load ptr, ptr %6, align 8
  %8 = zext nneg i32 %3 to i64
  %9 = getelementptr i8, ptr %7, i64 %8
  %10 = add nuw nsw i32 %3, 8
  store i32 %10, ptr %2, align 8
  br label %15

11:                                               ; preds = %1
  %12 = getelementptr inbounds i8, ptr %0, i64 64
  %13 = load ptr, ptr %12, align 8
  %14 = getelementptr i8, ptr %13, i64 8
  store ptr %14, ptr %12, align 8
  br label %15

15:                                               ; preds = %11, %5
  %16 = phi ptr [ %9, %5 ], [ %13, %11 ]
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = load ptr, ptr @display, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %17, i64 84
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = trunc i32 %20 to i8
  %22 = tail call i64 @XkbKeycodeToKeysym(ptr noundef %18, i8 noundef zeroext %21, i32 noundef 0, i32 noundef 0) #2
  %23 = tail call ptr @XKeysymToString(i64 noundef %22) #2
  %24 = getelementptr inbounds i8, ptr %0, i64 48
  %25 = load i32, ptr %24, align 8, !tbaa !13
  %26 = getelementptr inbounds i8, ptr %17, i64 80
  %27 = load i32, ptr %26, align 8, !tbaa !15
  %28 = zext i32 %27 to i64
  %29 = tail call i32 @unmask(ptr noundef nonnull %0, i64 noundef %28, ptr noundef nonnull @modkey_names, i64 noundef 45) #2
  %30 = load i32, ptr %24, align 8, !tbaa !13
  %31 = icmp sgt i32 %30, %25
  br i1 %31, label %32, label %34

32:                                               ; preds = %15
  %33 = tail call i32 @fmtrune(ptr noundef nonnull %0, i32 noundef 45) #2
  br label %34

34:                                               ; preds = %32, %15
  %35 = tail call i32 @fmtstrcpy(ptr noundef nonnull %0, ptr noundef %23) #2
  ret i32 %35
}

declare ptr @XKeysymToString(i64 noundef) local_unnamed_addr #1

declare i64 @XkbKeycodeToKeysym(ptr noundef, i8 noundef zeroext, i32 noundef, i32 noundef) local_unnamed_addr #1

declare i32 @unmask(ptr noundef, i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare i32 @fmtrune(ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @fmtstrcpy(ptr noundef, 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 84}
!10 = !{!"", !11, i64 0, !12, i64 8, !11, i64 16, !6, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !11, i64 64, !11, i64 68, !11, i64 72, !11, i64 76, !11, i64 80, !11, i64 84, !11, i64 88}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !11, i64 48}
!14 = !{!"Fmt", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !11, i64 48, !7, i64 56, !11, i64 80, !11, i64 84, !11, i64 88, !12, i64 96, !6, i64 104, !6, i64 112, !6, i64 120}
!15 = !{!10, !11, i64 80}


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