#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, n;
  char buf[1024];
  if (symlink("test.txt", "sym_link") == -1) {
    perror("fail to create symbol link");
    exit(1);
  }
  printf("already create a symbol-link\n");
  if ((fd = open("sym_link", O_RDWR)) == -1) {
    perror("fail to open");
    exit(1);
  }
  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, "Linkerist");
  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    $0x420,%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    0xe3d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1229 <main+0x89>
lea    0xe31(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    125f <main+0xbf>
lea    0xe08(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
lea    0xded(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x41c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x41c(%rbp),%edi
mov    -0x418(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12e9 <main+0x149>
lea    0xdab(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xe3f(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1070 <symlink@plt>
cmp    $0xffffffff,%eax
je     1278 <main+0xc8>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1281 <main+0xd1>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     128a <main+0xda>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe24(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe26(%rip),%rsi
mov    %r14,%rdi
call   10c0 <strcat@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1293 <main+0xe3>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd93(%rip),%rdi
jmp    129a <main+0xea>
lea    0xda5(%rip),%rdi
jmp    129a <main+0xea>
lea    0xda9(%rip),%rdi
jmp    129a <main+0xea>
lea    0xdcd(%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 = [9 x i8] c"sym_link\00"
@.str.2 = [27 x i8] c"fail to create symbol link\00"
@.str.3 = [30 x i8] c"already create a symbol-link\0A\00"
@O_RDWR = global i32 0
@.str.4 = [13 x i8] c"fail to open\00"
@.str.5 = [13 x i8] c"fail to read\00"
@.str.6 = [22 x i8] c"file content is : %s\0A\00"
@.str.7 = [10 x i8] c"Linkerist\00"
@.str.8 = [14 x i8] c"fail to write\00"
@.str.9 = [6 x i8] c"done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
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 ([9 x i8], [9 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 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = load i32, i32* %2
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 1024)
store i32 %21, i32* %3
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @strcat(i8* %32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%34 = load i32, i32* %2
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %34, i8* %35, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %26
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %26
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [9 x i8] c"sym_link\00"
@.str.2 = [27 x i8] c"fail to create symbol link\00"
@O_RDWR = global i32 0
@.str.4 = [13 x i8] c"fail to open\00"
@.str.5 = [13 x i8] c"fail to read\00"
@.str.6 = [22 x i8] c"file content is : %s\0A\00"
@.str.7 = [10 x i8] c"Linkerist\00"
@.str.8 = [14 x i8] c"fail to write\00"
@str = [29 x i8] c"already create a symbol-link\00"
@str.10 = [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 ([9 x i8], [9 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = call i32 @read(i32 %11, i8* %2, i32 1024)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = sext i32 %17 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %23
store i8 0, i8* %24
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%26 = call i32 @strcat(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%27 = call i32 @strlen(i8* %2)
%28 = call i32 @write(i32 %11, i8* %2, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %22
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.10, i64 0, i64 0))
%35 = call i32 @close(i32 %11)
ret i32 0
}