#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int close(int);
int errno;
int link(char *, char *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int unlink(char *);
int test_link(void) {
  int fd, rc;
  struct stat buf;
  char *tmpname = "test-link.dat";
  char *linkname = "test-link.link";
  fd = open(tmpname, O_CREAT | O_RDWR, S_IRWXU);
  if (fd < 0) {
    printf("Fail to create file %s, errno = %d at line %d!\n", tmpname, errno,
           323);
    return (1);
  }
  rc = close(fd);
  if (rc < 0) {
    printf("Fail to close file %s, errno = %d at line %d!\n", tmpname, errno,
           328);
    return (1);
  }
  rc = link(tmpname, linkname);
  if (rc < 0) {
    printf("Fail to add a link %s to file %s, errno = %d at line %d!\n",
           linkname, tmpname, errno, 333);
    return (1);
  }
  rc = stat(tmpname, &buf);
  if (rc < 0) {
    printf("Fail to stat file %s, errno = %d at line %d!\n", tmpname, errno,
           338);
    return (1);
  }
  if (buf.st_nlink != 2) {
    printf("Unexpected link count %d, errno = %d at line %d!\n",
           (int)buf.st_nlink, errno, 342);
    return (1);
  }
  rc = unlink(tmpname);
  if (rc < 0) {
    printf("Fail to unlink %s, errno = %d at line %d!\n", tmpname, errno, 347);
    return (1);
  }
  rc = unlink(linkname);
  if (rc < 0) {
    printf("Fail to unlink %s, errno = %d at line %d!\n", linkname, errno, 352);
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe94(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11db <test_link+0x7b>
mov    -0x18(%rbp),%rsi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
lea    0xe5a(%rip),%rdi
mov    $0x143,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136d <test_link+0x20d>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    121c <test_link+0xbc>
mov    -0x18(%rbp),%rsi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0xe49(%rip),%rdi
mov    $0x148,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136d <test_link+0x20d>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <link@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1267 <test_link+0x107>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2d83(%rip),%rax
mov    (%rax),%ecx
lea    0xe2e(%rip),%rdi
mov    $0x14d,%r8d
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136d <test_link+0x20d>
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12ad <test_link+0x14d>
mov    -0x18(%rbp),%rsi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
lea    0xe21(%rip),%rdi
mov    $0x152,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136d <test_link+0x20d>
cmpl   $0x2,-0x10(%rbp)
je     12e2 <test_link+0x182>
mov    -0x10(%rbp),%esi
mov    0x2d07(%rip),%rax
mov    (%rax),%edx
lea    0xe1a(%rip),%rdi
mov    $0x156,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136d <test_link+0x20d>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1324 <test_link+0x1c4>
mov    -0x18(%rbp),%rsi
mov    0x2cc5(%rip),%rax
mov    (%rax),%edx
lea    0xe0a(%rip),%rdi
mov    $0x15b,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136d <test_link+0x20d>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1366 <test_link+0x206>
mov    -0x20(%rbp),%rsi
mov    0x2c83(%rip),%rax
mov    (%rax),%edx
lea    0xdc8(%rip),%rdi
mov    $0x160,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136d <test_link+0x20d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_link>:
push   %rax
mov    0x2e90(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     122a <test_link+0xca>
mov    %eax,%edi
call   1050 <close@plt>
test   %eax,%eax
js     124b <test_link+0xeb>
lea    0xe5a(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <link@plt>
test   %eax,%eax
js     126c <test_link+0x10c>
lea    0xe3f(%rip),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %eax,%eax
js     1299 <test_link+0x139>
mov    (%rsp),%esi
cmp    $0x2,%esi
jne    12b7 <test_link+0x157>
lea    0xe1c(%rip),%rdi
call   1030 <unlink@plt>
test   %eax,%eax
js     12d5 <test_link+0x175>
lea    0xe16(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    12fd <test_link+0x19d>
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    0xefd(%rip),%rdi
lea    0xdee(%rip),%rsi
mov    $0x160,%ecx
jmp    12f1 <test_link+0x191>
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
lea    0xde3(%rip),%rdi
lea    0xdbf(%rip),%rsi
mov    $0x143,%ecx
jmp    12f1 <test_link+0x191>
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
lea    0xdf2(%rip),%rdi
lea    0xd9e(%rip),%rsi
mov    $0x148,%ecx
jmp    12f1 <test_link+0x191>
mov    0x2d55(%rip),%rax
mov    (%rax),%ecx
lea    0xe00(%rip),%rdi
lea    0xd8b(%rip),%rsi
lea    0xd76(%rip),%rdx
mov    $0x14d,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12f8 <test_link+0x198>
mov    0x2d28(%rip),%rax
mov    (%rax),%edx
lea    0xe0d(%rip),%rdi
lea    0xd50(%rip),%rsi
mov    $0x152,%ecx
jmp    12f1 <test_link+0x191>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edx
lea    0xe1d(%rip),%rdi
mov    $0x156,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12f8 <test_link+0x198>
mov    0x2cec(%rip),%rax
mov    (%rax),%edx
lea    0xe31(%rip),%rdi
lea    0xd14(%rip),%rsi
mov    $0x15b,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [14 x i8] c"test-link.dat\00"
@.str.1 = [15 x i8] c"test-link.link\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.2 = [48 x i8] c"Fail to create file %s, errno = %d at line %d!\0A\00"
@errno = global i32 0
@.str.3 = [47 x i8] c"Fail to close file %s, errno = %d at line %d!\0A\00"
@.str.4 = [58 x i8] c"Fail to add a link %s to file %s, errno = %d at line %d!\0A\00"
@.str.5 = [46 x i8] c"Fail to stat file %s, errno = %d at line %d!\0A\00"
@.str.6 = [50 x i8] c"Unexpected link count %d, errno = %d at line %d!\0A\00"
@.str.7 = [43 x i8] c"Fail to unlink %s, errno = %d at line %d!\0A\00"
define i32 @test_link() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i8*
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %6
%7 = load i8*, i8** %5
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRWXU
%12 = call i32 @open(i8* %7, i32 %10, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = load i8*, i8** %5
%17 = load i32, i32* @errno
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %16, i32 %17, i32 323)
store i32 1, i32* %1
br label %76
19: ; preds = %0
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i8*, i8** %5
%26 = load i32, i32* @errno
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %25, i32 %26, i32 328)
store i32 1, i32* %1
br label %76
28: ; preds = %19
%29 = load i8*, i8** %5
%30 = load i8*, i8** %6
%31 = call i32 @link(i8* %29, i8* %30)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = load i8*, i8** %5
%37 = load i32, i32* @errno
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i8* %35, i8* %36, i32 %37, i32 333)
store i32 1, i32* %1
br label %76
39: ; preds = %28
%40 = load i8*, i8** %5
%41 = call i32 @stat(i8* %40, %struct.stat* %4)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i8*, i8** %5
%46 = load i32, i32* @errno
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i8* %45, i32 %46, i32 338)
store i32 1, i32* %1
br label %76
48: ; preds = %39
%49 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%50 = load i32, i32* %49
%51 = icmp ne i32 %50, 2
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%54 = load i32, i32* %53
%55 = load i32, i32* @errno
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 %55, i32 342)
store i32 1, i32* %1
br label %76
57: ; preds = %48
%58 = load i8*, i8** %5
%59 = call i32 @unlink(i8* %58)
store i32 %59, i32* %3
%60 = load i32, i32* %3
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %57
%63 = load i8*, i8** %5
%64 = load i32, i32* @errno
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* %63, i32 %64, i32 347)
store i32 1, i32* %1
br label %76
66: ; preds = %57
%67 = load i8*, i8** %6
%68 = call i32 @unlink(i8* %67)
store i32 %68, i32* %3
%69 = load i32, i32* %3
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %66
%72 = load i8*, i8** %6
%73 = load i32, i32* @errno
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* %72, i32 %73, i32 352)
store i32 1, i32* %1
br label %76
75: ; preds = %66
store i32 0, i32* %1
br label %76
76: ; preds = %75, %71, %62, %52, %44, %34, %24, %15
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [14 x i8] c"test-link.dat\00"
@.str.1 = [15 x i8] c"test-link.link\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.2 = [48 x i8] c"Fail to create file %s, errno = %d at line %d!\0A\00"
@errno = global i32 0
@.str.3 = [47 x i8] c"Fail to close file %s, errno = %d at line %d!\0A\00"
@.str.4 = [58 x i8] c"Fail to add a link %s to file %s, errno = %d at line %d!\0A\00"
@.str.5 = [46 x i8] c"Fail to stat file %s, errno = %d at line %d!\0A\00"
@.str.6 = [50 x i8] c"Unexpected link count %d, errno = %d at line %d!\0A\00"
@.str.7 = [43 x i8] c"Fail to unlink %s, errno = %d at line %d!\0A\00"
define i32 @test_link() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRWXU
%7 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10, i32 323)
br label %49
12: ; preds = %0
%13 = tail call i32 @close(i32 %7)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %16, i32 328)
br label %49
18: ; preds = %12
%19 = tail call i32 @link(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %22, i32 333)
br label %49
24: ; preds = %18
%25 = call i32 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %28, i32 338)
br label %49
30: ; preds = %24
%31 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 2
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = load i32, i32* @errno
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i32 %32, i32 %35, i32 342)
br label %49
37: ; preds = %30
%38 = tail call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %41, i32 347)
br label %49
43: ; preds = %37
%44 = tail call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* @errno
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %47, i32 352)
br label %49
49: ; preds = %43, %46, %40, %34, %27, %21, %15, %9
%50 = phi i32 [ 1, %9 ], [ 1, %15 ], [ 1, %21 ], [ 1, %27 ], [ 1, %34 ], [ 1, %40 ], [ 1, %46 ], [ 0, %43 ]
ret i32 %50
}