#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*/ exit(int);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void new_extension(char *filename, char *ext, int len) {
  int i;
  i = strlen(filename) - 1;
  if (i + 2 > len) {
    printf("Internal error: filename too long %s:%d\n",
           "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/mikeakohn/naken_asm/refs/heads/master/"
           "common/naken_asm.c",
           56);
    exit(1);
  }
  while (i > 0) {
    if (filename[i] == '.') {
      strcpy(filename + i + 1, ext);
      break;
    }
    i--;
  }
  if (i == 0) {
    strcat(filename, ".");
    strcat(filename, ext);
  }
}


x86 assembly (clang, -O0):
<new_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
cmp    -0x14(%rbp),%eax
jle    11a3 <new_extension+0x53>
lea    0xe78(%rip),%rdi
lea    0xe9a(%rip),%rsi
mov    $0x38,%edx
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11a8 <new_extension+0x58>
cmpl   $0x0,-0x18(%rbp)
jle    11f2 <new_extension+0xa2>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    11e4 <new_extension+0x94>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11f2 <new_extension+0xa2>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    11a8 <new_extension+0x58>
cmpl   $0x0,-0x18(%rbp)
jne    1219 <new_extension+0xc9>
mov    -0x8(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_extension>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    %ebp,%eax
jge    11c8 <new_extension+0x78>
mov    %eax,%ecx
add    $0xffffffff,%eax
lea    (%rax,%rbx,1),%rdi
add    $0x2,%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2,%ecx
jl     11a0 <new_extension+0x50>
add    $0xffffffffffffffff,%rdi
cmpb   $0x2e,-0x1(%rbx,%rcx,1)
lea    -0x1(%rcx),%rcx
jne    1180 <new_extension+0x30>
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <strcpy@plt>
cmp    $0x1,%ecx
jne    11c3 <new_extension+0x73>
lea    0xf10(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <strcat@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe31(%rip),%rdi
lea    0xe53(%rip),%rsi
mov    $0x38,%edx
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"Internal error: filename too long %s:%d\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/mikeakohn/naken_asm/refs/heads/master/common/naken_asm.c\00"
@.str.2 = [2 x i8] c".\00"
define void @new_extension(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = sub nsw i32 %9, 1
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = add nsw i32 %11, 2
%13 = load i32, i32* %6
%14 = icmp sgt i32 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 56)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
br label %19
19: ; preds = %38, %18
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %41
22: ; preds = %19
%23 = load i8*, i8** %4
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 46
br i1 %29, label %30, label %38
30: ; preds = %22
%31 = load i8*, i8** %4
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i8*, i8** %5
%37 = call i32 @strcpy(i8* %35, i8* %36)
br label %41
38: ; preds = %22
%39 = load i32, i32* %7
%40 = add nsw i32 %39, -1
store i32 %40, i32* %7
br label %19
41: ; preds = %30, %19
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i8*, i8** %4
%46 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%47 = load i8*, i8** %4
%48 = load i8*, i8** %5
%49 = call i32 @strcat(i8* %47, i8* %48)
br label %50
50: ; preds = %44, %41
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"Internal error: filename too long %s:%d\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/mikeakohn/naken_asm/refs/heads/master/common/naken_asm.c\00"
@.str.2 = [2 x i8] c".\00"
define void @new_extension(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp slt i32 %4, %2
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = zext i32 %4 to i64
br label %11
8: ; preds = %3
%9 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 56)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %6, %16
%12 = phi i64 [ %7, %6 ], [ %13, %16 ]
%13 = add nsw i64 %12, -1
%14 = trunc i64 %12 to i32
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %25
16: ; preds = %11
%17 = getelementptr inbounds i8, i8* %0, i64 %13
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 46
br i1 %19, label %20, label %11
20: ; preds = %16
%21 = and i64 %13, 4294967295
%22 = getelementptr inbounds i8, i8* %0, i64 %21
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = tail call i32 @strcpy(i8* %23, i8* %1)
br label %31
25: ; preds = %11
%26 = trunc i64 %13 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @strcat(i8* %0, i8* %1)
br label %31
31: ; preds = %20, %28, %25
ret void
}