#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 int /*<<< orphan*/ szOldLogFileName;
typedef int /*<<< orphan*/ szNewLogFileName;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ rename(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
int do_shift_log_files(int iMaxLogNum, const char *szLogFileName) {
  char szOldLogFileName[260];
  char szNewLogFileName[260];
  int i;
  if (0 == szLogFileName)
    return -1;
  for (i = iMaxLogNum - 2; i >= 0; i--) {
    if (0 == i) {
      snprintf(szOldLogFileName, sizeof(szOldLogFileName), "%s", szLogFileName);
    } else {
      snprintf(szOldLogFileName, sizeof(szOldLogFileName), "%s__bak%d",
               szLogFileName, i);
    }
    if (access(szOldLogFileName, F_OK) == 0) {
      snprintf(szNewLogFileName, sizeof(szNewLogFileName), "%s__bak%d",
               szLogFileName, i + 1);
      if (rename(szOldLogFileName, szNewLogFileName) < 0) {
        return -6;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_shift_log_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    115a <do_shift_log_files+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <do_shift_log_files+0x12f>
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jl     1258 <do_shift_log_files+0x128>
xor    %eax,%eax
cmp    -0x234(%rbp),%eax
jne    11a4 <do_shift_log_files+0x74>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x104,%esi
lea    0xe68(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11c9 <do_shift_log_files+0x99>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x234(%rbp),%r8d
mov    $0x104,%esi
lea    0xe41(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1040 <access@plt>
cmp    $0x0,%rax
jne    123f <do_shift_log_files+0x10f>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x234(%rbp),%r8d
add    $0x1,%r8d
mov    $0x104,%esi
lea    0xdf9(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1050 <rename@plt>
cmp    $0x0,%rax
jge    123a <do_shift_log_files+0x10a>
movl   $0xfffffffa,-0x4(%rbp)
jmp    125f <do_shift_log_files+0x12f>
jmp    123f <do_shift_log_files+0x10f>
jmp    1244 <do_shift_log_files+0x114>
mov    -0x234(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x234(%rbp)
jmp    1166 <do_shift_log_files+0x36>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_shift_log_files>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
test   %rsi,%rsi
je     1211 <do_shift_log_files+0xe1>
mov    %edi,%ebp
xor    %eax,%eax
cmp    $0x2,%edi
jl     121a <do_shift_log_files+0xea>
mov    %rsi,%rbx
add    $0xfffffffe,%ebp
lea    0x110(%rsp),%r15
mov    0x2e6c(%rip),%r14
mov    %ebp,%r13d
jmp    118d <do_shift_log_files+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%ebp
cmp    $0xffffffff,%r13d
je     1218 <do_shift_log_files+0xe8>
mov    %r15,%rdi
mov    $0x104,%esi
sub    $0x1,%r13d
jae    11b0 <do_shift_log_files+0x80>
lea    0xe5e(%rip),%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11c4 <do_shift_log_files+0x94>
xchg   %ax,%ax
lea    0xe4c(%rip),%rdx
mov    %rbx,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    (%r14),%esi
mov    %r15,%rdi
call   1040 <access@plt>
test   %rax,%rax
jne    1180 <do_shift_log_files+0x50>
add    $0x1,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x104,%esi
lea    0xe1a(%rip),%rdx
mov    %rbx,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <rename@plt>
test   %rax,%rax
jns    1180 <do_shift_log_files+0x50>
mov    $0xfffffffa,%eax
jmp    121a <do_shift_log_files+0xea>
mov    $0xffffffff,%eax
jmp    121a <do_shift_log_files+0xea>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [10 x i8] c"%s__bak%d\00"
@F_OK = global i32 0
define i32 @do_shift_log_files(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [260 x i8]
%7 = alloca [260 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = icmp eq i8* null, %9
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %52
12: ; preds = %2
%13 = load i32, i32* %4
%14 = sub nsw i32 %13, 2
store i32 %14, i32* %8
br label %15
15: ; preds = %48, %12
%16 = load i32, i32* %8
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %51
18: ; preds = %15
%19 = load i32, i32* %8
%20 = icmp eq i32 0, %19
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = getelementptr inbounds [260 x i8], [260 x i8]* %6, i64 0, i64 0
%23 = load i8*, i8** %5
%24 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %22, i32 260, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %30
25: ; preds = %18
%26 = getelementptr inbounds [260 x i8], [260 x i8]* %6, i64 0, i64 0
%27 = load i8*, i8** %5
%28 = load i32, i32* %8
%29 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %26, i32 260, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %27, i32 %28)
br label %30
30: ; preds = %25, %21
%31 = getelementptr inbounds [260 x i8], [260 x i8]* %6, i64 0, i64 0
%32 = load i32, i32* @F_OK
%33 = call i64 @access(i8* %31, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %30
%36 = getelementptr inbounds [260 x i8], [260 x i8]* %7, i64 0, i64 0
%37 = load i8*, i8** %5
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 1
%40 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %36, i32 260, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %37, i32 %39)
%41 = getelementptr inbounds [260 x i8], [260 x i8]* %6, i64 0, i64 0
%42 = getelementptr inbounds [260 x i8], [260 x i8]* %7, i64 0, i64 0
%43 = call i64 @rename(i8* %41, i8* %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %35
store i32 -6, i32* %3
br label %52
46: ; preds = %35
br label %47
47: ; preds = %46, %30
br label %48
48: ; preds = %47
%49 = load i32, i32* %8
%50 = add nsw i32 %49, -1
store i32 %50, i32* %8
br label %15
51: ; preds = %15
store i32 0, i32* %3
br label %52
52: ; preds = %51, %45, %11
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [10 x i8] c"%s__bak%d\00"
@F_OK = global i32 0
define i32 @do_shift_log_files(i32 %0, i8* %1) {
%3 = alloca [260 x i8]
%4 = alloca [260 x i8]
%5 = getelementptr inbounds [260 x i8], [260 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [260 x i8], [260 x i8]* %4, i64 0, i64 0
%7 = icmp eq i8* %1, null
br i1 %7, label %31, label %8
8: ; preds = %2
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %31
10: ; preds = %8
%11 = add nsw i32 %0, -2
br label %12
12: ; preds = %10, %28
%13 = phi i32 [ %29, %28 ], [ %11, %10 ]
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 260, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %19
17: ; preds = %12
%18 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 260, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %13)
br label %19
19: ; preds = %17, %15
%20 = load i32, i32* @F_OK
%21 = call i64 @access(i8* %5, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = add nuw nsw i32 %13, 1
%25 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 260, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %24)
%26 = call i64 @rename(i8* %5, i8* %6)
%27 = icmp slt i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %19, %23
%29 = add nsw i32 %13, -1
%30 = icmp eq i32 %13, 0
br i1 %30, label %31, label %12
31: ; preds = %23, %28, %8, %2
%32 = phi i32 [ -1, %2 ], [ 0, %8 ], [ -6, %23 ], [ 0, %28 ]
ret i32 %32
}