#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 *strrchr(char const *, char);
int make_file_name(char *dest, size_t max, const char *orig,
                   const char *newext) {
  const char *oldext;
  oldext = strrchr(orig, '.');
  while (*orig && orig != oldext) {
    if (max <= 0)
      return 0;
    max--;
    *(dest++) = *(orig++);
  }
  do {
    if (max <= 0)
      return 0;
    max--;
    *(dest++) = *newext;
  } while (*(newext++));
  return 1;
}


x86 assembly (clang, -O0):
<make_file_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2e,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
je     115d <make_file_name+0x4d>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    116d <make_file_name+0x5d>
jmp    11b7 <make_file_name+0xa7>
cmpq   $0x0,-0x18(%rbp)
ja     1184 <make_file_name+0x74>
movl   $0x0,-0x4(%rbp)
jmp    1215 <make_file_name+0x105>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    113a <make_file_name+0x2a>
jmp    11bc <make_file_name+0xac>
cmpq   $0x0,-0x18(%rbp)
ja     11d3 <make_file_name+0xc3>
movl   $0x0,-0x4(%rbp)
jmp    1215 <make_file_name+0x105>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
cmpb   $0x0,(%rax)
jne    11bc <make_file_name+0xac>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_file_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rdx,%rdi
mov    $0x2e,%esi
call   1030 <strrchr@plt>
mov    (%rbx),%cl
test   %cl,%cl
je     1161 <make_file_name+0x51>
cmp    %rbx,%rax
je     1161 <make_file_name+0x51>
add    $0x1,%rbx
sub    $0x1,%r15
jb     1190 <make_file_name+0x80>
mov    %cl,(%r12)
add    $0x1,%r12
movzbl (%rbx),%ecx
test   %cl,%cl
je     1161 <make_file_name+0x51>
lea    0x1(%rbx),%rdx
cmp    %rax,%rbx
mov    %rdx,%rbx
jne    1140 <make_file_name+0x30>
xor    %eax,%eax
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %rcx,%r15
je     1192 <make_file_name+0x82>
movzbl (%r14,%rcx,1),%edx
mov    %dl,(%r12,%rcx,1)
cmpb   $0x0,(%r14,%rcx,1)
lea    0x1(%rcx),%rcx
jne    1170 <make_file_name+0x60>
mov    $0x1,%eax
jmp    1192 <make_file_name+0x82>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @make_file_name(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 i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%11 = load i8*, i8** %8
%12 = call i8* @strrchr(i8* %11, i8 signext 46)
store i8* %12, i8** %10
br label %13
13: ; preds = %28, %4
%14 = load i8*, i8** %8
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i8*, i8** %8
%20 = load i8*, i8** %10
%21 = icmp ne i8* %19, %20
br label %22
22: ; preds = %18, %13
%23 = phi i1 [ false, %13 ], [ %21, %18 ]
br i1 %23, label %24, label %36
24: ; preds = %22
%25 = load i64, i64* %7
%26 = icmp ule i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 0, i32* %5
br label %54
28: ; preds = %24
%29 = load i64, i64* %7
%30 = add i64 %29, -1
store i64 %30, i64* %7
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %8
%33 = load i8, i8* %31
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
store i8 %33, i8* %34
br label %13
36: ; preds = %22
br label %37
37: ; preds = %48, %36
%38 = load i64, i64* %7
%39 = icmp ule i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 0, i32* %5
br label %54
41: ; preds = %37
%42 = load i64, i64* %7
%43 = add i64 %42, -1
store i64 %43, i64* %7
%44 = load i8*, i8** %9
%45 = load i8, i8* %44
%46 = load i8*, i8** %6
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %6
store i8 %45, i8* %46
br label %48
48: ; preds = %41
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %9
%51 = load i8, i8* %49
%52 = icmp ne i8 %51, 0
br i1 %52, label %37, label %53
53: ; preds = %48
store i32 1, i32* %5
br label %54
54: ; preds = %53, %40, %27
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
define i32 @make_file_name(i8* nocapture writeonly %0, i64 %1, i8* %2, i8* nocapture %3) {
%5 = tail call i8* @strrchr(i8* dereferenceable(1) %2, i8 signext 46)
%6 = load i8, i8* %2
%7 = icmp ne i8 %6, 0
%8 = icmp ne i8* %5, %2
%9 = select i1 %7, i1 %8, i1 false
br i1 %9, label %10, label %24
10: ; preds = %4, %16
%11 = phi i8 [ %20, %16 ], [ %6, %4 ]
%12 = phi i8* [ %18, %16 ], [ %2, %4 ]
%13 = phi i64 [ %17, %16 ], [ %1, %4 ]
%14 = phi i8* [ %19, %16 ], [ %0, %4 ]
%15 = icmp eq i64 %13, 0
br i1 %15, label %39, label %16
16: ; preds = %10
%17 = add i64 %13, -1
%18 = getelementptr inbounds i8, i8* %12, i64 1
%19 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %11, i8* %14
%20 = load i8, i8* %18
%21 = icmp ne i8 %20, 0
%22 = icmp ne i8* %18, %5
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %10, label %24
24: ; preds = %16, %4
%25 = phi i8* [ %0, %4 ], [ %19, %16 ]
%26 = phi i64 [ %1, %4 ], [ %17, %16 ]
br label %27
27: ; preds = %24, %32
%28 = phi i8* [ %35, %32 ], [ %25, %24 ]
%29 = phi i64 [ %33, %32 ], [ %26, %24 ]
%30 = phi i8* [ %36, %32 ], [ %3, %24 ]
%31 = icmp eq i64 %29, 0
br i1 %31, label %39, label %32
32: ; preds = %27
%33 = add i64 %29, -1
%34 = load i8, i8* %30
%35 = getelementptr inbounds i8, i8* %28, i64 1
store i8 %34, i8* %28
%36 = getelementptr inbounds i8, i8* %30, i64 1
%37 = load i8, i8* %30
%38 = icmp eq i8 %37, 0
br i1 %38, label %39, label %27
39: ; preds = %10, %32, %27
%40 = phi i32 [ 1, %32 ], [ 0, %27 ], [ 0, %10 ]
ret i32 %40
}