#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 {
  int st_nlink;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ copy_fd(int, int);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ memmove(char *, char *, scalar_t__);
int mkstemp(char *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
scalar_t__ rename(char *, char const *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strrchr(char *, char);
int ensure_nolinks(const char *filename) {
  struct stat st;
  if (stat(filename, &st) != 0) {
    perror(filename);
    return 1;
  }
  if (st.st_nlink > 1) {
    char *tmpname = malloc(1 + strlen(filename) + 7 + 1), *c;
    int from_fd = -1, to_fd = -1;
    int error = 1;
    if (!tmpname)
      goto fail;
    from_fd = open(filename, O_RDONLY);
    if (from_fd == -1)
      goto fail;
    strcpy(tmpname, filename);
    strcat(tmpname, ".XXXXXX");
    c = strrchr(tmpname, '/');
    if (c == NULL)
      c = tmpname;
    else
      c++;
    memmove(c + 1, c, strlen(c) + 1);
    *c = '.';
    to_fd = mkstemp(tmpname);
    if (to_fd == -1)
      goto fail;
    if (copy_fd(from_fd, to_fd))
      goto fail;
    close(from_fd);
    from_fd = -1;
    close(to_fd);
    to_fd = -1;
    if (rename(tmpname, filename))
      goto fail;
    error = 0;
  fail:
    if (error)
      perror(filename);
    if (from_fd != -1)
      close(from_fd);
    if (to_fd != -1)
      close(to_fd);
    free(tmpname);
    return error;
  } else
    return 0;
}


x86 assembly (clang, -O0):
<ensure_nolinks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     1218 <ensure_nolinks+0x38>
mov    -0x10(%rbp),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13df <ensure_nolinks+0x1ff>
cmpl   $0x1,-0x18(%rbp)
jle    13d8 <ensure_nolinks+0x1f8>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
add    $0x7,%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
movl   $0xffffffff,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1268 <ensure_nolinks+0x88>
jmp    138d <ensure_nolinks+0x1ad>
mov    -0x10(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    128c <ensure_nolinks+0xac>
jmp    138d <ensure_nolinks+0x1ad>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xd5c(%rip),%rsi
call   10f0 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d3 <ensure_nolinks+0xf3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    12df <ensure_nolinks+0xff>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   10b0 <memmove@plt>
mov    -0x28(%rbp),%rax
movb   $0x2e,(%rax)
mov    -0x20(%rbp),%rdi
call   10a0 <mkstemp@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1332 <ensure_nolinks+0x152>
jmp    138d <ensure_nolinks+0x1ad>
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1100 <copy_fd@plt>
cmp    $0x0,%rax
je     134c <ensure_nolinks+0x16c>
jmp    138d <ensure_nolinks+0x1ad>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10e0 <rename@plt>
cmp    $0x0,%rax
je     1386 <ensure_nolinks+0x1a6>
jmp    138d <ensure_nolinks+0x1ad>
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     13a0 <ensure_nolinks+0x1c0>
mov    -0x10(%rbp),%rdi
call   10d0 <perror@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
je     13b2 <ensure_nolinks+0x1d2>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
cmpl   $0xffffffff,-0x30(%rbp)
je     13c4 <ensure_nolinks+0x1e4>
mov    -0x30(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13df <ensure_nolinks+0x1ff>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_nolinks>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    12e2 <ensure_nolinks+0x102>
xor    %ebp,%ebp
cmpl   $0x2,0x8(%rsp)
jl     130e <ensure_nolinks+0x12e>
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0x9(%rax),%rdi
call   1090 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     12d8 <ensure_nolinks+0xf8>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12d8 <ensure_nolinks+0xf8>
mov    %eax,%r12d
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xda5(%rip),%rsi
mov    %r14,%rdi
call   10f0 <strcat@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %r14,%rbx
lea    0x1(%rbx),%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10b0 <memmove@plt>
movb   $0x2e,(%rbx)
mov    %r14,%rdi
call   10a0 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     12f1 <ensure_nolinks+0x111>
mov    %eax,%ebx
mov    %r12d,%edi
mov    %eax,%esi
call   1100 <copy_fd@plt>
test   %rax,%rax
jne    131d <ensure_nolinks+0x13d>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   10e0 <rename@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1306 <ensure_nolinks+0x126>
mov    %r15,%rdi
call   10d0 <perror@plt>
jmp    1301 <ensure_nolinks+0x121>
mov    %r15,%rdi
call   10d0 <perror@plt>
mov    $0x1,%ebp
jmp    130e <ensure_nolinks+0x12e>
mov    %r15,%rdi
call   10d0 <perror@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    $0x1,%ebp
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   10d0 <perror@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%ebx
je     1306 <ensure_nolinks+0x126>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1306 <ensure_nolinks+0x126>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [8 x i8] c".XXXXXX\00"
define i32 @ensure_nolinks(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i64 @stat(i8* %10, %struct.stat* %4)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 @perror(i8* %14)
store i32 1, i32* %2
br label %106
16: ; preds = %1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%18 = load i32, i32* %17
%19 = icmp sgt i32 %18, 1
br i1 %19, label %20, label %105
20: ; preds = %16
%21 = load i8*, i8** %3
%22 = call i64 @strlen(i8* %21)
%23 = add nsw i64 1, %22
%24 = add nsw i64 %23, 7
%25 = add nsw i64 %24, 1
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %5
store i32 -1, i32* %7
store i32 -1, i32* %8
store i32 1, i32* %9
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %20
br label %83
30: ; preds = %20
%31 = load i8*, i8** %3
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %30
br label %83
37: ; preds = %30
%38 = load i8*, i8** %5
%39 = load i8*, i8** %3
%40 = call i32 @strcpy(i8* %38, i8* %39)
%41 = load i8*, i8** %5
%42 = call i32 @strcat(i8* %41, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%43 = load i8*, i8** %5
%44 = call i8* @strrchr(i8* %43, i8 signext 47)
store i8* %44, i8** %6
%45 = load i8*, i8** %6
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %37
%48 = load i8*, i8** %5
store i8* %48, i8** %6
br label %52
49: ; preds = %37
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %6
br label %52
52: ; preds = %49, %47
%53 = load i8*, i8** %6
%54 = getelementptr inbounds i8, i8* %53, i64 1
%55 = load i8*, i8** %6
%56 = load i8*, i8** %6
%57 = call i64 @strlen(i8* %56)
%58 = add nsw i64 %57, 1
%59 = call i32 @memmove(i8* %54, i8* %55, i64 %58)
%60 = load i8*, i8** %6
store i8 46, i8* %60
%61 = load i8*, i8** %5
%62 = call i32 @mkstemp(i8* %61)
store i32 %62, i32* %8
%63 = load i32, i32* %8
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %66
65: ; preds = %52
br label %83
66: ; preds = %52
%67 = load i32, i32* %7
%68 = load i32, i32* %8
%69 = call i64 @copy_fd(i32 %67, i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %66
br label %83
72: ; preds = %66
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
store i32 -1, i32* %7
%75 = load i32, i32* %8
%76 = call i32 @close(i32 %75)
store i32 -1, i32* %8
%77 = load i8*, i8** %5
%78 = load i8*, i8** %3
%79 = call i64 @rename(i8* %77, i8* %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %72
br label %83
82: ; preds = %72
store i32 0, i32* %9
br label %83
83: ; preds = %82, %81, %71, %65, %36, %29
%84 = load i32, i32* %9
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = load i8*, i8** %3
%88 = call i32 @perror(i8* %87)
br label %89
89: ; preds = %86, %83
%90 = load i32, i32* %7
%91 = icmp ne i32 %90, -1
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32, i32* %7
%94 = call i32 @close(i32 %93)
br label %95
95: ; preds = %92, %89
%96 = load i32, i32* %8
%97 = icmp ne i32 %96, -1
br i1 %97, label %98, label %101
98: ; preds = %95
%99 = load i32, i32* %8
%100 = call i32 @close(i32 %99)
br label %101
101: ; preds = %98, %95
%102 = load i8*, i8** %5
%103 = call i32 @free(i8* %102)
%104 = load i32, i32* %9
store i32 %104, i32* %2
br label %106
105: ; preds = %16
store i32 0, i32* %2
br label %106
106: ; preds = %105, %101, %13
%107 = load i32, i32* %2
ret i32 %107
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [8 x i8] c".XXXXXX\00"
define i32 @ensure_nolinks(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = call i32 @perror(i8* %0)
br label %57
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %57
12: ; preds = %8
%13 = call i64 @strlen(i8* dereferenceable(1) %0)
%14 = add nsw i64 %13, 9
%15 = call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %44, label %17
17: ; preds = %12
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %0, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %44, label %21
21: ; preds = %17
%22 = call i32 @strcpy(i8* %15, i8* %0)
%23 = call i32 @strcat(i8* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%24 = call i8* @strrchr(i8* %15, i8 signext 47)
%25 = icmp eq i8* %24, null
%26 = getelementptr inbounds i8, i8* %24, i64 1
%27 = select i1 %25, i8* %15, i8* %26
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = call i64 @strlen(i8* %27)
%30 = add nsw i64 %29, 1
%31 = call i32 @memmove(i8* %28, i8* %27, i64 %30)
store i8 46, i8* %27
%32 = call i32 @mkstemp(i8* %15)
%33 = icmp eq i32 %32, -1
br i1 %33, label %51, label %34
34: ; preds = %21
%35 = call i64 @copy_fd(i32 %19, i32 %32)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = call i32 @close(i32 %19)
%39 = call i32 @close(i32 %32)
%40 = call i64 @rename(i8* %15, i8* %0)
%41 = icmp eq i64 %40, 0
br i1 %41, label %54, label %42
42: ; preds = %37
%43 = call i32 @perror(i8* %0)
br label %54
44: ; preds = %12, %17
%45 = call i32 @perror(i8* %0)
br label %54
46: ; preds = %34
%47 = call i32 @perror(i8* %0)
%48 = call i32 @close(i32 %19)
br i1 %33, label %54, label %49
49: ; preds = %46
%50 = call i32 @close(i32 %32)
br label %54
51: ; preds = %21
%52 = call i32 @perror(i8* %0)
%53 = call i32 @close(i32 %19)
br label %54
54: ; preds = %51, %37, %44, %42, %49, %46
%55 = phi i32 [ 1, %49 ], [ 1, %46 ], [ 1, %44 ], [ 1, %42 ], [ 0, %37 ], [ 1, %51 ]
%56 = call i32 @free(i8* %15)
br label %57
57: ; preds = %8, %54, %6
%58 = phi i32 [ 1, %6 ], [ %55, %54 ], [ 0, %8 ]
ret i32 %58
}