#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 get(char *, int *, int, int);
size_t getNext(int *, int);
scalar_t__ malloc(int);
char *getPermutation(int n, int k) {
  char *ret = (char *)malloc((n + 1) * sizeof(char));
  char charset[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
  bool taken[] = {false, false, false, false, false,
                  false, false, false, false};
  int x[] = {0, 1, 2, 6, 24, 120, 720, 5040, 40320};
  int div, mod = k - 1, next;
  for (int i = n - 1; i > 0; i--) {
    div = mod / x[i];
    next = get(charset, taken, div, n);
    taken[next] = true;
    ret[n - 1 - i] = charset[next];
    mod = mod % x[i];
  }
  ret[n - 1] = charset[getNext(taken, n)];
  ret[n] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<getPermutation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0xe81(%rip),%rax
mov    %rax,-0x19(%rbp)
mov    0xe7f(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x24,%edx
call   1030 <memset@plt>
lea    -0x70(%rbp),%rdi
lea    0xe69(%rip),%rsi
mov    $0x24,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x78(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x80(%rbp)
cmpl   $0x0,-0x80(%rbp)
jle    1236 <getPermutation+0xe6>
mov    -0x78(%rbp),%eax
movslq -0x80(%rbp),%rcx
cltd
idivl  -0x70(%rbp,%rcx,4)
mov    %eax,-0x74(%rbp)
lea    -0x19(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x74(%rbp),%edx
mov    -0x4(%rbp),%ecx
call   1060 <get@plt>
mov    %eax,-0x7c(%rbp)
movslq -0x7c(%rbp),%rax
movl   $0x1,-0x40(%rbp,%rax,4)
movslq -0x7c(%rbp),%rax
mov    -0x19(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x80(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x78(%rbp),%eax
movslq -0x80(%rbp),%rcx
cltd
idivl  -0x70(%rbp,%rcx,4)
mov    %edx,-0x78(%rbp)
mov    -0x80(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x80(%rbp)
jmp    11c3 <getPermutation+0x73>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1070 <getNext@plt>
mov    -0x19(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPermutation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x1(%rbx),%edi
call   1030 <malloc@plt>
mov    %rax,%r14
movabs $0x3837363534333231,%rax
36 37 38
mov    %rax,0x8(%rsp)
movb   $0x39,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movl   $0x0,0x50(%rsp)
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
jle    1201 <getPermutation+0xd1>
add    $0xffffffff,%ebp
mov    %ebx,%ecx
mov    %eax,%eax
lea    0x1(%rcx),%r13
mov    %rax,0x18(%rsp)
mov    %rcx,0x20(%rsp)
sub    %rcx,%rax
mov    %r14,0x28(%rsp)
add    %rax,%r14
add    $0x1,%r14
lea    0x8(%rsp),%r15
lea    0x30(%rsp),%r12
mov    %ebp,%eax
cltd
lea    0xe56(%rip),%rcx
idivl  -0x8(%rcx,%r13,4)
mov    %edx,%ebp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %eax,%edx
mov    %ebx,%ecx
call   1040 <get@plt>
cltq
movl   $0x1,0x30(%rsp,%rax,4)
movzbl 0x8(%rsp,%rax,1),%eax
mov    %al,(%r14)
add    $0xffffffffffffffff,%r13
add    $0x1,%r14
cmp    $0x2,%r13
ja     11b0 <getPermutation+0x80>
mov    0x28(%rsp),%r14
mov    0x20(%rsp),%r15
mov    0x18(%rsp),%rbp
jmp    1207 <getPermutation+0xd7>
movslq %eax,%rbp
movslq %ebx,%r15
lea    0x30(%rsp),%rdi
mov    %ebx,%esi
call   1050 <getNext@plt>
mov    0x8(%rsp,%rax,1),%al
mov    %al,(%r14,%rbp,1)
movb   $0x0,(%r14,%r15,1)
mov    %r14,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.getPermutation.charset = [9 x i8] c"123456789"
@__const.getPermutation.x = [9 x i32] [i32 0, i32 1, i32 2, i32 6, i32 24, i32 120, i32 720, i32 5040, i32 40320]
define i8* @getPermutation(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [9 x i8]
%7 = alloca [9 x i32]
%8 = alloca [9 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 1
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %5
%20 = bitcast [9 x i8]* %6 to i8*
%21 = bitcast [9 x i32]* %7 to i8*
%22 = bitcast [9 x i32]* %8 to i8*
%23 = load i32, i32* %4
%24 = sub nsw i32 %23, 1
store i32 %24, i32* %10
%25 = load i32, i32* %3
%26 = sub nsw i32 %25, 1
store i32 %26, i32* %12
br label %27
27: ; preds = %62, %2
%28 = load i32, i32* %12
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %65
30: ; preds = %27
%31 = load i32, i32* %10
%32 = load i32, i32* %12
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [9 x i32], [9 x i32]* %8, i64 0, i64 %33
%35 = load i32, i32* %34
%36 = sdiv i32 %31, %35
store i32 %36, i32* %9
%37 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%38 = getelementptr inbounds [9 x i32], [9 x i32]* %7, i64 0, i64 0
%39 = load i32, i32* %9
%40 = load i32, i32* %3
%41 = call i32 @get(i8* %37, i32* %38, i32 %39, i32 %40)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [9 x i32], [9 x i32]* %7, i64 0, i64 %43
store i32 1, i32* %44
%45 = load i32, i32* %11
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = load i8*, i8** %5
%50 = load i32, i32* %3
%51 = sub nsw i32 %50, 1
%52 = load i32, i32* %12
%53 = sub nsw i32 %51, %52
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %49, i64 %54
store i8 %48, i8* %55
%56 = load i32, i32* %10
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [9 x i32], [9 x i32]* %8, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = srem i32 %56, %60
store i32 %61, i32* %10
br label %62
62: ; preds = %30
%63 = load i32, i32* %12
%64 = add nsw i32 %63, -1
store i32 %64, i32* %12
br label %27
65: ; preds = %27
%66 = getelementptr inbounds [9 x i32], [9 x i32]* %7, i64 0, i64 0
%67 = load i32, i32* %3
%68 = call i64 @getNext(i32* %66, i32 %67)
%69 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 %68
%70 = load i8, i8* %69
%71 = load i8*, i8** %5
%72 = load i32, i32* %3
%73 = sub nsw i32 %72, 1
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %71, i64 %74
store i8 %70, i8* %75
%76 = load i8*, i8** %5
%77 = load i32, i32* %3
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
store i8 0, i8* %79
%80 = load i8*, i8** %5
ret i8* %80
}

LLVM IR (-O2):
@__const.getPermutation.charset = [9 x i8] c"123456789"
@__const.getPermutation.x = [9 x i32] [i32 0, i32 1, i32 2, i32 6, i32 24, i32 120, i32 720, i32 5040, i32 40320]
define i8* @getPermutation(i32 %0, i32 %1) {
%3 = alloca [9 x i8]
%4 = alloca [9 x i32]
%5 = add nsw i32 %0, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%9 = bitcast [9 x i32]* %4 to i8*
%10 = add nsw i32 %0, -1
%11 = icmp sgt i32 %0, 1
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = sext i32 %10 to i64
%14 = sext i32 %0 to i64
br label %20
15: ; preds = %2
%16 = add nsw i32 %1, -1
%17 = getelementptr inbounds [9 x i32], [9 x i32]* %4, i64 0, i64 0
%18 = zext i32 %0 to i64
%19 = zext i32 %10 to i64
br label %29
20: ; preds = %29, %12
%21 = phi i64 [ %14, %12 ], [ %18, %29 ]
%22 = phi i64 [ %13, %12 ], [ %19, %29 ]
%23 = getelementptr inbounds [9 x i32], [9 x i32]* %4, i64 0, i64 0
%24 = call i64 @getNext(i32* %23, i32 %0)
%25 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = getelementptr inbounds i8, i8* %7, i64 %22
store i8 %26, i8* %27
%28 = getelementptr inbounds i8, i8* %7, i64 %21
store i8 0, i8* %28
ret i8* %7
29: ; preds = %15, %29
%30 = phi i64 [ %18, %15 ], [ %32, %29 ]
%31 = phi i32 [ %16, %15 ], [ %43, %29 ]
%32 = add nsw i64 %30, -1
%33 = getelementptr inbounds [9 x i32], [9 x i32]* @__const.getPermutation.x, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = sdiv i32 %31, %34
%36 = call i32 @get(i8* %8, i32* %17, i32 %35, i32 %0)
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [9 x i32], [9 x i32]* %4, i64 0, i64 %37
store i32 1, i32* %38
%39 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 %37
%40 = load i8, i8* %39
%41 = sub nuw nsw i64 %19, %32
%42 = getelementptr inbounds i8, i8* %7, i64 %41
store i8 %40, i8* %42
%43 = srem i32 %31, %34
%44 = icmp ugt i64 %30, 2
br i1 %44, label %29, label %20
}