#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*/ close(int);
int /*<<< orphan*/ error(char *, char *);
int fork_command(char *);
char *getenv(char *);
int read(int, char *, int);
char *realloc(char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
char *get_environment() {
  char *ptr;
  char *command;
  int readfd;
  int bytes = 0;
  int alloc = 0;
  command = getenv("TS_ENV");
  if (command == 0)
    return 0;
  readfd = fork_command(command);
  ptr = 0;
  do {
    int next;
    int res;
    next = bytes + 1000;
    if (next > alloc) {
      ptr = realloc(ptr, next);
      alloc = next;
    }
    res = read(readfd, ptr + bytes, 1000);
    if (res < 0)
      error("Cannot read from the TS_ENV command (%s)", command);
    else if (res == 0)
      break;
    else
      bytes += res;
  } while (1);
  ptr[bytes] = '\0';
  close(readfd);
  wait(0);
  return ptr;
}


x86 assembly (clang, -O0):
<get_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe73(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <get_environment+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    127d <get_environment+0x10d>
mov    -0x18(%rbp),%rdi
call   1070 <fork_command@plt>
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    $0x3e8,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    11ef <get_environment+0x7f>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    $0x3e8,%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1229 <get_environment+0xb9>
mov    -0x18(%rbp),%rsi
lea    0xde8(%rip),%rdi
call   1080 <error@plt>
jmp    1246 <get_environment+0xd6>
cmpl   $0x0,-0x2c(%rbp)
jne    1238 <get_environment+0xc8>
jmp    125a <get_environment+0xea>
mov    -0x2c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1246 <get_environment+0xd6>
jmp    124b <get_environment+0xdb>
mov    $0x1,%al
test   $0x1,%al
jne    11c2 <get_environment+0x52>
jmp    125a <get_environment+0xea>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <wait@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_environment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
call   1030 <getenv@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     1223 <get_environment+0xb3>
mov    %rax,%r14
mov    %rax,%rdi
call   1070 <fork_command@plt>
mov    %eax,%r12d
xor    %r13d,%r13d
xor    %ebx,%ebx
jmp    11bb <get_environment+0x4b>
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%r15
add    %eax,%r15d
test   %eax,%eax
je     120b <get_environment+0x9b>
lea    0x3e8(%r15),%ebp
mov    %r15,(%rsp)
movslq %r15d,%r15
cmp    %r13d,%ebp
jle    11e0 <get_environment+0x70>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%rbx
mov    %ebp,%r13d
lea    (%rbx,%r15,1),%rsi
mov    %r12d,%edi
mov    $0x3e8,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11b0 <get_environment+0x40>
lea    0xe0b(%rip),%rdi
mov    %r14,%rsi
call   1080 <error@plt>
cmp    %r13d,%ebp
jg     11d0 <get_environment+0x60>
jmp    11e0 <get_environment+0x70>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r12d,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <wait@plt>
jmp    1225 <get_environment+0xb5>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"TS_ENV\00"
@.str.1 = [41 x i8] c"Cannot read from the TS_ENV command (%s)\00"
define i8* @get_environment() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %5
store i32 0, i32* %6
%9 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %0
store i8* null, i8** %1
br label %59
13: ; preds = %0
%14 = load i8*, i8** %3
%15 = call i32 @fork_command(i8* %14)
store i32 %15, i32* %4
store i8* null, i8** %2
br label %16
16: ; preds = %49, %13
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1000
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = load i32, i32* %6
%21 = icmp sgt i32 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8*, i8** %2
%24 = load i32, i32* %7
%25 = call i8* @realloc(i8* %23, i32 %24)
store i8* %25, i8** %2
%26 = load i32, i32* %7
store i32 %26, i32* %6
br label %27
27: ; preds = %22, %16
%28 = load i32, i32* %4
%29 = load i8*, i8** %2
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = call i32 @read(i32 %28, i8* %32, i32 1000)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = load i8*, i8** %3
%38 = call i32 @error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %37)
br label %48
39: ; preds = %27
%40 = load i32, i32* %8
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
br label %50
43: ; preds = %39
%44 = load i32, i32* %8
%45 = load i32, i32* %5
%46 = add nsw i32 %45, %44
store i32 %46, i32* %5
br label %47
47: ; preds = %43
br label %48
48: ; preds = %47, %36
br label %49
49: ; preds = %48
br i1 true, label %16, label %50
50: ; preds = %49, %42
%51 = load i8*, i8** %2
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %4
%56 = call i32 @close(i32 %55)
%57 = call i32 @wait(i32 0)
%58 = load i8*, i8** %2
store i8* %58, i8** %1
br label %59
59: ; preds = %50, %12
%60 = load i8*, i8** %1
ret i8* %60
}

LLVM IR (-O2):
@.str = [7 x i8] c"TS_ENV\00"
@.str.1 = [41 x i8] c"Cannot read from the TS_ENV command (%s)\00"
define i8* @get_environment() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %33, label %3
3: ; preds = %0
%4 = tail call i32 @fork_command(i8* %1)
br label %5
5: ; preds = %25, %3
%6 = phi i32 [ %27, %25 ], [ 0, %3 ]
%7 = phi i32 [ %18, %25 ], [ 0, %3 ]
%8 = phi i8* [ %19, %25 ], [ null, %3 ]
%9 = add nsw i32 %6, 1000
%10 = sext i32 %6 to i64
br label %11
11: ; preds = %5, %23
%12 = phi i32 [ %18, %23 ], [ %7, %5 ]
%13 = phi i8* [ %19, %23 ], [ %8, %5 ]
%14 = icmp sgt i32 %9, %12
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i8* @realloc(i8* %13, i32 %9)
br label %17
17: ; preds = %15, %11
%18 = phi i32 [ %9, %15 ], [ %12, %11 ]
%19 = phi i8* [ %16, %15 ], [ %13, %11 ]
%20 = getelementptr inbounds i8, i8* %19, i64 %10
%21 = tail call i32 @read(i32 %4, i8* %20, i32 1000)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 @error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %11
25: ; preds = %17
%26 = icmp eq i32 %21, 0
%27 = add nsw i32 %21, %6
br i1 %26, label %28, label %5
28: ; preds = %25
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %19, i64 %29
store i8 0, i8* %30
%31 = tail call i32 @close(i32 %4)
%32 = tail call i32 @wait(i32 0)
br label %33
33: ; preds = %0, %28
%34 = phi i8* [ %19, %28 ], [ null, %0 ]
ret i8* %34
}