#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*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ safer_strncpy(char *, size_t const, char const *, size_t);
int /*<<< orphan*/ snprintf(char *, size_t const, char *, char const *,
                            char const *);
int /*<<< orphan*/ strcat(char *, char const *);
size_t strlen(char const *);
char *strrchr(char const *, char);
bool change_extension(char *pathbuf, const size_t pathbuf_length,
                      const char *filename, const char *extension) {
  size_t len_root;
  size_t len_ext;
  size_t len_final;
  char *separator;
  if (!pathbuf || (pathbuf_length == 0) || !filename || !extension) {
    errno = EINVAL;
    return false;
  }
  len_root = strlen(filename);
  len_ext = strlen(extension);
  separator = strrchr(filename, '.');
  if (!separator) {
    len_final = len_root + 1 + len_ext;
    if (len_final >= pathbuf_length) {
      return false;
    }
    snprintf(pathbuf, pathbuf_length, "%s.%s", filename, extension);
  } else {
    len_root = separator - filename;
    len_final = len_root + 1 + len_ext;
    if (len_final >= pathbuf_length) {
      return false;
    }
    safer_strncpy(pathbuf, pathbuf_length, filename, len_root);
    strcat(pathbuf, extension);
  }
  return true;
}


x86 assembly (clang, -O0):
<change_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1194 <change_extension+0x44>
cmpq   $0x0,-0x18(%rbp)
je     1194 <change_extension+0x44>
cmpq   $0x0,-0x20(%rbp)
je     1194 <change_extension+0x44>
cmpq   $0x0,-0x28(%rbp)
jne    11b2 <change_extension+0x62>
mov    0x2e3d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3c(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1296 <change_extension+0x146>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1234 <change_extension+0xe4>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1213 <change_extension+0xc3>
movl   $0x0,-0x4(%rbp)
jmp    1296 <change_extension+0x146>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xdd6(%rip),%rdx
call   1050 <snprintf@plt>
jmp    128f <change_extension+0x13f>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     126d <change_extension+0x11d>
movl   $0x0,-0x4(%rbp)
jmp    1296 <change_extension+0x146>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1030 <safer_strncpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <strcat@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_extension>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
je     11c6 <change_extension+0x76>
mov    %rsi,%r12
test   %rsi,%rsi
je     11c6 <change_extension+0x76>
mov    %rdx,%rbx
test   %rdx,%rdx
je     11c6 <change_extension+0x76>
mov    %rcx,%r14
test   %rcx,%rcx
je     11c6 <change_extension+0x76>
mov    %rdi,%r15
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11dc <change_extension+0x8c>
mov    %rax,%rcx
sub    %rbx,%rcx
lea    (%rcx,%r13,1),%rdx
add    $0x1,%rdx
xor    %eax,%eax
cmp    %r12,%rdx
jae    1210 <change_extension+0xc0>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <safer_strncpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
jmp    120b <change_extension+0xbb>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    0x2e0a(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    1210 <change_extension+0xc0>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r13,1),%rcx
add    $0x1,%rcx
xor    %eax,%eax
cmp    %r12,%rcx
jae    1210 <change_extension+0xc0>
lea    0xe06(%rip),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [6 x i8] c"%s.%s\00"
define i32 @change_extension(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %25
16: ; preds = %4
%17 = load i64, i64* %7
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i8*, i8** %9
%24 = icmp ne i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %22, %19, %16, %4
%26 = load i32, i32* @EINVAL
store i32 %26, i32* @errno
store i32 0, i32* %5
br label %75
27: ; preds = %22
%28 = load i8*, i8** %8
%29 = call i64 @strlen(i8* %28)
store i64 %29, i64* %10
%30 = load i8*, i8** %9
%31 = call i64 @strlen(i8* %30)
store i64 %31, i64* %11
%32 = load i8*, i8** %8
%33 = call i8* @strrchr(i8* %32, i8 signext 46)
store i8* %33, i8** %13
%34 = load i8*, i8** %13
%35 = icmp ne i8* %34, null
br i1 %35, label %51, label %36
36: ; preds = %27
%37 = load i64, i64* %10
%38 = add i64 %37, 1
%39 = load i64, i64* %11
%40 = add i64 %38, %39
store i64 %40, i64* %12
%41 = load i64, i64* %12
%42 = load i64, i64* %7
%43 = icmp uge i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %36
store i32 0, i32* %5
br label %75
45: ; preds = %36
%46 = load i8*, i8** %6
%47 = load i64, i64* %7
%48 = load i8*, i8** %8
%49 = load i8*, i8** %9
%50 = call i32 @snprintf(i8* %46, i64 %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %48, i8* %49)
br label %74
51: ; preds = %27
%52 = load i8*, i8** %13
%53 = load i8*, i8** %8
%54 = ptrtoint i8* %52 to i64
%55 = ptrtoint i8* %53 to i64
%56 = sub i64 %54, %55
store i64 %56, i64* %10
%57 = load i64, i64* %10
%58 = add i64 %57, 1
%59 = load i64, i64* %11
%60 = add i64 %58, %59
store i64 %60, i64* %12
%61 = load i64, i64* %12
%62 = load i64, i64* %7
%63 = icmp uge i64 %61, %62
br i1 %63, label %64, label %65
64: ; preds = %51
store i32 0, i32* %5
br label %75
65: ; preds = %51
%66 = load i8*, i8** %6
%67 = load i64, i64* %7
%68 = load i8*, i8** %8
%69 = load i64, i64* %10
%70 = call i32 @safer_strncpy(i8* %66, i64 %67, i8* %68, i64 %69)
%71 = load i8*, i8** %6
%72 = load i8*, i8** %9
%73 = call i32 @strcat(i8* %71, i8* %72)
br label %74
74: ; preds = %65, %45
store i32 1, i32* %5
br label %75
75: ; preds = %74, %64, %44, %25
%76 = load i32, i32* %5
ret i32 %76
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [6 x i8] c"%s.%s\00"
define i32 @change_extension(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = icmp ne i8* %0, null
%6 = icmp ne i64 %1, 0
%7 = and i1 %5, %6
%8 = icmp ne i8* %2, null
%9 = and i1 %7, %8
%10 = icmp ne i8* %3, null
%11 = and i1 %9, %10
br i1 %11, label %14, label %12
12: ; preds = %4
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
br label %35
14: ; preds = %4
%15 = tail call i64 @strlen(i8* %3)
%16 = tail call i8* @strrchr(i8* %2, i8 signext 46)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = tail call i64 @strlen(i8* %2)
%20 = add i64 %15, 1
%21 = add i64 %20, %19
%22 = icmp ult i64 %21, %1
br i1 %22, label %23, label %35
23: ; preds = %18
%24 = tail call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %3)
br label %35
25: ; preds = %14
%26 = ptrtoint i8* %16 to i64
%27 = ptrtoint i8* %2 to i64
%28 = sub i64 %26, %27
%29 = add i64 %15, 1
%30 = add i64 %29, %28
%31 = icmp ult i64 %30, %1
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = tail call i32 @safer_strncpy(i8* %0, i64 %1, i8* %2, i64 %28)
%34 = tail call i32 @strcat(i8* %0, i8* %3)
br label %35
35: ; preds = %23, %32, %25, %18, %12
%36 = phi i32 [ 0, %12 ], [ 0, %18 ], [ 0, %25 ], [ 1, %32 ], [ 1, %23 ]
ret i32 %36
}