#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*/ sprintf(char *, char *, int);
int findNthDigit(int n) {
  long step[] = {0,       9,        180,       2700,       36000,      450000,
                 5400000, 63000000, 720000000, 8100000000, 90000000000};
  int digits[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int tens[] = {0,      1,       10,       100,       1000,      10000,
                100000, 1000000, 10000000, 100000000, 1000000000};
  char str[12];
  int i;
  long nn = n;
  for (i = 1; nn > 0; i++) {
    nn -= step[i];
  }
  i--;
  n = nn + step[i];
  n--;
  sprintf(str, "%d", n / digits[i] + tens[i]);
  return (int)(str[n % digits[i]] - '0');
}


x86 assembly (clang, -O0):
<findNthDigit>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xec7(%rip),%rsi
mov    $0x58,%edx
call   1030 <memcpy@plt>
lea    -0x90(%rbp),%rdi
lea    0xf0f(%rip),%rsi
mov    $0x2c,%edx
call   1030 <memcpy@plt>
lea    -0xc0(%rbp),%rdi
lea    0xf27(%rip),%rsi
mov    $0x2c,%edx
call   1030 <memcpy@plt>
movslq -0x4(%rbp),%rax
mov    %rax,-0xd8(%rbp)
movl   $0x1,-0xd0(%rbp)
cmpq   $0x0,-0xd8(%rbp)
jle    11c7 <findNthDigit+0xa7>
movslq -0xd0(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rcx
mov    -0xd8(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0xd8(%rbp)
mov    -0xd0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd0(%rbp)
jmp    1188 <findNthDigit+0x68>
mov    -0xd0(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xd0(%rbp)
mov    -0xd8(%rbp),%rax
movslq -0xd0(%rbp),%rcx
add    -0x60(%rbp,%rcx,8),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
lea    -0xcc(%rbp),%rdi
mov    -0x4(%rbp),%eax
movslq -0xd0(%rbp),%rcx
cltd
idivl  -0x90(%rbp,%rcx,4)
mov    %eax,%edx
movslq -0xd0(%rbp),%rax
add    -0xc0(%rbp,%rax,4),%edx
lea    0xe97(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x4(%rbp),%eax
movslq -0xd0(%rbp),%rcx
cltd
idivl  -0x90(%rbp,%rcx,4)
movslq %edx,%rax
movsbl -0xcc(%rbp,%rax,1),%eax
ff
sub    $0x30,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findNthDigit>:
push   %rbx
sub    $0x10,%rsp
movslq %edi,%rax
test   %eax,%eax
jle    1145 <findNthDigit+0x35>
lea    0xee5(%rip),%rdx
xor    %ecx,%ecx
movabs $0x100000000,%rsi
nop
add    %rsi,%rcx
lea    0x8(%rdx),%rdi
sub    (%rdx),%rax
mov    %rdi,%rdx
jg     1130 <findNthDigit+0x20>
sar    $0x20,%rcx
jmp    1147 <findNthDigit+0x37>
xor    %ecx,%ecx
lea    0xeb2(%rip),%rdx
mov    (%rdx,%rcx,8),%edx
add    %edx,%eax
add    $0xffffffff,%eax
lea    0xf03(%rip),%rsi
cltd
idivl  (%rsi,%rcx,4)
mov    %edx,%ebx
lea    0xf26(%rip),%rdx
add    (%rdx,%rcx,4),%eax
lea    0xf48(%rip),%rsi
lea    0x4(%rsp),%rdi
mov    %eax,%edx
call   1030 <sprintf@plt>
movslq %ebx,%rax
movsbl 0x4(%rsp,%rax,1),%eax
add    $0xffffffd0,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.findNthDigit.step = [11 x i64] [i64 0, i64 9, i64 180, i64 2700, i64 36000, i64 450000, i64 5400000, i64 63000000, i64 720000000, i64 8100000000, i64 90000000000]
@__const.findNthDigit.digits = [11 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10]
@__const.findNthDigit.tens = [11 x i32] [i32 0, i32 1, i32 10, i32 100, i32 1000, i32 10000, i32 100000, i32 1000000, i32 10000000, i32 100000000, i32 1000000000]
@.str = [3 x i8] c"%d\00"
define i32 @findNthDigit(i32 %0) {
%2 = alloca i32
%3 = alloca [11 x i64]
%4 = alloca [11 x i32]
%5 = alloca [11 x i32]
%6 = alloca [12 x i8]
%7 = alloca i32
%8 = alloca i64
store i32 %0, i32* %2
%9 = bitcast [11 x i64]* %3 to i8*
%10 = bitcast [11 x i32]* %4 to i8*
%11 = bitcast [11 x i32]* %5 to i8*
%12 = load i32, i32* %2
%13 = sext i32 %12 to i64
store i64 %13, i64* %8
store i32 1, i32* %7
br label %14
14: ; preds = %24, %1
%15 = load i64, i64* %8
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [11 x i64], [11 x i64]* %3, i64 0, i64 %19
%21 = load i64, i64* %20
%22 = load i64, i64* %8
%23 = sub nsw i64 %22, %21
store i64 %23, i64* %8
br label %24
24: ; preds = %17
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %14
27: ; preds = %14
%28 = load i32, i32* %7
%29 = add nsw i32 %28, -1
store i32 %29, i32* %7
%30 = load i64, i64* %8
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [11 x i64], [11 x i64]* %3, i64 0, i64 %32
%34 = load i64, i64* %33
%35 = add nsw i64 %30, %34
%36 = trunc i64 %35 to i32
store i32 %36, i32* %2
%37 = load i32, i32* %2
%38 = add nsw i32 %37, -1
store i32 %38, i32* %2
%39 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 0
%40 = load i32, i32* %2
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [11 x i32], [11 x i32]* %4, i64 0, i64 %42
%44 = load i32, i32* %43
%45 = sdiv i32 %40, %44
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [11 x i32], [11 x i32]* %5, i64 0, i64 %47
%49 = load i32, i32* %48
%50 = add nsw i32 %45, %49
%51 = call i32 @sprintf(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %2
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [11 x i32], [11 x i32]* %4, i64 0, i64 %54
%56 = load i32, i32* %55
%57 = srem i32 %52, %56
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = sub nsw i32 %61, 48
ret i32 %62
}

LLVM IR (-O2):
@__const.findNthDigit.step = [11 x i64] [i64 0, i64 9, i64 180, i64 2700, i64 36000, i64 450000, i64 5400000, i64 63000000, i64 720000000, i64 8100000000, i64 90000000000]
@__const.findNthDigit.digits = [11 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10]
@__const.findNthDigit.tens = [11 x i32] [i32 0, i32 1, i32 10, i32 100, i32 1000, i32 10000, i32 100000, i32 1000000, i32 10000000, i32 100000000, i32 1000000000]
@.str = [3 x i8] c"%d\00"
define i32 @findNthDigit(i32 %0) {
%2 = alloca [12 x i8]
%3 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%4 = sext i32 %0 to i64
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %17
6: ; preds = %1, %6
%7 = phi i64 [ %12, %6 ], [ 1, %1 ]
%8 = phi i64 [ %11, %6 ], [ %4, %1 ]
%9 = getelementptr inbounds [11 x i64], [11 x i64]* @__const.findNthDigit.step, i64 0, i64 %7
%10 = load i64, i64* %9
%11 = sub nsw i64 %8, %10
%12 = add nuw i64 %7, 1
%13 = icmp sgt i64 %11, 0
br i1 %13, label %6, label %14
14: ; preds = %6
%15 = shl i64 %7, 32
%16 = ashr exact i64 %15, 32
br label %17
17: ; preds = %14, %1
%18 = phi i64 [ 0, %1 ], [ %16, %14 ]
%19 = phi i64 [ %4, %1 ], [ %11, %14 ]
%20 = getelementptr inbounds [11 x i64], [11 x i64]* @__const.findNthDigit.step, i64 0, i64 %18
%21 = load i64, i64* %20
%22 = add nsw i64 %21, %19
%23 = trunc i64 %22 to i32
%24 = add nsw i32 %23, -1
%25 = getelementptr inbounds [11 x i32], [11 x i32]* @__const.findNthDigit.digits, i64 0, i64 %18
%26 = load i32, i32* %25
%27 = sdiv i32 %24, %26
%28 = getelementptr inbounds [11 x i32], [11 x i32]* @__const.findNthDigit.tens, i64 0, i64 %18
%29 = load i32, i32* %28
%30 = add nsw i32 %29, %27
%31 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = srem i32 %24, %26
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = add nsw i32 %36, -48
ret i32 %37
}