#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ die(char *, char *, ...);
int /*<<< orphan*/ errno;
scalar_t__ readlink(char *, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void test_readlink(void) {
  char buf[4096];
  ssize_t ret;
  bzero(buf, sizeof(buf));
  ret = readlink("/tmp/symlink", buf, sizeof(buf));
  if (ret < 0)
    die("readlink failed: %s\n", strerror(errno));
  if (strcmp(buf, "/tmp/data") != 0)
    die("readlink strcmp failed: '%s' '%s'\n", buf, "/tmp/data");
}


x86 assembly (clang, -O0):
<test_readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
lea    -0x1000(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <bzero@plt>
lea    -0x1000(%rbp),%rsi
lea    0xe86(%rip),%rdi
mov    $0x1000,%edx
call   1030 <readlink@plt>
mov    %rax,-0x1008(%rbp)
cmpq   $0x0,-0x1008(%rbp)
jge    11b8 <test_readlink+0x68>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
movslq %eax,%rsi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1050 <die@plt>
lea    -0x1000(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11f1 <test_readlink+0xa1>
lea    -0x1000(%rbp),%rsi
lea    0xe49(%rip),%rdi
lea    0xe38(%rip),%rdx
mov    $0x0,%al
call   1050 <die@plt>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_readlink>:
push   %rbx
sub    $0x1000,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <bzero@plt>
lea    0xe91(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <readlink@plt>
test   %rax,%rax
jns    11a0 <test_readlink+0x50>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
movslq %eax,%rsi
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1050 <die@plt>
lea    0xe7b(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11cc <test_readlink+0x7c>
lea    0xe71(%rip),%rdi
lea    0xe60(%rip),%rdx
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <die@plt>
add    $0x1000,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/tmp/symlink\00"
@.str.1 = [21 x i8] c"readlink failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"/tmp/data\00"
@.str.3 = [35 x i8] c"readlink strcmp failed: '%s' '%s'\0A\00"
define void @test_readlink() {
%1 = alloca [4096 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%4 = call i32 @bzero(i8* %3, i32 4096)
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%6 = call i64 @readlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %5, i32 4096)
store i64 %6, i64* %2
%7 = load i64, i64* %2
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i8*
%14 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %9, %0
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%21 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %19, %15
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/tmp/symlink\00"
@.str.1 = [21 x i8] c"readlink failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"/tmp/data\00"
@.str.3 = [35 x i8] c"readlink strcmp failed: '%s' '%s'\0A\00"
define void @test_readlink() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = call i32 @bzero(i8* %2, i32 4096)
%4 = call i64 @readlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %2, i32 4096)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %6, %0
%13 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 (i8*, i8*, ...) @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
ret void
}