#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 getpid();
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, ...);
int /*<<< orphan*/ system(char *);
char *who(int /*<<< orphan*/);
void ls_proc_fd(int fd) {
  char cmd[128];
  if (fd == -1)
    snprintf(cmd, sizeof cmd, "ls -l /proc/%d/fd", getpid());
  else
    snprintf(cmd, sizeof cmd, "ls -l /proc/%d/fd/%d", getpid(), fd);
  printf("%s %s\n", who(0), cmd);
  system(cmd);
}


x86 assembly (clang, -O0):
<ls_proc_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    119e <ls_proc_fd+0x4e>
lea    -0x90(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,%ecx
mov    $0x80,%esi
lea    0xe6e(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    11d3 <ls_proc_fd+0x83>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0xa0(%rbp),%rdi
mov    %eax,%ecx
mov    -0x4(%rbp),%r8d
mov    $0x80,%esi
lea    0xe46(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
xor    %edi,%edi
call   1070 <who@plt>
mov    %rax,%rsi
lea    -0x90(%rbp),%rdx
lea    0xe3c(%rip),%rdi
call   1050 <printf@plt>
lea    -0x90(%rbp),%rdi
call   1040 <system@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_proc_fd>:
push   %rbx
sub    $0x80,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    $0xffffffff,%ebx
je     1183 <ls_proc_fd+0x33>
lea    0xea5(%rip),%rdx
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %eax,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    119b <ls_proc_fd+0x4b>
lea    0xe76(%rip),%rdx
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
xor    %edi,%edi
call   1070 <who@plt>
lea    0xe7e(%rip),%rdi
mov    %rsp,%rbx
mov    %rax,%rsi
mov    %rbx,%rdx
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
add    $0x80,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"ls -l /proc/%d/fd\00"
@.str.1 = [21 x i8] c"ls -l /proc/%d/fd/%d\00"
@.str.2 = [7 x i8] c"%s %s\0A\00"
define void @ls_proc_fd(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = call i32 (...) @getpid()
%9 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %15
10: ; preds = %1
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%12 = call i32 (...) @getpid()
%13 = load i32, i32* %2
%14 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %11, i32 128, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %13)
br label %15
15: ; preds = %10, %6
%16 = call i8* @who(i32 0)
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %16, i8* %17)
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%20 = call i32 @system(i8* %19)
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"ls -l /proc/%d/fd\00"
@.str.1 = [21 x i8] c"ls -l /proc/%d/fd/%d\00"
@.str.2 = [7 x i8] c"%s %s\0A\00"
define void @ls_proc_fd(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = icmp eq i32 %0, -1
%5 = tail call i32 (...) @getpid()
br i1 %4, label %6, label %8
6: ; preds = %1
%7 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 128, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %10
8: ; preds = %1
%9 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %3, i32 128, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %0)
br label %10
10: ; preds = %8, %6
%11 = call i8* @who(i32 0)
%12 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %11, i8* %3)
%13 = call i32 @system(i8* %3)
ret void
}