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

/* Variables and functions */
int /*<<< orphan*/ errno;
size_t readlink(char *, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ test_msg(char *, char *, int, int, int /*<<< orphan*/,
                            int /*<<< orphan*/);
int read_fd_link(int lfd, char *buf, size_t size) {
  ssize_t ret;
  char t[32];
  snprintf(t, sizeof(t), "/proc/self/fd/%d", lfd);
  ret = readlink(t, buf, size - 1);
  if (ret < 0) {
    test_msg("ERR: %s:%d: "
             "Can't read link of fd %d"
             " (errno = %d (%s))\n",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/"
             "master/test/zdtm/live/static/proc-self.c",
             29, lfd, errno, strerror(errno));
    return -1;
  }
  buf[ret] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<read_fd_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xe9a(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <readlink@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11dd <read_fd_link+0x9d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x48(%rbp),%ecx
mov    -0x44(%rbp),%r8d
mov    %eax,%r9d
lea    0xe51(%rip),%rdi
lea    0xe82(%rip),%rsi
mov    $0x1d,%edx
call   1050 <test_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <read_fd_link+0xb0>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_fd_link>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xec8(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
add    $0xffffffffffffffff,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <readlink@plt>
movb   $0x0,(%r14,%rax,1)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [56 x i8] c"ERR: %s:%d: Can't read link of fd %d (errno = %d (%s))\0A\00"
@.str.2 = [166 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/master/test/zdtm/live/static/proc-self.c\00"
@errno = global i32 0
define i32 @read_fd_link(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca [32 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @snprintf(i8* %10, i32 32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = sub i64 %15, 1
%17 = call i64 @readlink(i8* %13, i8* %14, i64 %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp ult i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %3
%21 = load i32, i32* %5
%22 = load i32, i32* @errno
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 @test_msg(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([166 x i8], [166 x i8]* @.str.2, i64 0, i64 0), i32 29, i32 %21, i32 %22, i32 %24)
store i32 -1, i32* %4
br label %30
26: ; preds = %3
%27 = load i8*, i8** %6
%28 = load i64, i64* %8
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8 0, i8* %29
store i32 0, i32* %4
br label %30
30: ; preds = %26, %20
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@errno = global i32 0
define i32 @read_fd_link(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = add i64 %2, -1
%8 = call i64 @readlink(i8* %5, i8* %1, i64 %7)
%9 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 0, i8* %9
ret i32 0
}