#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ build_heap(int *, int);
int /*<<< orphan*/ modify_heap(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ swap(int *, int *);
void find_min_n(int a[], int n, int k) {
  build_heap(a, k);
  int i = k;
  for (; i < n; i++) {
    if (a[i] < a[0]) {
      swap(a, a + i);
      modify_heap(a, k, 0);
    }
  }
}


x86 assembly (clang, -O0):
<find_min_n>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <build_heap@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b0 <find_min_n+0x80>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jge    119d <find_min_n+0x6d>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1030 <modify_heap@plt>
jmp    11a2 <find_min_n+0x72>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1154 <find_min_n+0x24>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_min_n>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
mov    %edx,%esi
call   1040 <build_heap@plt>
cmp    %ebp,%r14d
jge    118d <find_min_n+0x5d>
movslq %r14d,%rax
lea    (%r15,%rax,4),%rbx
jmp    116c <find_min_n+0x3c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x4,%rbx
add    $0xffffffff,%ebp
cmp    %ebp,%r14d
je     118d <find_min_n+0x5d>
mov    (%rbx),%eax
cmp    (%r15),%eax
jge    1160 <find_min_n+0x30>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <swap@plt>
mov    %r15,%rdi
mov    %r14d,%esi
xor    %edx,%edx
call   1030 <modify_heap@plt>
jmp    1160 <find_min_n+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @find_min_n(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i32*, i32** %4
%9 = load i32, i32* %6
%10 = call i32 @build_heap(i32* %8, i32 %9)
%11 = load i32, i32* %6
store i32 %11, i32* %7
br label %12
12: ; preds = %37, %3
%13 = load i32, i32* %7
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %40
16: ; preds = %12
%17 = load i32*, i32** %4
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32, i32* %20
%22 = load i32*, i32** %4
%23 = getelementptr inbounds i32, i32* %22, i64 0
%24 = load i32, i32* %23
%25 = icmp slt i32 %21, %24
br i1 %25, label %26, label %36
26: ; preds = %16
%27 = load i32*, i32** %4
%28 = load i32*, i32** %4
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = call i32 @swap(i32* %27, i32* %31)
%33 = load i32*, i32** %4
%34 = load i32, i32* %6
%35 = call i32 @modify_heap(i32* %33, i32 %34, i32 0)
br label %36
36: ; preds = %26, %16
br label %37
37: ; preds = %36
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %12
40: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @find_min_n(i32* %0, i32 %1, i32 %2) {
%4 = tail call i32 @build_heap(i32* %0, i32 %2)
%5 = icmp slt i32 %2, %1
br i1 %5, label %6, label %21
6: ; preds = %3
%7 = sext i32 %2 to i64
br label %8
8: ; preds = %6, %17
%9 = phi i64 [ %7, %6 ], [ %18, %17 ]
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = load i32, i32* %0
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @swap(i32* %0, i32* %10)
%16 = tail call i32 @modify_heap(i32* %0, i32 %2, i32 0)
br label %17
17: ; preds = %8, %14
%18 = add nsw i64 %9, 1
%19 = trunc i64 %18 to i32
%20 = icmp eq i32 %19, %1
br i1 %20, label %21, label %8
21: ; preds = %17, %3
ret void
}