#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 stat {
  scalar_t__ st_size;
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ fpath;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchown(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, scalar_t__);
int open(char *, int, int /*<<< orphan*/);
int openat(int, char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ rename(char *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
scalar_t__ unlink(char const *);
scalar_t__ write(int, void *, scalar_t__);
int do_cow_name(int dirfd, char const *name) {
  int nfd, sfd;
  void *addr;
  struct stat stb;
  char fpath[1024];
  if ((sfd = openat(dirfd, name, O_RDONLY, 0)) == -1)
    return -1;
  if (fstat(sfd, &stb)) {
    close(sfd);
    return -1;
  }
  snprintf(fpath, sizeof(fpath) - 1, "%s,,+++", name);
  if ((nfd = open(fpath, O_CREAT | O_EXCL | O_WRONLY, stb.st_mode)) == -1) {
    close(sfd);
    return -1;
  }
  if ((addr = mmap(NULL, stb.st_size, PROT_READ, MAP_PRIVATE, sfd, 0)) ==
      MAP_FAILED) {
    close(nfd);
    unlink(fpath);
    close(sfd);
    return -1;
  }
  if (write(nfd, addr, stb.st_size) != stb.st_size) {
    munmap(addr, stb.st_size);
    close(nfd);
    unlink(fpath);
    close(sfd);
    return -1;
  }
  munmap(addr, stb.st_size);
  close(sfd);
  fchown(nfd, stb.st_uid, stb.st_gid);
  close(nfd);
  if (unlink(name)) {
    unlink(fpath);
    return -1;
  }
  rename(fpath, name);
  return 0;
}


x86 assembly (clang, -O0):
<do_cow_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1050 <openat@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11f1 <do_cow_name+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <do_cow_name+0x1ef>
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0x0,%rax
je     121b <do_cow_name+0x6b>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <do_cow_name+0x1ef>
lea    -0x440(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x3ff,%esi
lea    0xdce(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x440(%rbp),%rdi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    0x2d7a(%rip),%rax
or     (%rax),%esi
mov    0x2d51(%rip),%rax
or     (%rax),%esi
mov    -0x28(%rbp),%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1281 <do_cow_name+0xd1>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <do_cow_name+0x1ef>
mov    -0x38(%rbp),%rsi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    0x2d2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d06(%rip),%rcx
cmp    (%rcx),%rax
jne    12e3 <do_cow_name+0x133>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
lea    -0x440(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <do_cow_name+0x1ef>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <write@plt>
cmp    -0x38(%rbp),%rax
je     1332 <do_cow_name+0x182>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
lea    -0x440(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <do_cow_name+0x1ef>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x14(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
call   10b0 <fchown@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     1388 <do_cow_name+0x1d8>
lea    -0x440(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139f <do_cow_name+0x1ef>
lea    -0x440(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <rename@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_cow_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%rbx
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1050 <openat@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     12f3 <do_cow_name+0x143>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
test   %rax,%rax
jne    12ec <do_cow_name+0x13c>
lea    0xdff(%rip),%rdx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x3ff,%esi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
mov    0x2ddb(%rip),%rax
mov    0x2da4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d79(%rip),%rax
or     (%rax),%esi
mov    0x18(%rsp),%edx
mov    %r15,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12ec <do_cow_name+0x13c>
mov    %eax,%r15d
mov    0x8(%rsp),%r12
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    0x2d62(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%r13
mov    0x2d3e(%rip),%rax
cmp    (%rax),%r13
je     12da <do_cow_name+0x12a>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1040 <write@plt>
mov    %rax,(%rsp)
mov    %r13,%rdi
mov    %r12,%rsi
call   1090 <munmap@plt>
cmp    %r12,(%rsp)
jne    12da <do_cow_name+0x12a>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x10(%rsp),%edx
mov    0x14(%rsp),%esi
mov    %r15d,%edi
call   10b0 <fchown@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
lea    0x20(%rsp),%rdi
test   %rax,%rax
je     1308 <do_cow_name+0x158>
call   1030 <unlink@plt>
jmp    12f3 <do_cow_name+0x143>
mov    %r15d,%edi
call   1080 <close@plt>
lea    0x20(%rsp),%rdi
call   1030 <unlink@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rsi
call   10c0 <rename@plt>
xor    %r14d,%r14d
jmp    12f3 <do_cow_name+0x143>

LLVM IR (-O0):
%struct.stat = type { i64, i32, i32, i32 }
@O_RDONLY = global i32 0
@.str = [8 x i8] c"%s,,+++\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i32 @do_cow_name(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.stat
%10 = alloca [1024 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%11 = load i32, i32* %4
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @openat(i32 %11, i8* %12, i32 %13, i32 0)
store i32 %14, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %102
17: ; preds = %2
%18 = load i32, i32* %7
%19 = call i64 @fstat(i32 %18, %struct.stat* %9)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %3
br label %102
24: ; preds = %17
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%26 = load i8*, i8** %5
%27 = call i32 @snprintf(i8* %25, i32 1023, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_EXCL
%31 = or i32 %29, %30
%32 = load i32, i32* @O_WRONLY
%33 = or i32 %31, %32
%34 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 3
%35 = load i32, i32* %34
%36 = call i32 @open(i8* %28, i32 %33, i32 %35)
store i32 %36, i32* %6
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %24
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %3
br label %102
41: ; preds = %24
%42 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @MAP_PRIVATE
%46 = load i32, i32* %7
%47 = call i8* @mmap(i32* null, i64 %43, i32 %44, i32 %45, i32 %46, i32 0)
store i8* %47, i8** %8
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp eq i8* %47, %48
br i1 %49, label %50, label %57
50: ; preds = %41
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%54 = call i64 @unlink(i8* %53)
%55 = load i32, i32* %7
%56 = call i32 @close(i32 %55)
store i32 -1, i32* %3
br label %102
57: ; preds = %41
%58 = load i32, i32* %6
%59 = load i8*, i8** %8
%60 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%61 = load i64, i64* %60
%62 = call i64 @write(i32 %58, i8* %59, i64 %61)
%63 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%64 = load i64, i64* %63
%65 = icmp ne i64 %62, %64
br i1 %65, label %66, label %77
66: ; preds = %57
%67 = load i8*, i8** %8
%68 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%69 = load i64, i64* %68
%70 = call i32 @munmap(i8* %67, i64 %69)
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%74 = call i64 @unlink(i8* %73)
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
store i32 -1, i32* %3
br label %102
77: ; preds = %57
%78 = load i8*, i8** %8
%79 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%80 = load i64, i64* %79
%81 = call i32 @munmap(i8* %78, i64 %80)
%82 = load i32, i32* %7
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* %6
%85 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%86 = load i32, i32* %85
%87 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%88 = load i32, i32* %87
%89 = call i32 @fchown(i32 %84, i32 %86, i32 %88)
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
%92 = load i8*, i8** %5
%93 = call i64 @unlink(i8* %92)
%94 = icmp ne i64 %93, 0
br i1 %94, label %95, label %98
95: ; preds = %77
%96 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%97 = call i64 @unlink(i8* %96)
store i32 -1, i32* %3
br label %102
98: ; preds = %77
%99 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%100 = load i8*, i8** %5
%101 = call i32 @rename(i8* %99, i8* %100)
store i32 0, i32* %3
br label %102
102: ; preds = %98, %95, %66, %50, %38, %21, %16
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
%struct.stat = type { i64, i32, i32, i32 }
@O_RDONLY = global i32 0
@.str = [8 x i8] c"%s,,+++\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i32 @do_cow_name(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @openat(i32 %0, i8* %1, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %62, label %10
10: ; preds = %2
%11 = call i64 @fstat(i32 %8, %struct.stat* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @close(i32 %8)
br label %62
15: ; preds = %10
%16 = call i32 @snprintf(i8* %6, i32 1023, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %1)
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_EXCL
%19 = or i32 %18, %17
%20 = load i32, i32* @O_WRONLY
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 3
%23 = load i32, i32* %22
%24 = call i32 @open(i8* %6, i32 %21, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @close(i32 %8)
br label %62
28: ; preds = %15
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%30 = load i64, i64* %29
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @MAP_PRIVATE
%33 = call i8* @mmap(i32* null, i64 %30, i32 %31, i32 %32, i32 %8, i32 0)
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %40
36: ; preds = %28
%37 = call i32 @close(i32 %24)
%38 = call i64 @unlink(i8* %6)
%39 = call i32 @close(i32 %8)
br label %62
40: ; preds = %28
%41 = call i64 @write(i32 %24, i8* %33, i64 %30)
%42 = icmp eq i64 %41, %30
%43 = call i32 @munmap(i8* %33, i64 %30)
br i1 %42, label %48, label %44
44: ; preds = %40
%45 = call i32 @close(i32 %24)
%46 = call i64 @unlink(i8* %6)
%47 = call i32 @close(i32 %8)
br label %62
48: ; preds = %40
%49 = call i32 @close(i32 %8)
%50 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%53 = load i32, i32* %52
%54 = call i32 @fchown(i32 %24, i32 %51, i32 %53)
%55 = call i32 @close(i32 %24)
%56 = call i64 @unlink(i8* %1)
%57 = icmp eq i64 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %48
%59 = call i64 @unlink(i8* %6)
br label %62
60: ; preds = %48
%61 = call i32 @rename(i8* %6, i8* %1)
br label %62
62: ; preds = %2, %60, %58, %44, %36, %26, %13
%63 = phi i32 [ -1, %13 ], [ -1, %26 ], [ -1, %36 ], [ -1, %44 ], [ -1, %58 ], [ 0, %60 ], [ -1, %2 ]
ret i32 %63
}