#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ environment;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strlen(char *);
void print_process_environment(pid_t pid) {
  int fd;
  char filename[24];
  char environment[1024 * 8];
  size_t length;
  char *next_var;
  snprintf(filename, sizeof(filename), "/proc/%d/environ", (int)pid);
  fd = open(filename, O_RDONLY);
  length = read(fd, environment, sizeof(environment));
  close(fd);
  environment[length] = '\0';
  next_var = environment;
  while (next_var < environment + length) {
    printf("%s\n", next_var);
    next_var += strlen(next_var) + 1;
  }
}


x86 assembly (clang, -O0):
<print_process_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %eax,%ecx
mov    $0x18,%esi
lea    0xe7b(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0x2000,%edx
call   1070 <read@plt>
mov    %rax,-0x2038(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x2038(%rbp),%rax
movb   $0x0,-0x2030(%rbp,%rax,1)
lea    -0x2030(%rbp),%rax
mov    %rax,-0x2040(%rbp)
mov    -0x2040(%rbp),%rax
lea    -0x2030(%rbp),%rcx
add    -0x2038(%rbp),%rcx
cmp    %rcx,%rax
jae    1233 <print_process_environment+0xd3>
mov    -0x2040(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2040(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
add    -0x2040(%rbp),%rax
mov    %rax,-0x2040(%rbp)
jmp    11df <print_process_environment+0x7f>
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_process_environment>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2020,%rsp
mov    %rdi,%rcx
lea    0xe8b(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x18,%esi
call   1050 <snprintf@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0x20(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1070 <read@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1060 <close@plt>
movb   $0x0,0x20(%rsp,%r14,1)
test   %r14,%r14
jle    11ef <print_process_environment+0x8f>
lea    (%rsp,%r14,1),%rbp
add    $0x20,%rbp
lea    0xe41(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
add    $0x1,%rbx
cmp    %rbp,%rbx
jb     11d0 <print_process_environment+0x70>
add    $0x2020,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/environ\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define void @print_process_environment(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca [24 x i8]
%5 = alloca [8192 x i8]
%6 = alloca i64
%7 = alloca i8*
store i64 %0, i64* %2
%8 = getelementptr inbounds [24 x i8], [24 x i8]* %4, i64 0, i64 0
%9 = load i64, i64* %2
%10 = trunc i64 %9 to i32
%11 = call i32 @snprintf(i8* %8, i32 24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [24 x i8], [24 x i8]* %4, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%17 = call i64 @read(i32 %15, i8* %16, i32 8192)
store i64 %17, i64* %6
%18 = load i32, i32* %3
%19 = call i32 @close(i32 %18)
%20 = load i64, i64* %6
%21 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 %20
store i8 0, i8* %21
%22 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
store i8* %22, i8** %7
br label %23
23: ; preds = %29, %1
%24 = load i8*, i8** %7
%25 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%26 = load i64, i64* %6
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = icmp ult i8* %24, %27
br i1 %28, label %29, label %37
29: ; preds = %23
%30 = load i8*, i8** %7
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %7
%33 = call i64 @strlen(i8* %32)
%34 = add nsw i64 %33, 1
%35 = load i8*, i8** %7
%36 = getelementptr inbounds i8, i8* %35, i64 %34
store i8* %36, i8** %7
br label %23
37: ; preds = %23
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/environ\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define void @print_process_environment(i64 %0) {
%2 = alloca [24 x i8]
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [24 x i8], [24 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%6 = trunc i64 %0 to i32
%7 = call i32 @snprintf(i8* %4, i32 24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %4, i32 %8)
%10 = call i64 @read(i32 %9, i8* %5, i32 8192)
%11 = call i32 @close(i32 %9)
%12 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 %10
store i8 0, i8* %12
%13 = icmp sgt i64 %10, 0
br i1 %13, label %14, label %21
14: ; preds = %1, %14
%15 = phi i8* [ %19, %14 ], [ %5, %1 ]
%16 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = call i64 @strlen(i8* dereferenceable(1) %15)
%18 = add nsw i64 %17, 1
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = icmp ult i8* %19, %12
br i1 %20, label %14, label %21
21: ; preds = %14, %1
ret void
}