#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*/ free(char *);
char *malloc(long);
int occurence(char, char *);
int string_pos(char *, char *);
long strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int nth_delimiter(char *search_for, char *search_in, char delimiter) {
  char buf[10000];
  int pos;
  long len = strlen(search_for);
  int i;
  char *search_for_pipe = malloc(len + 1 + 1);
  search_for_pipe[0] = delimiter;
  for (i = 1; i < len + 1; i++) {
    search_for_pipe[i] = search_for[i - 1];
  }
  search_for_pipe[i + 1] = '\0';
  pos = string_pos(search_for_pipe, search_in);
  strncpy(buf, search_in, pos);
  free(search_for_pipe);
  return occurence(delimiter, buf) + 1;
}


x86 assembly (clang, -O0):
<nth_delimiter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2750,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x2740(%rbp)
mov    -0x2740(%rbp),%rdi
add    $0x1,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x2750(%rbp)
mov    -0x11(%rbp),%cl
mov    -0x2750(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x2744(%rbp)
movslq -0x2744(%rbp),%rax
mov    -0x2740(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jge    120c <nth_delimiter+0xac>
mov    -0x8(%rbp),%rax
mov    -0x2744(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x2750(%rbp),%rax
movslq -0x2744(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2744(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2744(%rbp)
jmp    11b9 <nth_delimiter+0x59>
mov    -0x2750(%rbp),%rax
mov    -0x2744(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2750(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <string_pos@plt>
mov    %eax,-0x2734(%rbp)
lea    -0x2730(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2734(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x2750(%rbp),%rdi
call   1030 <free@plt>
mov    -0x11(%rbp),%al
lea    -0x2730(%rbp),%rsi
movsbl %al,%edi
call   1070 <occurence@plt>
add    $0x1,%eax
add    $0x2750,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nth_delimiter>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2710,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
call   1050 <strlen@plt>
mov    %rax,%rbx
lea    0x2(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %r14b,(%rax)
test   %rbx,%rbx
jle    11bb <nth_delimiter+0x4b>
mov    %rbp,%rdi
add    $0x1,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
add    $0x2,%ebx
jmp    11c0 <nth_delimiter+0x50>
mov    $0x2,%ebx
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1090 <string_pos@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
movsbl %r14b,%edi
mov    %rbx,%rsi
call   1080 <occurence@plt>
add    $0x1,%eax
add    $0x2710,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nth_delimiter(i8* %0, i8* %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca [10000 x i8]
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8 %2, i8* %6
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %9
%14 = load i64, i64* %9
%15 = add nsw i64 %14, 1
%16 = add nsw i64 %15, 1
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %11
%18 = load i8, i8* %6
%19 = load i8*, i8** %11
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 %18, i8* %20
store i32 1, i32* %10
br label %21
21: ; preds = %38, %3
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = load i64, i64* %9
%25 = add nsw i64 %24, 1
%26 = icmp slt i64 %23, %25
br i1 %26, label %27, label %41
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = load i32, i32* %10
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = load i8, i8* %32
%34 = load i8*, i8** %11
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 %33, i8* %37
br label %38
38: ; preds = %27
%39 = load i32, i32* %10
%40 = add nsw i32 %39, 1
store i32 %40, i32* %10
br label %21
41: ; preds = %21
%42 = load i8*, i8** %11
%43 = load i32, i32* %10
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %11
%48 = load i8*, i8** %5
%49 = call i32 @string_pos(i8* %47, i8* %48)
store i32 %49, i32* %8
%50 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%51 = load i8*, i8** %5
%52 = load i32, i32* %8
%53 = call i32 @strncpy(i8* %50, i8* %51, i32 %52)
%54 = load i8*, i8** %11
%55 = call i32 @free(i8* %54)
%56 = load i8, i8* %6
%57 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%58 = call i32 @occurence(i8 signext %56, i8* %57)
%59 = add nsw i32 %58, 1
ret i32 %59
}

LLVM IR (-O2):
define i32 @nth_delimiter(i8* nocapture %0, i8* %1, i8 signext %2) {
%4 = alloca [10000 x i8]
%5 = getelementptr inbounds [10000 x i8], [10000 x i8]* %4, i64 0, i64 0
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = add nsw i64 %6, 2
%8 = tail call i8* @malloc(i64 %7)
store i8 %2, i8* %8
%9 = icmp slt i64 %6, 1
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = getelementptr i8, i8* %8, i64 1
%12 = add nuw i64 %6, 2
%13 = and i64 %12, 4294967295
br label %14
14: ; preds = %10, %3
%15 = phi i64 [ 2, %3 ], [ %13, %10 ]
%16 = getelementptr inbounds i8, i8* %8, i64 %15
store i8 0, i8* %16
%17 = tail call i32 @string_pos(i8* %8, i8* %1)
%18 = call i32 @strncpy(i8* %5, i8* %1, i32 %17)
%19 = call i32 @free(i8* %8)
%20 = call i32 @occurence(i8 signext %2, i8* %5)
%21 = add nsw i32 %20, 1
ret i32 %21
}