#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*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int snprintf(char *, size_t, char const *, ...);
int get_log_filename(int pid, const char *dir, const char *pattern, char *path,
                     size_t size) {
  int idx = 0;
  do {
    int offset = snprintf(path, size, "%s/", dir);
    snprintf(path + offset, size - offset, pattern, pid, idx++);
  } while (access(path, F_OK) == 0);
  return 0;
}


x86 assembly (clang, -O0):
<get_log_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0xeab(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
movslq -0x30(%rbp),%rax
sub    %rax,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x4(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    %r8d,%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1040 <access@plt>
cmp    $0x0,%rax
je     1142 <get_log_filename+0x22>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_log_filename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%rbx
mov    %rdx,(%rsp)
mov    %rsi,%r12
mov    %edi,%ebp
xor    %r13d,%r13d
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
lea    0xeb3(%rip),%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
cltq
lea    (%rbx,%rax,1),%rdi
mov    %r14,%rsi
sub    %rax,%rsi
lea    0x1(%r13),%r15d
mov    (%rsp),%rdx
mov    %ebp,%ecx
mov    %r13d,%r8d
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <access@plt>
mov    %r15d,%r13d
test   %rax,%rax
je     1140 <get_log_filename+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s/\00"
@F_OK = global i32 0
define i32 @get_log_filename(i32 %0, i8* %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i64 %4, i64* %10
store i32 0, i32* %11
br label %13
13: ; preds = %31, %5
%14 = load i8*, i8** %9
%15 = load i64, i64* %10
%16 = load i8*, i8** %7
%17 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %14, i64 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 %17, i32* %12
%18 = load i8*, i8** %9
%19 = load i32, i32* %12
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i64, i64* %10
%23 = load i32, i32* %12
%24 = sext i32 %23 to i64
%25 = sub i64 %22, %24
%26 = load i8*, i8** %8
%27 = load i32, i32* %6
%28 = load i32, i32* %11
%29 = add nsw i32 %28, 1
store i32 %29, i32* %11
%30 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %21, i64 %25, i8* %26, i32 %27, i32 %28)
br label %31
31: ; preds = %13
%32 = load i8*, i8** %9
%33 = load i32, i32* @F_OK
%34 = call i64 @access(i8* %32, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %13, label %36
36: ; preds = %31
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s/\00"
@F_OK = global i32 0
define i32 @get_log_filename(i32 %0, i8* %1, i8* nocapture %2, i8* nocapture %3, i64 %4) {
br label %6
6: ; preds = %6, %5
%7 = phi i32 [ 0, %5 ], [ %12, %6 ]
%8 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %3, i64 %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %1)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %3, i64 %9
%11 = sub i64 %4, %9
%12 = add nuw nsw i32 %7, 1
%13 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %10, i64 %11, i8* %2, i32 %0, i32 %7)
%14 = load i32, i32* @F_OK
%15 = tail call i64 @access(i8* %3, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %6, label %17
17: ; preds = %6
ret i32 0
}