#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 /*<<< orphan*/ readlink_result;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ assert(int);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fputc(int, int /*<<< orphan*/);
scalar_t__ readlink(char const *, char *, size_t);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char const *);
scalar_t__ strlen(char const *);
scalar_t__ symlink(char const *, char const *);
int main() {
  const char *target = "./input.txt";
  const char *linkpath = "/sandbox/subdir/test_link";
  char readlink_result[128];
  size_t result_size = sizeof(readlink_result);
  assert(0 == symlink(target, linkpath));
  assert(readlink(linkpath, readlink_result, result_size) ==
         strlen(target) + 1);
  assert(0 == strcmp(readlink_result, target));
  FILE *file = fopen(linkpath, "r");
  assert(file != NULL);
  int c = fgetc(file);
  while (c != EOF) {
    int wrote = fputc(c, stdout);
    assert(wrote != EOF);
    c = fgetc(file);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe68(%rip),%rax
mov    %rax,-0x18(%rbp)
movq   $0x80,-0xa8(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <symlink@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x18(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    -0xa8(%rbp),%rdx
call   1030 <readlink@plt>
mov    %rax,-0xc0(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0xc0(%rbp),%rax
add    $0x1,%rcx
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x18(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0xb0(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0xb4(%rbp)
mov    -0xb4(%rbp),%eax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%eax
je     12df <main+0x15f>
mov    -0xb4(%rbp),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
call   1070 <fputc@plt>
mov    %eax,-0xb8(%rbp)
mov    -0xb8(%rbp),%eax
mov    0x2d2f(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0xb0(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0xb4(%rbp)
jmp    127d <main+0xfd>
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
lea    0xe7d(%rip),%r14
lea    0xe82(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <symlink@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1080 <assert@plt>
mov    %rsp,%rbx
mov    $0x80,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
xor    %edi,%edi
cmp    $0xc,%rax
sete   %dil
call   1080 <assert@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1080 <assert@plt>
lea    0xe41(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1080 <assert@plt>
mov    %r14,%rdi
call   1050 <fgetc@plt>
mov    0x2ddb(%rip),%rbx
cmp    (%rbx),%eax
je     1243 <main+0xd3>
mov    0x2dc0(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %eax,%edi
call   1060 <fputc@plt>
xor    %edi,%edi
cmp    (%rbx),%eax
setne  %dil
call   1080 <assert@plt>
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    (%rbx),%eax
jne    1220 <main+0xb0>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"./input.txt\00"
@.str.1 = [26 x i8] c"/sandbox/subdir/test_link\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i32 0
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [128 x i8]
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8** %3
store i64 128, i64* %5
%9 = load i8*, i8** %2
%10 = load i8*, i8** %3
%11 = call i64 @symlink(i8* %9, i8* %10)
%12 = icmp eq i64 0, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %3
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%17 = load i64, i64* %5
%18 = call i64 @readlink(i8* %15, i8* %16, i64 %17)
%19 = load i8*, i8** %2
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = icmp eq i64 %18, %21
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%26 = load i8*, i8** %2
%27 = call i64 @strcmp(i8* %25, i8* %26)
%28 = icmp eq i64 0, %27
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i8*, i8** %3
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %6
%33 = load i32*, i32** %6
%34 = icmp ne i32* %33, null
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i32*, i32** %6
%38 = call i32 @fgetc(i32* %37)
store i32 %38, i32* %7
br label %39
39: ; preds = %43, %0
%40 = load i32, i32* %7
%41 = load i32, i32* @EOF
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %54
43: ; preds = %39
%44 = load i32, i32* %7
%45 = load i32, i32* @stdout
%46 = call i32 @fputc(i32 %44, i32 %45)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = load i32, i32* @EOF
%49 = icmp ne i32 %47, %48
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i32*, i32** %6
%53 = call i32 @fgetc(i32* %52)
store i32 %53, i32* %7
br label %39
54: ; preds = %39
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str = [12 x i8] c"./input.txt\00"
@.str.1 = [26 x i8] c"/sandbox/subdir/test_link\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i32 0
@stdout = global i32 0
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i64 @symlink(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = call i64 @readlink(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %2, i64 128)
%8 = icmp eq i64 %7, 12
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i32* @fopen(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp ne i32* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i32 @fgetc(i32* %15)
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %19, %20
br i1 %21, label %33, label %22
22: ; preds = %0, %22
%23 = phi i32 [ %30, %22 ], [ %19, %0 ]
%24 = load i32, i32* @stdout
%25 = call i32 @fputc(i32 %23, i32 %24)
%26 = load i32, i32* @EOF
%27 = icmp ne i32 %25, %26
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = call i32 @fgetc(i32* %15)
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %22
33: ; preds = %22, %0
ret i32 0
}