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

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
scalar_t__ asprintf(char **, char *, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ ksft_exit_fail_msg(char *, ...);
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ readlink(char *, char *, int /*<<< orphan*/);
char *fdreadlink(int fd) {
  char *target, *tmp;
  do {
    if (asprintf(&tmp, "/proc/self/fd/%d", fd) < 0)
      ksft_exit_fail_msg(
          "%s:%d %s failed\n",
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/Broadcom/linux-rdma-nxt/refs/heads/"
          "hwpoison/.backup_do_not_remove/tools/testing/selftests/openat2/"
          "helpers.c",
          59, "asprintf");
  } while (0);
  target = malloc(PATH_MAX);
  if (!target)
    ksft_exit_fail_msg("fdreadlink: malloc failed\n");
  memset(target, 0, PATH_MAX);
  do {
    if (readlink(tmp, target, PATH_MAX) < 0)
      ksft_exit_fail_msg(
          "%s:%d %s failed\n",
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/Broadcom/linux-rdma-nxt/refs/heads/"
          "hwpoison/.backup_do_not_remove/tools/testing/selftests/openat2/"
          "helpers.c",
          66, "readlink");
  } while (0);
  free(tmp);
  return target;
}


x86 assembly (clang, -O0):
<fdreadlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe87(%rip),%rsi
call   1080 <asprintf@plt>
cmp    $0x0,%rax
jge    11a9 <fdreadlink+0x49>
lea    0xe82(%rip),%rdi
lea    0xe8c(%rip),%rsi
mov    $0x3b,%edx
lea    0xf46(%rip),%rcx
mov    $0x0,%al
call   1050 <ksft_exit_fail_msg@plt>
jmp    11ae <fdreadlink+0x4e>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d9 <fdreadlink+0x79>
lea    0xf1f(%rip),%rdi
mov    $0x0,%al
call   1050 <ksft_exit_fail_msg@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
call   1040 <readlink@plt>
cmp    $0x0,%rax
jge    122e <fdreadlink+0xce>
lea    0xdfd(%rip),%rdi
lea    0xe07(%rip),%rsi
mov    $0x42,%edx
lea    0xee5(%rip),%rcx
mov    $0x0,%al
call   1050 <ksft_exit_fail_msg@plt>
jmp    1233 <fdreadlink+0xd3>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdreadlink>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%edx
lea    0xe8f(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1080 <asprintf@plt>
test   %rax,%rax
jns    11a1 <fdreadlink+0x41>
lea    0xe8a(%rip),%rdi
lea    0xe94(%rip),%rsi
lea    0xf53(%rip),%rcx
mov    $0x3b,%edx
xor    %eax,%eax
call   1050 <ksft_exit_fail_msg@plt>
mov    0x2e48(%rip),%r14
mov    (%r14),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11cb <fdreadlink+0x6b>
lea    0xf30(%rip),%rdi
xor    %eax,%eax
call   1050 <ksft_exit_fail_msg@plt>
mov    (%r14),%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    0x8(%rsp),%rdi
mov    (%r14),%edx
mov    %rbx,%rsi
call   1040 <readlink@plt>
test   %rax,%rax
jns    120d <fdreadlink+0xad>
lea    0xe1e(%rip),%rdi
lea    0xe28(%rip),%rsi
lea    0xf0b(%rip),%rcx
mov    $0x42,%edx
xor    %eax,%eax
call   1050 <ksft_exit_fail_msg@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [17 x i8] c"%s:%d %s failed\0A\00"
@.str.2 = [198 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Broadcom/linux-rdma-nxt/refs/heads/hwpoison/.backup_do_not_remove/tools/testing/selftests/openat2/helpers.c\00"
@.str.3 = [9 x i8] c"asprintf\00"
@PATH_MAX = global i32 0
@.str.4 = [27 x i8] c"fdreadlink: malloc failed\0A\00"
@.str.5 = [9 x i8] c"readlink\00"
define i8* @fdreadlink(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i32 %0, i32* %2
br label %5
5: ; preds = %1
%6 = load i32, i32* %2
%7 = call i64 @asprintf(i8** %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([198 x i8], [198 x i8]* @.str.2, i64 0, i64 0), i32 59, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
br label %12
12: ; preds = %11
%13 = load i32, i32* @PATH_MAX
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = load i8*, i8** %3
%21 = load i32, i32* @PATH_MAX
%22 = call i32 @memset(i8* %20, i32 0, i32 %21)
br label %23
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = load i8*, i8** %3
%26 = load i32, i32* @PATH_MAX
%27 = call i64 @readlink(i8* %24, i8* %25, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([198 x i8], [198 x i8]* @.str.2, i64 0, i64 0), i32 66, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
br label %32
32: ; preds = %31
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [17 x i8] c"%s:%d %s failed\0A\00"
@.str.2 = [198 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Broadcom/linux-rdma-nxt/refs/heads/hwpoison/.backup_do_not_remove/tools/testing/selftests/openat2/helpers.c\00"
@.str.3 = [9 x i8] c"asprintf\00"
@PATH_MAX = global i32 0
@.str.4 = [27 x i8] c"fdreadlink: malloc failed\0A\00"
@.str.5 = [9 x i8] c"readlink\00"
define i8* @fdreadlink(i32 %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i64 @asprintf(i8** %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([198 x i8], [198 x i8]* @.str.2, i64 0, i64 0), i32 59, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* @PATH_MAX
%10 = call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%14 = load i32, i32* @PATH_MAX
br label %15
15: ; preds = %12, %8
%16 = phi i32 [ %14, %12 ], [ %9, %8 ]
%17 = call i32 @memset(i8* %10, i32 0, i32 %16)
%18 = load i8*, i8** %2
%19 = load i32, i32* @PATH_MAX
%20 = call i64 @readlink(i8* %18, i8* %10, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 (i8*, ...) @ksft_exit_fail_msg(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([198 x i8], [198 x i8]* @.str.2, i64 0, i64 0), i32 66, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
%25 = load i8*, i8** %2
%26 = call i32 @free(i8* %25)
ret i8* %10
}