#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*/ proc_path;
typedef int /*<<< orphan*/ pcap_path;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ warn(char *, char *);
int pcap_fd_check(int fd) {
  int rv = 0;
  char pcap_path[PATH_MAX];
  char proc_path[PATH_MAX];
  snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", fd);
  rv = readlink(proc_path, pcap_path, sizeof(pcap_path));
  if (rv < 0) {
    warn("readlink failed on %s", proc_path);
    return 1;
  }
  fprintf(stderr, "pcap fd %d path %s\n", fd, pcap_path);
  return 0;
}


x86 assembly (clang, -O0):
<pcap_fd_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e97(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0x38(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x40(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%ecx
lea    0xe54(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
call   1040 <readlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11f4 <pcap_fd_check+0xb4>
mov    -0x48(%rbp),%rsi
lea    0xe35(%rip),%rdi
call   1030 <warn@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    121e <pcap_fd_check+0xde>
mov    -0x40(%rbp),%rcx
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe1c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pcap_fd_check>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    0x2e9b(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe84(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1040 <readlink@plt>
test   %eax,%eax
js     11bb <pcap_fd_check+0x7b>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    %r14d,%edx
mov    %r12,%rcx
call   1060 <fprintf@plt>
xor    %eax,%eax
jmp    11cf <pcap_fd_check+0x8f>
lea    0xe4f(%rip),%rdi
mov    %rbx,%rsi
call   1030 <warn@plt>
mov    $0x1,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [22 x i8] c"readlink failed on %s\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"pcap fd %d path %s\0A\00"
define i32 @pcap_fd_check(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = alloca i8, i64 %14
store i64 %14, i64* %7
%16 = trunc i64 %14 to i32
%17 = load i32, i32* %3
%18 = call i32 @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = trunc i64 %10 to i32
%20 = call i32 @readlink(i8* %15, i8* %12, i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 @warn(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 1, i32* %2
store i32 1, i32* %8
br label %29
25: ; preds = %1
%26 = load i32, i32* @stderr
%27 = load i32, i32* %3
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8* %12)
store i32 0, i32* %2
store i32 1, i32* %8
br label %29
29: ; preds = %25, %23
%30 = load i8*, i8** %5
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [22 x i8] c"readlink failed on %s\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"pcap fd %d path %s\0A\00"
define i32 @pcap_fd_check(i32 %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = call i32 @snprintf(i8* %5, i32 %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32 @readlink(i8* %5, i8* %4, i32 %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @warn(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %0, i8* %4)
br label %14
14: ; preds = %11, %9
%15 = phi i32 [ 1, %9 ], [ 0, %11 ]
ret i32 %15
}