#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 */
struct timeval {
  scalar_t__ tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
char *alloca(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ gmtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int rename(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, int);
int /*<<< orphan*/ strftime(char *, int, char *, int /*<<< orphan*/);
int strlen(char *);
void rotate(char *directory, char *current_file) {
  int buflen = strlen(directory) + 50;
  char *buf = alloca(strlen(directory) + 50);
  struct timeval tv;
  gettimeofday(&tv, NULL);
  char tmbuf[500];
  strftime(tmbuf, 500, "%Y-%m-%d_%H:%M:%S", gmtime(&tv.tv_sec));
  snprintf(buf, buflen, "%sa%s.%06d", directory, tmbuf, (int)tv.tv_usec);
  int r = rename(current_file, buf);
  if (r < 0) {
    perror("error rotating current");
    fatal("current rotation rename() error");
  }
}


x86 assembly (clang, -O0):
<rotate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x32,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
add    $0x32,%edi
call   1060 <alloca@plt>
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x230(%rbp),%rax
mov    %rax,-0x240(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   10a0 <gmtime@plt>
mov    -0x240(%rbp),%rdi
mov    %eax,%ecx
mov    $0x1f4,%esi
lea    0xdfa(%rip),%rdx
call   1070 <strftime@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%rcx
lea    -0x230(%rbp),%r8
mov    -0x30(%rbp),%rax
mov    %eax,%r9d
lea    0xde7(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <rename@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jge    1268 <rotate+0xd8>
lea    0xdc6(%rip),%rdi
call   1080 <perror@plt>
lea    0xdd1(%rip),%rdi
call   10b0 <fatal@plt>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rotate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x210,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x32(%rax),%r15d
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x32(%rax),%edi
call   1060 <alloca@plt>
mov    %rax,%rbp
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x8(%rsp),%rdi
call   10a0 <gmtime@plt>
lea    0xe24(%rip),%rdx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x1f4,%esi
mov    %eax,%ecx
call   1070 <strftime@plt>
mov    (%rsp),%r9d
lea    0xe17(%rip),%rdx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %rbx,%rcx
mov    %r12,%r8
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1090 <rename@plt>
test   %eax,%eax
js     122b <rotate+0x9b>
add    $0x210,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
lea    0xdf6(%rip),%rdi
call   10b0 <fatal@plt>
jmp    121b <rotate+0x8b>

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
@.str = [18 x i8] c"%Y-%m-%d_%H:%M:%S\00"
@.str.1 = [11 x i8] c"%sa%s.%06d\00"
@.str.2 = [23 x i8] c"error rotating current\00"
@.str.3 = [32 x i8] c"current rotation rename() error\00"
define void @rotate(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.timeval
%8 = alloca [500 x i8]
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 50
store i32 %12, i32* %5
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 50
%16 = call i8* @alloca(i32 %15)
store i8* %16, i8** %6
%17 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%18 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%20 = call i32 @gmtime(i32* %19)
%21 = call i32 @strftime(i8* %18, i32 500, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i8*, i8** %6
%23 = load i32, i32* %5
%24 = load i8*, i8** %3
%25 = getelementptr inbounds [500 x i8], [500 x i8]* %8, i64 0, i64 0
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%27 = load i64, i64* %26
%28 = trunc i64 %27 to i32
%29 = call i32 @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %25, i32 %28)
%30 = load i8*, i8** %4
%31 = load i8*, i8** %6
%32 = call i32 @rename(i8* %30, i8* %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %2
%36 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %35, %2
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
@.str = [18 x i8] c"%Y-%m-%d_%H:%M:%S\00"
@.str.1 = [11 x i8] c"%sa%s.%06d\00"
@.str.2 = [23 x i8] c"error rotating current\00"
@.str.3 = [32 x i8] c"current rotation rename() error\00"
define void @rotate(i8* %0, i8* nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca [500 x i8]
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 50
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, 50
%9 = tail call i8* @alloca(i32 %8)
%10 = bitcast %struct.timeval* %3 to i8*
%11 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%12 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%14 = call i32 @gmtime(i32* %13)
%15 = call i32 @strftime(i8* %12, i32 500, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i32
%19 = call i32 @snprintf(i8* %9, i32 %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %12, i32 %18)
%20 = call i32 @rename(i8* %1, i8* %9)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %22, %2
ret void
}