#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 */
scalar_t__ digit_at(int, int);
scalar_t__ log10(int);
int /*<<< orphan*/ printf(char *, int);
int swap(int, int, int);
int next_permutation(int n) {
  int cursor;
  int len = (int)log10(n) + 1;
  for (cursor = 0;
       cursor < len && digit_at(n, cursor) <= digit_at(n, cursor + 1); cursor++)
    ;
  printf("%d\n", n);
  printf("%d\n", cursor);
  if (cursor == len - 1)
    return swap(n, len - 2, len - 1);
}


x86 assembly (clang, -O0):
<next_permutation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <log10@plt>
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
xor    %eax,%eax
cmp    -0x10(%rbp),%ecx
mov    %al,-0x11(%rbp)
jge    119e <next_permutation+0x5e>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <digit_at@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1060 <digit_at@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
setle  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    11ae <next_permutation+0x6e>
jmp    11c1 <next_permutation+0x81>
jmp    11b3 <next_permutation+0x73>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1160 <next_permutation+0x20>
mov    -0x8(%rbp),%esi
lea    0xe35(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe26(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1207 <next_permutation+0xc7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
sub    $0x2,%esi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
call   1050 <swap@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_permutation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
call   1040 <log10@plt>
mov    %rax,%r14
test   %r14d,%r14d
mov    $0xffffffff,%ebx
mov    $0xffffffff,%r13d
cmovns %r14d,%r13d
lea    0x1(%r13),%eax
mov    %eax,0x4(%rsp)
cmp    %ebx,%r13d
je     119b <next_permutation+0x5b>
lea    0x1(%rbx),%ebp
mov    %r15d,%edi
mov    %ebp,%esi
call   1060 <digit_at@plt>
mov    %rax,%r12
add    $0x2,%ebx
mov    %r15d,%edi
mov    %ebx,%esi
call   1060 <digit_at@plt>
mov    %ebp,%ebx
cmp    %rax,%r12
jle    1170 <next_permutation+0x30>
jmp    119f <next_permutation+0x5f>
mov    0x4(%rsp),%ebp
lea    0xe5a(%rip),%rbx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
cmp    %r14d,%ebp
jne    11dd <next_permutation+0x9d>
lea    -0x1(%r14),%esi
mov    %r15d,%edi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <swap@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
define i32 @next_permutation(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i64 @log10(i32 %6)
%8 = trunc i64 %7 to i32
%9 = add nsw i32 %8, 1
store i32 %9, i32* %5
store i32 0, i32* %4
br label %10
10: ; preds = %26, %1
%11 = load i32, i32* %4
%12 = load i32, i32* %5
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %23
14: ; preds = %10
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = call i64 @digit_at(i32 %15, i32 %16)
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
%21 = call i64 @digit_at(i32 %18, i32 %20)
%22 = icmp sle i64 %17, %21
br label %23
23: ; preds = %14, %10
%24 = phi i1 [ false, %10 ], [ %22, %14 ]
br i1 %24, label %25, label %29
25: ; preds = %23
br label %26
26: ; preds = %25
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %10
29: ; preds = %23
%30 = load i32, i32* %3
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %4
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %4
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, 1
%37 = icmp eq i32 %34, %36
br i1 %37, label %38, label %45
38: ; preds = %29
%39 = load i32, i32* %3
%40 = load i32, i32* %5
%41 = sub nsw i32 %40, 2
%42 = load i32, i32* %5
%43 = sub nsw i32 %42, 1
%44 = call i32 @swap(i32 %39, i32 %41, i32 %43)
store i32 %44, i32* %2
br label %45
45: ; preds = %38, %29
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define i32 @next_permutation(i32 %0) {
%2 = tail call i64 @log10(i32 %0)
%3 = trunc i64 %2 to i32
%4 = call i32 @llvm.smax.i32(i32 %3, i32 -1)
%5 = add i32 %4, 1
br label %6
6: ; preds = %9, %1
%7 = phi i32 [ 0, %1 ], [ %11, %9 ]
%8 = icmp eq i32 %7, %5
br i1 %8, label %14, label %9
9: ; preds = %6
%10 = tail call i64 @digit_at(i32 %0, i32 %7)
%11 = add nuw i32 %7, 1
%12 = tail call i64 @digit_at(i32 %0, i32 %11)
%13 = icmp sgt i64 %10, %12
br i1 %13, label %14, label %6
14: ; preds = %6, %9
%15 = phi i32 [ %5, %6 ], [ %7, %9 ]
%16 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %15)
%18 = icmp eq i32 %15, %3
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = add nsw i32 %3, -1
%21 = tail call i32 @swap(i32 %0, i32 %20, i32 %3)
br label %22
22: ; preds = %14, %19
%23 = phi i32 [ %21, %19 ], [ undef, %14 ]
ret i32 %23
}