void nth_element(char *search_in, char delimiter, int n, char *substring) {
  int start, end;
  start = nth_pos(search_in, delimiter, n) + 1;
  end = nth_pos(search_in, delimiter, n + 1);
  sprintf(substring, "%.*s", end - start, search_in + start);
}


x86 assembly (clang, -O0):
<nth_element>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <nth_pos@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
mov    $0x0,%al
call   1030 <nth_pos@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%rcx
movslq -0x1c(%rbp),%rax
add    %rax,%rcx
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nth_element>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r12
xor    %eax,%eax
call   1030 <nth_pos@plt>
mov    %eax,%ebx
add    $0x1,%ebp
mov    %r12,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <nth_pos@plt>
movslq %ebx,%rcx
not    %ebx
add    %eax,%ebx
add    %r12,%rcx
add    $0x1,%rcx
lea    0xe9d(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <sprintf@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%.*s\00"
define void @nth_element(i8* %0, i8 signext %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8 %1, i8* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%11 = load i8*, i8** %5
%12 = load i8, i8* %6
%13 = sext i8 %12 to i32
%14 = load i32, i32* %7
%15 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @nth_pos to i32 (i8*, i32, i32, ...)*)(i8* %11, i32 %13, i32 %14)
%16 = add nsw i32 %15, 1
store i32 %16, i32* %9
%17 = load i8*, i8** %5
%18 = load i8, i8* %6
%19 = sext i8 %18 to i32
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 1
%22 = call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @nth_pos to i32 (i8*, i32, i32, ...)*)(i8* %17, i32 %19, i32 %21)
store i32 %22, i32* %10
%23 = load i8*, i8** %8
%24 = load i32, i32* %10
%25 = load i32, i32* %9
%26 = sub nsw i32 %24, %25
%27 = load i8*, i8** %5
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = call i32 (i8*, i8*, ...) @sprintf(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %26, i8* %30)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%.*s\00"
define void @nth_element(i8* %0, i8 signext %1, i32 %2, i8* nocapture writeonly %3) {
%5 = sext i8 %1 to i32
%6 = tail call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @nth_pos to i32 (i8*, i32, i32, ...)*)(i8* %0, i32 %5, i32 %2)
%7 = add nsw i32 %6, 1
%8 = add nsw i32 %2, 1
%9 = tail call i32 (i8*, i32, i32, ...) bitcast (i32 (...)* @nth_pos to i32 (i8*, i32, i32, ...)*)(i8* %0, i32 %5, i32 %8)
%10 = sub nsw i32 %9, %7
%11 = sext i32 %7 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %12)
ret void
}