#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 */
char *quoted_string_parse(char *, char *, int, int, char);
int strlen(char *);
scalar_t__ strncmp_case(char *, char *, int);
int find_nth_str(char *line, char *str, int occurence) {
  char *p = line;
  char val[256];
  int col = 0;
  int len = strlen(str);
  int nfound = 0;
  while (p) {
    p = quoted_string_parse(p, val, 256, -1, ',');
    if (strncmp_case(val, str, len) == 0) {
      ++nfound;
      if (nfound == occurence) {
        return col;
      }
    }
    ++col;
  }
  return -1;
}


x86 assembly (clang, -O0):
<find_nth_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x134(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x138(%rbp)
movl   $0x0,-0x13c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1205 <find_nth_str+0xd5>
mov    -0x28(%rbp),%rdi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
mov    $0xffffffff,%ecx
mov    $0x2c,%r8d
call   1040 <quoted_string_parse@plt>
mov    %rax,-0x28(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x138(%rbp),%edx
call   1050 <strncmp_case@plt>
cmp    $0x0,%rax
jne    11f1 <find_nth_str+0xc1>
mov    -0x13c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x13c(%rbp)
mov    -0x13c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11ec <find_nth_str+0xbc>
mov    -0x134(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <find_nth_str+0xdc>
jmp    11f1 <find_nth_str+0xc1>
mov    -0x134(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x134(%rbp)
jmp    1171 <find_nth_str+0x41>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_nth_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edx,0xc(%rsp)
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   %rbp,%rbp
je     11b6 <find_nth_str+0x86>
mov    %eax,%r12d
xor    %r14d,%r14d
lea    0x10(%rsp),%r13
xor    %ebx,%ebx
jmp    1178 <find_nth_str+0x48>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
test   %rbp,%rbp
je     11b6 <find_nth_str+0x86>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x100,%edx
mov    $0xffffffff,%ecx
mov    $0x2c,%r8d
call   1040 <quoted_string_parse@plt>
mov    %rax,%rbp
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1050 <strncmp_case@plt>
test   %rax,%rax
jne    1170 <find_nth_str+0x40>
add    $0x1,%r14d
cmp    0xc(%rsp),%r14d
jne    1170 <find_nth_str+0x40>
jmp    11bb <find_nth_str+0x8b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @find_nth_str(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
store i8* %13, i8** %8
store i32 0, i32* %10
%14 = load i8*, i8** %6
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %11
store i32 0, i32* %12
br label %16
16: ; preds = %37, %3
%17 = load i8*, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %40
19: ; preds = %16
%20 = load i8*, i8** %8
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%22 = call i8* @quoted_string_parse(i8* %20, i8* %21, i32 256, i32 -1, i8 signext 44)
store i8* %22, i8** %8
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%24 = load i8*, i8** %6
%25 = load i32, i32* %11
%26 = call i64 @strncmp_case(i8* %23, i8* %24, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %19
%29 = load i32, i32* %12
%30 = add nsw i32 %29, 1
store i32 %30, i32* %12
%31 = load i32, i32* %12
%32 = load i32, i32* %7
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = load i32, i32* %10
store i32 %35, i32* %4
br label %41
36: ; preds = %28
br label %37
37: ; preds = %36, %19
%38 = load i32, i32* %10
%39 = add nsw i32 %38, 1
store i32 %39, i32* %10
br label %16
40: ; preds = %16
store i32 -1, i32* %4
br label %41
41: ; preds = %40, %34
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @find_nth_str(i8* %0, i8* %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %1)
%7 = icmp eq i8* %0, null
br i1 %7, label %22, label %8
8: ; preds = %3, %18
%9 = phi i32 [ %19, %18 ], [ 0, %3 ]
%10 = phi i32 [ %20, %18 ], [ 0, %3 ]
%11 = phi i8* [ %12, %18 ], [ %0, %3 ]
%12 = call i8* @quoted_string_parse(i8* %11, i8* %5, i32 256, i32 -1, i8 signext 44)
%13 = call i64 @strncmp_case(i8* %5, i8* %1, i32 %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = add nsw i32 %9, 1
%17 = icmp eq i32 %16, %2
br i1 %17, label %22, label %18
18: ; preds = %15, %8
%19 = phi i32 [ %16, %15 ], [ %9, %8 ]
%20 = add nuw nsw i32 %10, 1
%21 = icmp eq i8* %12, null
br i1 %21, label %22, label %8
22: ; preds = %15, %18, %3
%23 = phi i32 [ -1, %3 ], [ -1, %18 ], [ %10, %15 ]
ret i32 %23
}