#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*/ qsort(int *, int /*<<< orphan*/, int);
int fn(int i) {
  int arr[4];
  arr[0] = i / 1000;
  arr[1] = (i % 1000) / 100;
  arr[2] = (i % 100) / 10;
  arr[3] = i % 10;
  qsort(arr, 0, 3);
  return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[1];
}


x86 assembly (clang, -O0):
<fn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1030 <qsort@plt>
imul   $0x3e8,-0x20(%rbp),%eax
imul   $0x64,-0x1c(%rbp),%ecx
add    %ecx,%eax
imul   $0xa,-0x18(%rbp),%ecx
add    %ecx,%eax
add    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fn>:
sub    $0x18,%rsp
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x3e8,%ecx,%ecx
mov    %eax,%edx
sub    %ecx,%edx
movswl %dx,%ecx
imul   $0x147b,%ecx,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x13,%ecx
add    %edx,%ecx
mov    %ecx,0x4(%rsp)
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
mov    %eax,%edx
sub    %ecx,%edx
movsbl %dl,%ecx
imul   $0x67,%ecx,%ecx
movzwl %cx,%ecx
movswl %cx,%edx
shr    $0xf,%ecx
shr    $0xa,%edx
add    %cl,%dl
movsbl %dl,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %eax,0xc(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1030 <qsort@plt>
imul   $0x3e8,(%rsp),%eax
mov    0x4(%rsp),%ecx
mov    0x8(%rsp),%edx
imul   $0x64,%ecx,%esi
lea    (%rdx,%rdx,4),%edx
add    %ecx,%eax
add    %esi,%eax
lea    (%rax,%rdx,2),%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
define i32 @fn(i32 %0) {
%2 = alloca i32
%3 = alloca [4 x i32]
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = sdiv i32 %4, 1000
%6 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
store i32 %5, i32* %6
%7 = load i32, i32* %2
%8 = srem i32 %7, 1000
%9 = sdiv i32 %8, 100
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
store i32 %9, i32* %10
%11 = load i32, i32* %2
%12 = srem i32 %11, 100
%13 = sdiv i32 %12, 10
%14 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
store i32 %13, i32* %14
%15 = load i32, i32* %2
%16 = srem i32 %15, 10
%17 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
store i32 %16, i32* %17
%18 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%19 = call i32 @qsort(i32* %18, i32 0, i32 3)
%20 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, 1000
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%24 = load i32, i32* %23
%25 = mul nsw i32 %24, 100
%26 = add nsw i32 %22, %25
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%28 = load i32, i32* %27
%29 = mul nsw i32 %28, 10
%30 = add nsw i32 %26, %29
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = add nsw i32 %30, %32
ret i32 %33
}

LLVM IR (-O2):
define i32 @fn(i32 %0) {
%2 = alloca [4 x i32]
%3 = bitcast [4 x i32]* %2 to i8*
%4 = sdiv i32 %0, 1000
%5 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
store i32 %4, i32* %5
%6 = srem i32 %0, 1000
%7 = trunc i32 %6 to i16
%8 = sdiv i16 %7, 100
%9 = sext i16 %8 to i32
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
store i32 %9, i32* %10
%11 = srem i32 %0, 100
%12 = trunc i32 %11 to i8
%13 = sdiv i8 %12, 10
%14 = sext i8 %13 to i32
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
store i32 %14, i32* %15
%16 = srem i32 %0, 10
%17 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
store i32 %16, i32* %17
%18 = call i32 @qsort(i32* %5, i32 0, i32 3)
%19 = load i32, i32* %5
%20 = mul nsw i32 %19, 1000
%21 = load i32, i32* %10
%22 = mul nsw i32 %21, 100
%23 = load i32, i32* %15
%24 = mul nsw i32 %23, 10
%25 = add i32 %21, %20
%26 = add i32 %25, %22
%27 = add i32 %26, %24
ret i32 %27
}