#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 *get_extension(char *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int /*<<< orphan*/ to_lower(char *);
void set_extension(char *file, const char *filetype) {
  char lc_filetype[4] = {0};
  char *extension;
  char lc_extension[4];
  strncpy(lc_filetype, filetype, 3);
  to_lower(lc_filetype);
  extension = get_extension(file);
  if (extension) {
    strcpy(lc_extension, extension);
    to_lower(lc_extension);
    if (strcmp(lc_filetype, lc_extension) == 0) {
      return;
    }
    *(extension - 1) = '\0';
  }
  if (strlen(file) > 251) {
    file[251] = '\0';
  }
  strcat(file, ".");
  strncat(file, filetype, 3);
}


x86 assembly (clang, -O0):
<set_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    -0x14(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
lea    -0x14(%rbp),%rdi
call   1060 <to_lower@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <get_extension@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     121d <set_extension+0x8d>
lea    -0x24(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x24(%rbp),%rdi
call   1060 <to_lower@plt>
lea    -0x14(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1215 <set_extension+0x85>
jmp    125e <set_extension+0xce>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0xfb,%eax
jle    123c <set_extension+0xac>
mov    -0x8(%rbp),%rax
movb   $0x0,0xfb(%rax)
mov    -0x8(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   10a0 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1080 <strncat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_extension>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x0,(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1060 <to_lower@plt>
mov    %r15,%rdi
call   10a0 <get_extension@plt>
test   %rax,%rax
je     11e9 <set_extension+0x69>
mov    %rax,%rbx
lea    0x4(%rsp),%r12
mov    %r12,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
call   1060 <to_lower@plt>
mov    %rsp,%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     121f <set_extension+0x9f>
movb   $0x0,-0x1(%rbx)
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    $0xfc,%eax
jl     1200 <set_extension+0x80>
movb   $0x0,0xfb(%r15)
lea    0xdf9(%rip),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1070 <strncat@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define void @set_extension(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [4 x i8]
%6 = alloca i8*
%7 = alloca [4 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = bitcast [4 x i8]* %5 to i8*
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%10 = load i8*, i8** %4
%11 = call i32 @strncpy(i8* %9, i8* %10, i32 3)
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%13 = call i32 @to_lower(i8* %12)
%14 = load i8*, i8** %3
%15 = call i8* @get_extension(i8* %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %32
18: ; preds = %2
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%20 = load i8*, i8** %6
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%23 = call i32 @to_lower(i8* %22)
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%25 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%26 = call i64 @strcmp(i8* %24, i8* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %18
br label %45
29: ; preds = %18
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i64 -1
store i8 0, i8* %31
br label %32
32: ; preds = %29, %2
%33 = load i8*, i8** %3
%34 = call i32 @strlen(i8* %33)
%35 = icmp sgt i32 %34, 251
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i64 251
store i8 0, i8* %38
br label %39
39: ; preds = %36, %32
%40 = load i8*, i8** %3
%41 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%42 = load i8*, i8** %3
%43 = load i8*, i8** %4
%44 = call i32 @strncat(i8* %42, i8* %43, i32 3)
br label %45
45: ; preds = %39, %28
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define void @set_extension(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca [4 x i8]
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%7 = call i32 @strncpy(i8* %5, i8* %1, i32 3)
%8 = call i32 @to_lower(i8* %5)
%9 = call i8* @get_extension(i8* %0)
%10 = icmp eq i8* %9, null
br i1 %10, label %18, label %11
11: ; preds = %2
%12 = call i32 @strcpy(i8* %6, i8* %9)
%13 = call i32 @to_lower(i8* %6)
%14 = call i64 @strcmp(i8* %5, i8* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %11
%17 = getelementptr inbounds i8, i8* %9, i64 -1
store i8 0, i8* %17
br label %18
18: ; preds = %16, %2
%19 = call i32 @strlen(i8* %0)
%20 = icmp sgt i32 %19, 251
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %0, i64 251
store i8 0, i8* %22
br label %23
23: ; preds = %21, %18
%24 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @strncat(i8* %0, i8* %1, i32 3)
br label %26
26: ; preds = %11, %23
ret void
}