#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 */
size_t PATH_MAX;
char *malloc(size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *_copy_path_extension(const char *path) {
  size_t path_length = strlen(path);
  if (path_length > PATH_MAX)
    return NULL;
  const char *path_pointer = path + path_length;
  while (path_pointer < path) {
    if (*path_pointer == '.')
      break;
    path_pointer--;
  }
  if (path_pointer == path)
    return NULL;
  path_pointer++;
  size_t path_extension_size = (path + path_length) - path_pointer + 2;
  char *path_extension = malloc(path_extension_size);
  strncpy(path_extension, path_pointer, path_extension_size - 1);
  return path_extension;
}


x86 assembly (clang, -O0):
<_copy_path_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e9c(%rip),%rcx
cmp    (%rcx),%rax
jbe    116a <_copy_path_extension+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    1212 <_copy_path_extension+0xe2>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11aa <_copy_path_extension+0x7a>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1199 <_copy_path_extension+0x69>
jmp    11aa <_copy_path_extension+0x7a>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1176 <_copy_path_extension+0x46>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11c5 <_copy_path_extension+0x95>
movq   $0x0,-0x8(%rbp)
jmp    1212 <_copy_path_extension+0xe2>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_copy_path_extension>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    0x2eac(%rip),%rcx
cmp    (%rcx),%rax
ja     119a <_copy_path_extension+0x6a>
lea    (%r15,%rax,1),%r14
mov    %r14,%rbx
test   %rax,%rax
jns    116e <_copy_path_extension+0x3e>
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
cmpb   $0x2e,(%rbx)
je     116e <_copy_path_extension+0x3e>
add    $0xffffffffffffffff,%rbx
cmp    %r15,%rbx
jb     1160 <_copy_path_extension+0x30>
cmp    %r15,%rbx
je     119a <_copy_path_extension+0x6a>
add    $0x1,%rbx
sub    %rbx,%r14
lea    0x2(%r14),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
add    $0x1,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
jmp    119d <_copy_path_extension+0x6d>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@PATH_MAX = global i64 0
define i8* @_copy_path_extension(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = load i64, i64* @PATH_MAX
%4 = icmp ugt i64 %2, %3
br i1 %4, label %27, label %5
5: ; preds = %1
%6 = getelementptr inbounds i8, i8* %0, i64 %2
%7 = icmp slt i64 %2, 0
br i1 %7, label %8, label %15
8: ; preds = %5, %12
%9 = phi i8* [ %13, %12 ], [ %6, %5 ]
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 46
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %9, i64 -1
%14 = icmp ult i8* %13, %0
br i1 %14, label %8, label %15
15: ; preds = %12, %8, %5
%16 = phi i8* [ %6, %5 ], [ %9, %8 ], [ %13, %12 ]
%17 = icmp eq i8* %16, %0
br i1 %17, label %27, label %18
18: ; preds = %15
%19 = getelementptr inbounds i8, i8* %16, i64 1
%20 = ptrtoint i8* %6 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
%23 = add nsw i64 %22, 2
%24 = tail call i8* @malloc(i64 %23)
%25 = add i64 %22, 1
%26 = tail call i32 @strncpy(i8* %24, i8* %19, i64 %25)
br label %27
27: ; preds = %18, %15, %1
%28 = phi i8* [ null, %1 ], [ %24, %18 ], [ null, %15 ]
ret i8* %28
}