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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ link(char const *, char *);
char *malloc(scalar_t__);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ unlink(char *);
int use_hardlinks_p(const char *tname) {
  char *lname;
  struct stat sb;
  unsigned int nlink;
  int res;
  if (stat(tname, &sb))
    return -1;
  nlink = (unsigned int)sb.st_nlink;
  lname = malloc((strlen(tname) + 1 + 1));
  if (!lname)
    return -1;
  strcpy(lname, tname);
  strcat(lname, "x");
  (void)link(tname, lname);
  if (stat(tname, &sb))
    res = -1;
  else if (sb.st_nlink == nlink + 1)
    res = 0;
  else
    res = 1;
  unlink(lname);
  free((lname));
  return res;
}


x86 assembly (clang, -O0):
<use_hardlinks_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11af <use_hardlinks_p+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <use_hardlinks_p+0xf7>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e9 <use_hardlinks_p+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <use_hardlinks_p+0xf7>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <link@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     1236 <use_hardlinks_p+0xb6>
movl   $0xffffffff,-0x28(%rbp)
jmp    125f <use_hardlinks_p+0xdf>
mov    -0x20(%rbp),%eax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    1253 <use_hardlinks_p+0xd3>
movl   $0x0,-0x28(%rbp)
jmp    125a <use_hardlinks_p+0xda>
movl   $0x1,-0x28(%rbp)
jmp    125f <use_hardlinks_p+0xdf>
mov    -0x18(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<use_hardlinks_p>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1070 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    120d <use_hardlinks_p+0x8d>
mov    (%rsp),%r15d
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x2(%rax),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     120d <use_hardlinks_p+0x8d>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
lea    0xe35(%rip),%rsi
mov    %r14,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <link@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
add    $0x1,%r15d
xor    %ecx,%ecx
cmp    %r15d,(%rsp)
setne  %cl
neg    %rax
sbb    %ebp,%ebp
or     %ecx,%ebp
mov    %r14,%rdi
call   1040 <unlink@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"x\00"
define i32 @use_hardlinks_p(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @stat(i8* %8, %struct.stat* %5)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %51
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%14 = load i32, i32* %13
store i32 %14, i32* %6
%15 = load i8*, i8** %3
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %16, 1
%18 = add nsw i64 %17, 1
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %12
store i32 -1, i32* %2
br label %51
23: ; preds = %12
%24 = load i8*, i8** %4
%25 = load i8*, i8** %3
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = load i8*, i8** %4
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%29 = load i8*, i8** %3
%30 = load i8*, i8** %4
%31 = call i32 @link(i8* %29, i8* %30)
%32 = load i8*, i8** %3
%33 = call i64 @stat(i8* %32, %struct.stat* %5)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %23
store i32 -1, i32* %7
br label %45
36: ; preds = %23
%37 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%38 = load i32, i32* %37
%39 = load i32, i32* %6
%40 = add i32 %39, 1
%41 = icmp eq i32 %38, %40
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 0, i32* %7
br label %44
43: ; preds = %36
store i32 1, i32* %7
br label %44
44: ; preds = %43, %42
br label %45
45: ; preds = %44, %35
%46 = load i8*, i8** %4
%47 = call i32 @unlink(i8* %46)
%48 = load i8*, i8** %4
%49 = call i32 @free(i8* %48)
%50 = load i32, i32* %7
store i32 %50, i32* %2
br label %51
51: ; preds = %45, %22, %11
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"x\00"
define i32 @use_hardlinks_p(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 %6, label %26
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = call i64 @strlen(i8* dereferenceable(1) %0)
%10 = add nsw i64 %9, 2
%11 = call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %26, label %13
13: ; preds = %6
%14 = call i32 @strcpy(i8* %11, i8* %0)
%15 = call i32 @strcat(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @link(i8* %0, i8* %11)
%17 = call i64 @stat(i8* %0, %struct.stat* %2)
%18 = icmp eq i64 %17, 0
%19 = load i32, i32* %7
%20 = add i32 %8, 1
%21 = icmp ne i32 %19, %20
%22 = zext i1 %21 to i32
%23 = select i1 %18, i32 %22, i32 -1
%24 = call i32 @unlink(i8* %11)
%25 = call i32 @free(i8* %11)
br label %26
26: ; preds = %6, %1, %13
%27 = phi i32 [ %23, %13 ], [ -1, %1 ], [ -1, %6 ]
ret i32 %27
}