#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 strlen(char const *);
const char *GetTrailingPathComponents(const char *path, int noOfComponents) {
  const char *ptr = path + strlen(path);
  int count = 0;
  while (--ptr > path) {
    if (*ptr == '/') {
      if (count++ == noOfComponents) {
        break;
      }
    }
  }
  return (ptr);
}


x86 assembly (clang, -O0):
<GetTrailingPathComponents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
cmp    -0x8(%rbp),%rax
jbe    1190 <GetTrailingPathComponents+0x80>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    118b <GetTrailingPathComponents+0x7b>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    -0xc(%rbp),%eax
jne    1186 <GetTrailingPathComponents+0x76>
jmp    1190 <GetTrailingPathComponents+0x80>
jmp    118b <GetTrailingPathComponents+0x7b>
jmp    1147 <GetTrailingPathComponents+0x37>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetTrailingPathComponents>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
cltq
add    %rbx,%rax
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jbe    114a <GetTrailingPathComponents+0x3a>
xor    %ecx,%ecx
jmp    1139 <GetTrailingPathComponents+0x29>
nop
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jbe    1147 <GetTrailingPathComponents+0x37>
cmpb   $0x2f,(%rax)
jne    1130 <GetTrailingPathComponents+0x20>
cmp    %ebp,%ecx
je     114a <GetTrailingPathComponents+0x3a>
add    $0x1,%ecx
jmp    1130 <GetTrailingPathComponents+0x20>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i8* @GetTrailingPathComponents(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %7, i64 %10
store i8* %11, i8** %5
store i32 0, i32* %6
br label %12
12: ; preds = %29, %2
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i32 -1
store i8* %14, i8** %5
%15 = load i8*, i8** %3
%16 = icmp ugt i8* %14, %15
br i1 %16, label %17, label %30
17: ; preds = %12
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 47
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
%25 = load i32, i32* %4
%26 = icmp eq i32 %23, %25
br i1 %26, label %27, label %28
27: ; preds = %22
br label %30
28: ; preds = %22
br label %29
29: ; preds = %28, %17
br label %12
30: ; preds = %27, %12
%31 = load i8*, i8** %5
ret i8* %31
}

LLVM IR (-O2):
define i8* @GetTrailingPathComponents(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = getelementptr inbounds i8, i8* %5, i64 -1
%7 = icmp ugt i8* %6, %0
br i1 %7, label %8, label %20
8: ; preds = %2, %16
%9 = phi i8* [ %18, %16 ], [ %6, %2 ]
%10 = phi i32 [ %17, %16 ], [ 0, %2 ]
%11 = load i8, i8* %9
%12 = icmp eq i8 %11, 47
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = add nsw i32 %10, 1
%15 = icmp eq i32 %10, %1
br i1 %15, label %20, label %16
16: ; preds = %13, %8
%17 = phi i32 [ %14, %13 ], [ %10, %8 ]
%18 = getelementptr inbounds i8, i8* %9, i64 -1
%19 = icmp ugt i8* %18, %0
br i1 %19, label %8, label %20
20: ; preds = %16, %13, %2
%21 = phi i8* [ %6, %2 ], [ %9, %13 ], [ %0, %16 ]
ret i8* %21
}