#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*/ ssize_t;

/* Variables and functions */
int __syscall(int, char const *, char *, size_t);
int /*<<< orphan*/ __syscall_ret(int);
ssize_t readlink(const char *restrict path, char *restrict buf,
                 size_t bufsize) {
  char dummy[1];
  if (!bufsize) {
    buf = dummy;
    bufsize = 1;
  }
  int r = __syscall(85, path, buf, bufsize);
  if (buf == dummy && r > 0)
    r = 0;
  return __syscall_ret(r);
}


x86 assembly (clang, -O0):
<readlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    114f <readlink+0x2f>
lea    -0x19(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x55,%edi
call   1030 <__syscall@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
lea    -0x19(%rbp),%rcx
cmp    %rcx,%rax
jne    118a <readlink+0x6a>
cmpl   $0x0,-0x20(%rbp)
jle    118a <readlink+0x6a>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <__syscall_ret@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlink>:
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
mov    $0x1,%ecx
cmovne %rdx,%rcx
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    0x7(%rsp),%r14
cmove  %r14,%rbx
mov    $0x55,%edi
mov    %rbx,%rdx
call   1030 <__syscall@plt>
xor    %edi,%edi
test   %eax,%eax
cmovle %eax,%edi
cmp    %r14,%rbx
cmovne %eax,%edi
call   1040 <__syscall_ret@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @readlink(i8* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [1 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
store i8* %12, i8** %5
store i64 1, i64* %6
br label %13
13: ; preds = %11, %3
%14 = load i8*, i8** %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = call i32 @__syscall(i32 85, i8* %14, i8* %15, i64 %16)
store i32 %17, i32* %8
%18 = load i8*, i8** %5
%19 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = load i32, i32* %8
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 0, i32* %8
br label %25
25: ; preds = %24, %21, %13
%26 = load i32, i32* %8
%27 = call i32 @__syscall_ret(i32 %26)
ret i32 %27
}

LLVM IR (-O2):
define i32 @readlink(i8* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca [1 x i8]
%5 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %2, 0
%7 = select i1 %6, i64 1, i64 %2
%8 = select i1 %6, i8* %5, i8* %1
%9 = call i32 @__syscall(i32 85, i8* %0, i8* %8, i64 %7)
%10 = icmp eq i8* %8, %5
%11 = icmp sgt i32 %9, 0
%12 = select i1 %10, i1 %11, i1 false
%13 = select i1 %12, i32 0, i32 %9
%14 = call i32 @__syscall_ret(i32 %13)
ret i32 %14
}