#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*/ ENOMEM;
int /*<<< orphan*/ assert(int);
char *dirname(char *);
scalar_t__ dirsync(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *newname(char *, int);
int rename(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char *);
int filldentry(char *path, int pat) {
  char *new = NULL;
  char *cpy = NULL;
  char *dir;
  if (!(new = newname(path, pat)))
    goto nomem;
  if (!(cpy = strdup(path)))
    goto nomem;
  assert(strlen(cpy) == strlen(new));
  if (rename(path, new) == -1)
    goto error;
  dir = dirname(cpy);
  if (dirsync(dir) < 0)
    goto error;
  free(cpy);
  assert(strlen(path) == strlen(new));
  strcpy(path, new);
  free(new);
  return 0;
nomem:
  errno = ENOMEM;
error:
  if (cpy)
    free(cpy);
  if (new)
    free(new);
  return -1;
}


x86 assembly (clang, -O0):
<filldentry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <newname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ce <filldentry+0x3e>
jmp    12af <filldentry+0x11f>
mov    -0x10(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ea <filldentry+0x5a>
jmp    12af <filldentry+0x11f>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10a0 <rename@plt>
cmp    $0xffffffff,%eax
jne    1232 <filldentry+0xa2>
jmp    12c1 <filldentry+0x131>
mov    -0x28(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <dirsync@plt>
cmp    $0x0,%rax
jge    1257 <filldentry+0xc7>
jmp    12c1 <filldentry+0x131>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f0 <filldentry+0x160>
mov    0x2d2a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d19(%rip),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     12d5 <filldentry+0x145>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
je     12e9 <filldentry+0x159>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filldentry>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1090 <newname@plt>
test   %rax,%rax
je     1246 <filldentry+0xb6>
mov    %rax,%rbx
mov    %r14,%rdi
call   10b0 <strdup@plt>
test   %rax,%rax
je     125a <filldentry+0xca>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <strlen@plt>
xor    %edi,%edi
cmp    %rax,%r12
sete   %dil
call   1070 <assert@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <rename@plt>
cmp    $0xffffffff,%eax
je     126e <filldentry+0xde>
mov    %r15,%rdi
call   1060 <dirname@plt>
mov    %rax,%rdi
call   1080 <dirsync@plt>
test   %rax,%rax
js     126e <filldentry+0xde>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <strlen@plt>
xor    %edi,%edi
cmp    %rax,%r15
sete   %dil
call   1070 <assert@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1283 <filldentry+0xf3>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    0x2d82(%rip),%rcx
mov    %eax,(%rcx)
jmp    127e <filldentry+0xee>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    0x2d6e(%rip),%rcx
mov    %eax,(%rcx)
jmp    1276 <filldentry+0xe6>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @filldentry(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %6
store i8* null, i8** %7
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i8* @newname(i8* %9, i32 %10)
store i8* %11, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
br label %54
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %14
br label %54
19: ; preds = %14
%20 = load i8*, i8** %7
%21 = call i64 @strlen(i8* %20)
%22 = load i8*, i8** %6
%23 = call i64 @strlen(i8* %22)
%24 = icmp eq i64 %21, %23
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i8*, i8** %4
%28 = load i8*, i8** %6
%29 = call i32 @rename(i8* %27, i8* %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %19
br label %56
32: ; preds = %19
%33 = load i8*, i8** %7
%34 = call i8* @dirname(i8* %33)
store i8* %34, i8** %8
%35 = load i8*, i8** %8
%36 = call i64 @dirsync(i8* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
br label %56
39: ; preds = %32
%40 = load i8*, i8** %7
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %4
%43 = call i64 @strlen(i8* %42)
%44 = load i8*, i8** %6
%45 = call i64 @strlen(i8* %44)
%46 = icmp eq i64 %43, %45
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i8*, i8** %4
%50 = load i8*, i8** %6
%51 = call i32 @strcpy(i8* %49, i8* %50)
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
store i32 0, i32* %3
br label %69
54: ; preds = %18, %13
%55 = load i32, i32* @ENOMEM
store i32 %55, i32* @errno
br label %56
56: ; preds = %54, %38, %31
%57 = load i8*, i8** %7
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i8*, i8** %7
%61 = call i32 @free(i8* %60)
br label %62
62: ; preds = %59, %56
%63 = load i8*, i8** %6
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i8*, i8** %6
%67 = call i32 @free(i8* %66)
br label %68
68: ; preds = %65, %62
store i32 -1, i32* %3
br label %69
69: ; preds = %68, %39
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @filldentry(i8* %0, i32 %1) {
%3 = tail call i8* @newname(i8* %0, i32 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i32, i32* @ENOMEM
store i32 %6, i32* @errno
br label %37
7: ; preds = %2
%8 = tail call i8* @strdup(i8* %0)
%9 = icmp eq i8* %8, null
br i1 %9, label %33, label %10
10: ; preds = %7
%11 = tail call i64 @strlen(i8* %8)
%12 = tail call i64 @strlen(i8* %3)
%13 = icmp eq i64 %11, %12
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @rename(i8* %0, i8* %3)
%17 = icmp eq i32 %16, -1
br i1 %17, label %31, label %18
18: ; preds = %10
%19 = tail call i8* @dirname(i8* %8)
%20 = tail call i64 @dirsync(i8* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %18
%23 = tail call i32 @free(i8* %8)
%24 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%25 = tail call i64 @strlen(i8* %3)
%26 = icmp eq i64 %24, %25
%27 = zext i1 %26 to i32
%28 = tail call i32 @assert(i32 %27)
%29 = tail call i32 @strcpy(i8* %0, i8* %3)
%30 = tail call i32 @free(i8* %3)
br label %37
31: ; preds = %18, %10
%32 = tail call i32 @free(i8* %8)
br label %35
33: ; preds = %7
%34 = load i32, i32* @ENOMEM
store i32 %34, i32* @errno
br label %35
35: ; preds = %33, %31
%36 = tail call i32 @free(i8* %3)
br label %37
37: ; preds = %5, %35, %22
%38 = phi i32 [ 0, %22 ], [ -1, %35 ], [ -1, %5 ]
ret i32 %38
}