#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free_env(char **);
char **malloc(int);
scalar_t__ read_n(int, int *, int);
scalar_t__ read_string(int, char **);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strncmp(char *, char *, int);
int read_env(int fd, char ***envp) {
  int envc = 0;
  int i;
  if (read_n(fd, &envc, sizeof(int)) < 0) {
    fprintf(stderr, "systemsafe: read_env: %s\n", strerror(errno));
    return (-1);
  }
  if (!(*envp = malloc((envc + 1) * sizeof(**envp)))) {
    fprintf(stderr, "systemsafe: read_env: malloc: %s\n", strerror(errno));
    return (-1);
  }
  for (i = 0; i < envc; i++) {
    char *entry;
    if (read_string(fd, &entry) < 0) {
      fprintf(stderr, "systemsafe: %s\n", strerror(errno));
      free_env(*envp);
      return (-1);
    }
    if (strncmp("LD_PRELOAD=", entry, 10) == 0)
      entry[11] = '\0';
    (*envp)[i] = entry;
  }
  (*envp)[envc] = NULL;
  return (0);
}


x86 assembly (clang, -O0):
<read_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read_n@plt>
cmp    $0x0,%rax
jge    11d9 <read_env+0x69>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe38(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_env+0x194>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1235 <read_env+0xc5>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xdf6(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_env+0x194>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12ea <read_env+0x17a>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <read_string@plt>
cmp    $0x0,%rax
jge    12a2 <read_env+0x132>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdb7(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <free_env@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_env+0x194>
mov    -0x20(%rbp),%rsi
lea    0xd9f(%rip),%rdi
mov    $0xa,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12c9 <read_env+0x159>
mov    -0x20(%rbp),%rax
movb   $0x0,0xb(%rax)
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x18(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    123c <read_env+0xcc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_env>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %edi,%r13d
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1040 <read_n@plt>
test   %rax,%rax
js     122c <read_env+0xbc>
mov    0xc(%rsp),%ebp
lea    0x8(,%rbp,8),%edi
call   1060 <malloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     124c <read_env+0xdc>
test   %ebp,%ebp
jle    12b7 <read_env+0x147>
xor    %ebx,%ebx
lea    0x10(%rsp),%r14
lea    0xe75(%rip),%r15
jmp    11fa <read_env+0x8a>
nopl   0x0(%rax)
mov    (%r12),%rax
mov    %rcx,(%rax,%rbx,8)
add    $0x1,%rbx
movslq 0xc(%rsp),%rbp
cmp    %rbp,%rbx
jge    12b3 <read_env+0x143>
mov    %r13d,%edi
mov    %r14,%rsi
call   1090 <read_string@plt>
test   %rax,%rax
js     127b <read_env+0x10b>
mov    0x10(%rsp),%rsi
mov    %r15,%rdi
mov    $0xa,%edx
call   1030 <strncmp@plt>
mov    0x10(%rsp),%rcx
test   %rax,%rax
jne    11e0 <read_env+0x70>
movb   $0x0,0xb(%rcx)
jmp    11e0 <read_env+0x70>
mov    0x2dad(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdb6(%rip),%rsi
jmp    126a <read_env+0xfa>
mov    0x2d8d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdb0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12c4 <read_env+0x154>
mov    0x2d5e(%rip),%rax
mov    (%rax),%ebp
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xda3(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    (%r12),%rdi
call   1070 <free_env@plt>
mov    $0xffffffff,%eax
jmp    12c4 <read_env+0x154>
mov    (%r12),%rax
movslq %ebp,%rcx
movq   $0x0,(%rax,%rcx,8)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"systemsafe: read_env: %s\0A\00"
@errno = global i32 0
@.str.1 = [34 x i8] c"systemsafe: read_env: malloc: %s\0A\00"
@.str.2 = [16 x i8] c"systemsafe: %s\0A\00"
@.str.3 = [12 x i8] c"LD_PRELOAD=\00"
define i32 @read_env(i32 %0, i8*** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8***
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %4
store i8*** %1, i8**** %5
store i32 0, i32* %6
%9 = load i32, i32* %4
%10 = call i64 @read_n(i32 %9, i32* %6, i32 4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %71
17: ; preds = %2
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 8
%22 = trunc i64 %21 to i32
%23 = call i8** @malloc(i32 %22)
%24 = load i8***, i8**** %5
store i8** %23, i8*** %24
%25 = icmp ne i8** %23, null
br i1 %25, label %31, label %26
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 -1, i32* %3
br label %71
31: ; preds = %17
store i32 0, i32* %7
br label %32
32: ; preds = %62, %31
%33 = load i32, i32* %7
%34 = load i32, i32* %6
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %65
36: ; preds = %32
%37 = load i32, i32* %4
%38 = call i64 @read_string(i32 %37, i8** %8)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = load i8***, i8**** %5
%46 = load i8**, i8*** %45
%47 = call i32 @free_env(i8** %46)
store i32 -1, i32* %3
br label %71
48: ; preds = %36
%49 = load i8*, i8** %8
%50 = call i64 @strncmp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %49, i32 10)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i64 11
store i8 0, i8* %54
br label %55
55: ; preds = %52, %48
%56 = load i8*, i8** %8
%57 = load i8***, i8**** %5
%58 = load i8**, i8*** %57
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
store i8* %56, i8** %61
br label %62
62: ; preds = %55
%63 = load i32, i32* %7
%64 = add nsw i32 %63, 1
store i32 %64, i32* %7
br label %32
65: ; preds = %32
%66 = load i8***, i8**** %5
%67 = load i8**, i8*** %66
%68 = load i32, i32* %6
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8*, i8** %67, i64 %69
store i8* null, i8** %70
store i32 0, i32* %3
br label %71
71: ; preds = %65, %40, %26, %12
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"systemsafe: read_env: %s\0A\00"
@errno = global i32 0
@.str.1 = [34 x i8] c"systemsafe: read_env: malloc: %s\0A\00"
@.str.2 = [16 x i8] c"systemsafe: %s\0A\00"
@.str.3 = [12 x i8] c"LD_PRELOAD=\00"
define i32 @read_env(i32 %0, i8*** nocapture %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = call i64 @read_n(i32 %0, i32* %3, i32 4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %60
13: ; preds = %2
%14 = load i32, i32* %3
%15 = shl i32 %14, 3
%16 = add i32 %15, 8
%17 = call i8** @malloc(i32 %16)
store i8** %17, i8*** %1
%18 = icmp eq i8** %17, null
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = icmp sgt i32 %14, 0
br i1 %20, label %21, label %55
21: ; preds = %19
%22 = bitcast i8** %4 to i8*
br label %28
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %60
28: ; preds = %21, %46
%29 = phi i64 [ 0, %21 ], [ %49, %46 ]
%30 = call i64 @read_string(i32 %0, i8** %4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i8**, i8*** %1
%38 = call i32 @free_env(i8** %37)
br label %60
39: ; preds = %28
%40 = load i8*, i8** %4
%41 = call i64 @strncmp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %40, i32 10)
%42 = icmp eq i64 %41, 0
%43 = load i8*, i8** %4
br i1 %42, label %44, label %46
44: ; preds = %39
%45 = getelementptr inbounds i8, i8* %43, i64 11
store i8 0, i8* %45
br label %46
46: ; preds = %39, %44
%47 = load i8**, i8*** %1
%48 = getelementptr inbounds i8*, i8** %47, i64 %29
store i8* %43, i8** %48
%49 = add nuw nsw i64 %29, 1
%50 = load i32, i32* %3
%51 = sext i32 %50 to i64
%52 = icmp slt i64 %49, %51
br i1 %52, label %28, label %53
53: ; preds = %46
%54 = load i8**, i8*** %1
br label %55
55: ; preds = %19, %53
%56 = phi i32 [ %50, %53 ], [ %14, %19 ]
%57 = phi i8** [ %54, %53 ], [ %17, %19 ]
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8*, i8** %57, i64 %58
store i8* null, i8** %59
br label %60
60: ; preds = %32, %55, %23, %8
%61 = phi i32 [ -1, %8 ], [ 0, %55 ], [ -1, %23 ], [ -1, %32 ]
ret i32 %61
}