#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int readlink(char *, char *, int);
int symlink(char *, char *);
int main(void) {
  char buf[1024];
  int fd, 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 ((n = readlink("sl1", buf, 1024)) == -1) {
    perror("fail to read symbol-link");
    exit(1);
  }
  buf[n] = '\0';
  printf("%s\n", buf);
  if ((n = readlink("sl2", buf, 1024)) == -1) {
    perror("fail to read symbol-link");
    exit(1);
  }
  buf[n] = '\0';
  printf("%s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1050 <symlink@plt>
cmp    $0xffffffff,%eax
jne    1194 <main+0x44>
lea    0xe88(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe6e(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1050 <symlink@plt>
cmp    $0xffffffff,%eax
jne    11c6 <main+0x76>
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x418(%rbp)
cmp    $0xffffffff,%eax
jne    1203 <main+0xb3>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movslq -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
lea    -0x410(%rbp),%rsi
lea    0xdf5(%rip),%rdi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x418(%rbp)
cmp    $0xffffffff,%eax
jne    1262 <main+0x112>
lea    0xdd9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movslq -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xdc6(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x400,%rsp
lea    0xea1(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1050 <symlink@plt>
cmp    $0xffffffff,%eax
je     11f6 <main+0xa6>
lea    0xe8e(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1050 <symlink@plt>
cmp    $0xffffffff,%eax
je     11f6 <main+0xa6>
lea    0xe76(%rip),%rdi
mov    %rsp,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
je     11ff <main+0xaf>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe93(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xe64(%rip),%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
je     11ff <main+0xaf>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe62(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
ret
lea    0xe10(%rip),%rdi
jmp    1206 <main+0xb6>
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <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"
@.str.4 = [25 x i8] c"fail to read symbol-link\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32
%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 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%18 = call i32 @readlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %17, i32 1024)
store i32 %18, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %25
store i8 0, i8* %26
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%30 = call i32 @readlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %29, i32 1024)
store i32 %30, i32* %4
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %23
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%40 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %39)
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"
@.str.4 = [25 x i8] c"fail to read symbol-link\00"
@.str.5 = [4 x i8] c"%s\0A\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 = call i32 @readlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32 1024)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = sext i32 %15 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%24 = call i32 @readlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %2, i32 1024)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = sext i32 %24 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %2)
ret i32 0
}