#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strlen(char *);
int symlink(char *, char *);
int write(int, char *, int /*<<< orphan*/);
int main(void) {
  int fd;
  char buf[1024];
  int n;
  if (symlink("test.txt", "sl1") == -1) {
    perror("fail to create symbol link");
    exit(1);
  }
  if (symlink("sl1", "sl2") == -1) {
    perror("fail to create symbol link");
    exit(1);
  }
  if ((fd = open("sl2", O_RDWR)) == -1) {
    perror("fail to open");
    exit(1);
  }
  printf("already create symbol-links\n");
  if ((n = read(fd, buf, 1024)) == -1) {
    perror("fail to read");
    exit(1);
  }
  buf[n] = '\0';
  printf("file content is : %s\n", buf);
  strcat(buf, ", admin");
  if (write(fd, buf, strlen(buf)) == -1) {
    perror("fail to write");
    exit(1);
  }
  printf("done\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1060 <symlink@plt>
cmp    $0xffffffff,%eax
jne    11e4 <main+0x44>
lea    0xe38(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xe1e(%rip),%rdi
lea    0xe36(%rip),%rsi
call   1060 <symlink@plt>
cmp    $0xffffffff,%eax
jne    1216 <main+0x76>
lea    0xe06(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    0xe02(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    124d <main+0xad>
lea    0xdee(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0xffffffff,%eax
jne    1294 <main+0xf4>
lea    0xdd1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
lea    0xdb3(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x424(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x424(%rbp),%edi
mov    -0x420(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1321 <main+0x181>
lea    0xd6f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
lea    0xe40(%rip),%rdi
lea    0xe42(%rip),%rsi
call   1070 <symlink@plt>
cmp    $0xffffffff,%eax
je     1292 <main+0xe2>
lea    0xe2d(%rip),%rdi
lea    0xe45(%rip),%rsi
call   1070 <symlink@plt>
cmp    $0xffffffff,%eax
je     1292 <main+0xe2>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     129b <main+0xeb>
mov    %eax,%ebp
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     12a4 <main+0xf4>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe08(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe0a(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <strcat@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12ad <main+0xfd>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd74(%rip),%rdi
jmp    12b4 <main+0x104>
lea    0xd8a(%rip),%rdi
jmp    12b4 <main+0x104>
lea    0xd8e(%rip),%rdi
jmp    12b4 <main+0x104>
lea    0xdb0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [4 x i8] c"sl1\00"
@.str.2 = [27 x i8] c"fail to create symbol link\00"
@.str.3 = [4 x i8] c"sl2\00"
@O_RDWR = global i32 0
@.str.4 = [13 x i8] c"fail to open\00"
@.str.5 = [29 x i8] c"already create symbol-links\0A\00"
@.str.6 = [13 x i8] c"fail to read\00"
@.str.7 = [22 x i8] c"file content is : %s\0A\00"
@.str.8 = [8 x i8] c", admin\00"
@.str.9 = [14 x i8] c"fail to write\00"
@.str.10 = [6 x i8] c"done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @symlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i32 @symlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %17)
store i32 %18, i32* %2
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 1024)
store i32 %27, i32* %4
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %23
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%39 = call i32 @strcat(i8* %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%40 = load i32, i32* %2
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = call i32 @write(i32 %40, i8* %41, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %32
%47 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %32
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0))
%51 = load i32, i32* %2
%52 = call i32 @close(i32 %51)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [4 x i8] c"sl1\00"
@.str.2 = [27 x i8] c"fail to create symbol link\00"
@.str.3 = [4 x i8] c"sl2\00"
@O_RDWR = global i32 0
@.str.4 = [13 x i8] c"fail to open\00"
@.str.6 = [13 x i8] c"fail to read\00"
@.str.7 = [22 x i8] c"file content is : %s\0A\00"
@.str.8 = [8 x i8] c", admin\00"
@.str.9 = [14 x i8] c"fail to write\00"
@str = [28 x i8] c"already create symbol-links\00"
@str.11 = [5 x i8] c"done\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @symlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 @symlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%23 = call i32 @read(i32 %16, i8* %2, i32 1024)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = sext i32 %23 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%32 = call i32 @strcat(i8* %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%33 = call i32 @strlen(i8* %2)
%34 = call i32 @write(i32 %16, i8* %2, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %28
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.11, i64 0, i64 0))
%41 = call i32 @close(i32 %16)
ret i32 0
}