#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 *index(char *, char);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *nthPathComponent(char *buf, char *source, int n) {
  if (!source || !buf)
    return NULL;
  int i = 0;
  char *pathItem = (char *)malloc(strlen(source) + 1);
  char *os1 = pathItem;
  char *s = source;
  for (i = 0; (i < n) && (s = index(s, '/')); i++, *s++)
    ;
  if (!s) {
    printf("not enough path components!!!!!\n");
    free(pathItem);
    return NULL;
  }
  while (*s != '/' && (*os1++ = *s++))
    ;
  *os1++ = 0;
  strcpy(buf, pathItem);
  free(pathItem);
  return buf;
}


x86 assembly (clang, -O0):
<nthPathComponent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1189 <nthPathComponent+0x29>
cmpq   $0x0,-0x10(%rbp)
jne    1196 <nthPathComponent+0x36>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <nthPathComponent+0x183>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
cmp    -0x1c(%rbp),%ecx
mov    %al,-0x39(%rbp)
jge    11fa <nthPathComponent+0x9a>
mov    -0x38(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <index@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    120a <nthPathComponent+0xaa>
jmp    1229 <nthPathComponent+0xc9>
jmp    120f <nthPathComponent+0xaf>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11cd <nthPathComponent+0x6d>
cmpq   $0x0,-0x38(%rbp)
jne    1256 <nthPathComponent+0xf6>
lea    0xdc5(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <nthPathComponent+0x183>
jmp    125b <nthPathComponent+0xfb>
mov    -0x38(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x3a(%rbp)
je     129e <nthPathComponent+0x13e>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
mov    (%rax),%al
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x3a(%rbp)
mov    -0x3a(%rbp),%al
test   $0x1,%al
jne    12ae <nthPathComponent+0x14e>
jmp    12b3 <nthPathComponent+0x153>
jmp    125b <nthPathComponent+0xfb>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nthPathComponent>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
test   %rdi,%rdi
je     1205 <nthPathComponent+0xa5>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1205 <nthPathComponent+0xa5>
mov    %edx,%ebp
mov    %rdi,%r14
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
test   %ebp,%ebp
jle    11be <nthPathComponent+0x5e>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <index@plt>
test   %rax,%rax
je     11eb <nthPathComponent+0x8b>
mov    %rax,%rbx
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    11a0 <nthPathComponent+0x40>
xor    %eax,%eax
movzbl (%rbx,%rax,1),%ecx
cmp    $0x2f,%cl
je     11d5 <nthPathComponent+0x75>
mov    %cl,(%r15,%rax,1)
add    $0x1,%rax
test   %cl,%cl
jne    11c0 <nthPathComponent+0x60>
mov    %r15,%rcx
add    %rax,%rcx
movb   $0x0,(%rcx)
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
jmp    11fa <nthPathComponent+0x9a>
lea    0xe0e(%rip),%rdi
call   1050 <puts@plt>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"not enough path components!!!!!\0A\00"
define i8* @nthPathComponent(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %14, %3
store i8* null, i8** %4
br label %76
18: ; preds = %14
store i32 0, i32* %8
%19 = load i8*, i8** %6
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %9
%24 = load i8*, i8** %9
store i8* %24, i8** %10
%25 = load i8*, i8** %6
store i8* %25, i8** %11
store i32 0, i32* %8
br label %26
26: ; preds = %37, %18
%27 = load i32, i32* %8
%28 = load i32, i32* %7
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i8*, i8** %11
%32 = call i8* @index(i8* %31, i8 signext 47)
store i8* %32, i8** %11
%33 = icmp ne i8* %32, null
br label %34
34: ; preds = %30, %26
%35 = phi i1 [ false, %26 ], [ %33, %30 ]
br i1 %35, label %36, label %43
36: ; preds = %34
br label %37
37: ; preds = %36
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
%40 = load i8*, i8** %11
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %11
%42 = load i8, i8* %40
br label %26
43: ; preds = %34
%44 = load i8*, i8** %11
%45 = icmp ne i8* %44, null
br i1 %45, label %50, label %46
46: ; preds = %43
%47 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%48 = load i8*, i8** %9
%49 = call i32 @free(i8* %48)
store i8* null, i8** %4
br label %76
50: ; preds = %43
br label %51
51: ; preds = %66, %50
%52 = load i8*, i8** %11
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 47
br i1 %55, label %56, label %64
56: ; preds = %51
%57 = load i8*, i8** %11
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %11
%59 = load i8, i8* %57
%60 = load i8*, i8** %10
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %10
store i8 %59, i8* %60
%62 = sext i8 %59 to i32
%63 = icmp ne i32 %62, 0
br label %64
64: ; preds = %56, %51
%65 = phi i1 [ false, %51 ], [ %63, %56 ]
br i1 %65, label %66, label %67
66: ; preds = %64
br label %51
67: ; preds = %64
%68 = load i8*, i8** %10
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %10
store i8 0, i8* %68
%70 = load i8*, i8** %5
%71 = load i8*, i8** %9
%72 = call i32 @strcpy(i8* %70, i8* %71)
%73 = load i8*, i8** %9
%74 = call i32 @free(i8* %73)
%75 = load i8*, i8** %5
store i8* %75, i8** %4
br label %76
76: ; preds = %67, %46, %17
%77 = load i8*, i8** %4
ret i8* %77
}

LLVM IR (-O2):
@str = [32 x i8] c"not enough path components!!!!!\00"
define i8* @nthPathComponent(i8* %0, i8* %1, i32 %2) {
%4 = icmp ne i8* %1, null
%5 = icmp ne i8* %0, null
%6 = and i1 %5, %4
br i1 %6, label %7, label %41
7: ; preds = %3
%8 = tail call i64 @strlen(i8* %1)
%9 = add nsw i64 %8, 1
%10 = tail call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
%12 = icmp sgt i32 %2, 0
br i1 %12, label %15, label %13
13: ; preds = %20, %7
%14 = phi i8* [ %1, %7 ], [ %22, %20 ]
br label %26
15: ; preds = %7, %20
%16 = phi i8* [ %22, %20 ], [ %1, %7 ]
%17 = phi i32 [ %21, %20 ], [ 0, %7 ]
%18 = tail call i8* @index(i8* %16, i8 signext 47)
%19 = icmp eq i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = add nuw nsw i32 %17, 1
%22 = getelementptr inbounds i8, i8* %18, i64 1
%23 = icmp eq i32 %21, %2
br i1 %23, label %13, label %15
24: ; preds = %15
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %38
26: ; preds = %13, %31
%27 = phi i8* [ %33, %31 ], [ %11, %13 ]
%28 = phi i8* [ %32, %31 ], [ %14, %13 ]
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 47
br i1 %30, label %35, label %31
31: ; preds = %26
%32 = getelementptr inbounds i8, i8* %28, i64 1
%33 = getelementptr inbounds i8, i8* %27, i64 1
store i8 %29, i8* %27
%34 = icmp eq i8 %29, 0
br i1 %34, label %35, label %26
35: ; preds = %26, %31
%36 = phi i8* [ %33, %31 ], [ %27, %26 ]
store i8 0, i8* %36
%37 = tail call i32 @strcpy(i8* %0, i8* %11)
br label %38
38: ; preds = %35, %24
%39 = phi i8* [ null, %24 ], [ %0, %35 ]
%40 = tail call i32 @free(i8* %11)
br label %41
41: ; preds = %38, %3
%42 = phi i8* [ null, %3 ], [ %39, %38 ]
ret i8* %42
}