#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*/ strncpy(char *, char *, int);
char *strtok(char *, char *);
int get_ith_split(char *string, char *delimiter, int ith, char *str_result) {
  int i;
  char *pch;
  pch = strtok(string, delimiter);
  if (pch == NULL)
    return -1;
  for (i = ith; i > 0; i--) {
    pch = strtok(NULL, delimiter);
    if (pch == NULL)
      return -1;
  }
  strncpy(str_result, pch, 100);
  return 0;
}


x86 assembly (clang, -O0):
<get_ith_split>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    115f <get_ith_split+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <get_ith_split+0xa3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11aa <get_ith_split+0x8a>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1197 <get_ith_split+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <get_ith_split+0xa3>
jmp    119c <get_ith_split+0x7c>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    1165 <get_ith_split+0x45>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    $0x64,%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ith_split>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebx
mov    %rsi,%rbp
call   1040 <strtok@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     117a <get_ith_split+0x5a>
test   %ebx,%ebx
jle    1167 <get_ith_split+0x47>
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbp,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
je     117a <get_ith_split+0x5a>
add    $0xffffffff,%ebx
cmp    $0x2,%ebx
jge    1150 <get_ith_split+0x30>
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x64,%edx
call   1030 <strncpy@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_ith_split(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%12 = load i8*, i8** %6
%13 = load i8*, i8** %7
%14 = call i8* @strtok(i8* %12, i8* %13)
store i8* %14, i8** %11
%15 = load i8*, i8** %11
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %37
18: ; preds = %4
%19 = load i32, i32* %8
store i32 %19, i32* %10
br label %20
20: ; preds = %30, %18
%21 = load i32, i32* %10
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i8*, i8** %7
%25 = call i8* @strtok(i8* null, i8* %24)
store i8* %25, i8** %11
%26 = load i8*, i8** %11
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %5
br label %37
29: ; preds = %23
br label %30
30: ; preds = %29
%31 = load i32, i32* %10
%32 = add nsw i32 %31, -1
store i32 %32, i32* %10
br label %20
33: ; preds = %20
%34 = load i8*, i8** %9
%35 = load i8*, i8** %11
%36 = call i32 @strncpy(i8* %34, i8* %35, i32 100)
store i32 0, i32* %5
br label %37
37: ; preds = %33, %28, %17
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
define i32 @get_ith_split(i8* %0, i8* nocapture %1, i32 %2, i8* %3) {
%5 = tail call i8* @strtok(i8* %0, i8* %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %19, label %7
7: ; preds = %4
%8 = icmp sgt i32 %2, 0
br i1 %8, label %12, label %16
9: ; preds = %12
%10 = add nsw i32 %13, -1
%11 = icmp sgt i32 %13, 1
br i1 %11, label %12, label %16
12: ; preds = %7, %9
%13 = phi i32 [ %10, %9 ], [ %2, %7 ]
%14 = tail call i8* @strtok(i8* null, i8* %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %19, label %9
16: ; preds = %9, %7
%17 = phi i8* [ %5, %7 ], [ %14, %9 ]
%18 = tail call i32 @strncpy(i8* %3, i8* %17, i32 100)
br label %19
19: ; preds = %12, %4, %16
%20 = phi i32 [ 0, %16 ], [ -1, %4 ], [ -1, %12 ]
ret i32 %20
}