#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 */
typedef int mode_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int readlink(char *, char *, int);
int symlink(char *, char *);
int main() {
  int fd;
  int link_result;
  char buf[255];
  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  char *filename = "./symlink_org.txt";
  fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
  link_result = symlink("symlink_org.txt", "symlink.txt");
  if (link_result == -1) {
    printf("error\n");
    return link_result;
  }
  link_result = readlink("symlink.txt", buf, 255);
  if (link_result == -1) {
    printf("error\n");
    return link_result;
  }
  buf[link_result] = '\0';
  printf("symlink.txt : READLINK = %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    0x2e76(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e4c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x114(%rbp)
lea    0xe7d(%rip),%rax
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    -0x114(%rbp),%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
lea    0xe41(%rip),%rdi
lea    0xe4a(%rip),%rsi
call   1050 <symlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11f3 <main+0xb3>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1259 <main+0x119>
lea    -0x110(%rbp),%rsi
lea    0xe11(%rip),%rdi
mov    $0xff,%edx
call   1030 <readlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1231 <main+0xf1>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1259 <main+0x119>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x100,%rsp
mov    0x2e59(%rip),%rax
mov    0x2e72(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e57(%rip),%rax
or     (%rax),%edx
mov    0x2e46(%rip),%rax
or     (%rax),%edx
mov    0x2e25(%rip),%rax
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
lea    0xe62(%rip),%rdi
call   1070 <open@plt>
lea    0xe58(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1060 <symlink@plt>
cmp    $0xffffffff,%eax
je     11ef <main+0x9f>
lea    0xe50(%rip),%rdi
mov    %rsp,%rsi
mov    $0xff,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11ef <main+0x9f>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe3d(%rip),%rdi
xor    %ebx,%ebx
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1200 <main+0xb0>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [18 x i8] c"./symlink_org.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [16 x i8] c"symlink_org.txt\00"
@.str.2 = [12 x i8] c"symlink.txt\00"
@.str.3 = [7 x i8] c"error\0A\00"
@.str.4 = [29 x i8] c"symlink.txt : READLINK = %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = alloca i32
%6 = alloca i8*
store i32 0, i32* %1
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
store i32 %13, i32* %5
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %6
%14 = load i8*, i8** %6
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* %5
%21 = call i32 @open(i8* %14, i32 %19, i32 %20)
store i32 %21, i32* %2
%22 = call i32 @symlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* %3
store i32 %27, i32* %1
br label %42
28: ; preds = %0
%29 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%30 = call i32 @readlink(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %29, i32 255)
store i32 %30, i32* %3
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* %3
store i32 %35, i32* %1
br label %42
36: ; preds = %28
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %40)
store i32 0, i32* %1
br label %42
42: ; preds = %36, %33, %25
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [18 x i8] c"./symlink_org.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [16 x i8] c"symlink_org.txt\00"
@.str.2 = [12 x i8] c"symlink.txt\00"
@.str.4 = [29 x i8] c"symlink.txt : READLINK = %s\0A\00"
@str.5 = [6 x i8] c"error\00"
define i32 @main() {
%1 = alloca [255 x i8]
%2 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @S_IRUSR
%4 = load i32, i32* @S_IWUSR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRGRP
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IROTH
%9 = or i32 %7, %8
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %9)
%16 = tail call i32 @symlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.5, i64 0, i64 0))
br label %29
20: ; preds = %0
%21 = call i32 @readlink(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32 255)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.5, i64 0, i64 0))
br label %29
25: ; preds = %20
%26 = sext i32 %21 to i64
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 %26
store i8 0, i8* %27
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %29
29: ; preds = %25, %23, %18
%30 = phi i32 [ -1, %18 ], [ -1, %23 ], [ 0, %25 ]
ret i32 %30
}