#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 */
typedef scalar_t__ time_t;
struct tm {
  int tm_year;
  int tm_mon;
  int /*<<< orphan*/ tm_mday;
};

/* Variables and functions */
int /*<<< orphan*/ localtime_r(scalar_t__ *, struct tm *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strrchr(char const *, char);
scalar_t__ time(int /*<<< orphan*/ *);
void convert_logfilename(const char *origin, char *converted, size_t namelen) {
  char *p;
  int len;
  struct tm m;
  time_t now;
  len = strlen(origin);
  if (namelen < (size_t)len + 10)
    goto cannot_convert;
  now = time(NULL);
  if (now == (time_t)-1)
    goto cannot_convert;
  localtime_r(&now, &m);
  p = strrchr(origin, '.');
  if (p == NULL) {
    sprintf(converted, "%s.%04d%02d%02d", origin, m.tm_year + 1900,
            m.tm_mon + 1, m.tm_mday);
  } else {
    len = p - origin;
    memcpy(converted, origin, len);
    sprintf(converted + len, ".%04d%02d%02d", m.tm_year + 1900, m.tm_mon + 1,
            m.tm_mday);
    strcat(converted, p);
  }
  return;
cannot_convert:
  strncpy(converted, origin, namelen);
  converted[namelen - 1] = '\0';
}


x86 assembly (clang, -O0):
<convert_logfilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    $0xa,%rcx
cmp    %rcx,%rax
jae    11ba <convert_logfilename+0x3a>
jmp    128d <convert_logfilename+0x10d>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    11d7 <convert_logfilename+0x57>
jmp    128d <convert_logfilename+0x10d>
lea    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <localtime_r@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1231 <convert_logfilename+0xb1>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x30(%rbp),%ecx
add    $0x76c,%ecx
mov    -0x2c(%rbp),%r8d
add    $0x1,%r8d
mov    -0x28(%rbp),%r9d
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
jmp    1288 <convert_logfilename+0x108>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x30(%rbp),%edx
add    $0x76c,%edx
mov    -0x2c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x28(%rbp),%r8d
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <strcat@plt>
jmp    12ae <convert_logfilename+0x12e>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_logfilename>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <strlen@plt>
cltq
add    $0xa,%rax
cmp    %rbx,%rax
ja     1220 <convert_logfilename+0xa0>
xor    %edi,%edi
call   1080 <time@plt>
mov    %rax,0x10(%rsp)
cmp    $0xffffffffffffffff,%rax
je     1220 <convert_logfilename+0xa0>
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1040 <localtime_r@plt>
mov    %r12,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1240 <convert_logfilename+0xc0>
mov    %rax,%r15
mov    %rax,%rbx
sub    %r12,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1070 <memcpy@plt>
movslq %ebx,%rdi
mov    $0x76c,%edx
add    (%rsp),%edx
add    %r14,%rdi
mov    0x4(%rsp),%ecx
mov    0x8(%rsp),%r8d
add    $0x1,%ecx
lea    0xdf6(%rip),%rsi
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
jmp    1234 <convert_logfilename+0xb4>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    $0x76c,%ecx
add    (%rsp),%ecx
mov    0x4(%rsp),%r8d
mov    0x8(%rsp),%r9d
add    $0x1,%r8d
lea    0xda3(%rip),%rsi
mov    %r14,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
jmp    1234 <convert_logfilename+0xb4>

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

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32 }
@.str = [16 x i8] c"%s.%04d%02d%02d\00"
@.str.1 = [14 x i8] c".%04d%02d%02d\00"
define void @convert_logfilename(i8* %0, i8* %1, i64 %2) {
%4 = alloca %struct.tm
%5 = alloca i64
%6 = bitcast %struct.tm* %4 to i8*
%7 = bitcast i64* %5 to i8*
%8 = tail call i32 @strlen(i8* %0)
%9 = sext i32 %8 to i64
%10 = add nsw i64 %9, 10
%11 = icmp ugt i64 %10, %2
br i1 %11, label %48, label %12
12: ; preds = %3
%13 = tail call i64 @time(i32* null)
store i64 %13, i64* %5
%14 = icmp eq i64 %13, -1
br i1 %14, label %48, label %15
15: ; preds = %12
%16 = call i32 @localtime_r(i64* %5, %struct.tm* %4)
%17 = call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %29
19: ; preds = %15
%20 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1900
%23 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 1
%24 = load i32, i32* %23
%25 = add nsw i32 %24, 1
%26 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %22, i32 %25, i32 %27)
br label %52
29: ; preds = %15
%30 = ptrtoint i8* %17 to i64
%31 = ptrtoint i8* %0 to i64
%32 = sub i64 %30, %31
%33 = trunc i64 %32 to i32
%34 = call i32 @memcpy(i8* %1, i8* %0, i32 %33)
%35 = shl i64 %32, 32
%36 = ashr exact i64 %35, 32
%37 = getelementptr inbounds i8, i8* %1, i64 %36
%38 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 0
%39 = load i32, i32* %38
%40 = add nsw i32 %39, 1900
%41 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 1
%42 = load i32, i32* %41
%43 = add nsw i32 %42, 1
%44 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 2
%45 = load i32, i32* %44
%46 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %37, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %43, i32 %45)
%47 = call i32 @strcat(i8* %1, i8* %17)
br label %52
48: ; preds = %12, %3
%49 = tail call i32 @strncpy(i8* %1, i8* %0, i64 %2)
%50 = add i64 %2, -1
%51 = getelementptr inbounds i8, i8* %1, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %19, %29, %48
ret void
}