#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*/ line;

/* Variables and functions */
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
char **realloc(char **, size_t);
int /*<<< orphan*/ stdin;
char *strdup(char *);
size_t strlen(char *);
char **readenv(void) {
  char **env = NULL;
  char line[1024];
  size_t len, n;
  n = 0;
  while (1) {
    if (fgets(line, sizeof(line), stdin) == NULL)
      break;
    len = strlen(line);
    if (len <= 1) {
      break;
    }
    if (++n > 32)
      errx(1, "can't allocate that much variables");
    env = realloc(env, n * sizeof(char *));
    if (env == NULL)
      err(1, "realloc");
    if (len > 0 && line[len - 1] == '\n')
      line[len - 1] = '\x00';
    env[n - 1] = strdup(line);
    if (env[n - 1] == NULL)
      err(1, "strdup");
  }
  if (env == NULL)
    errx(1, "no variable set\n");
  return env;
}


x86 assembly (clang, -O0):
<readenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11a7 <readenv+0x47>
jmp    12bd <readenv+0x15d>
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x1,-0x418(%rbp)
01
ja     11cd <readenv+0x6d>
jmp    12bd <readenv+0x15d>
mov    -0x420(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x420(%rbp)
cmp    $0x20,%rax
jbe    11fa <readenv+0x9a>
mov    $0x1,%edi
lea    0xe0b(%rip),%rsi
call   1040 <errx@plt>
mov    -0x8(%rbp),%rdi
mov    -0x420(%rbp),%rsi
shl    $0x3,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    122e <readenv+0xce>
mov    $0x1,%edi
lea    0xdfa(%rip),%rsi
call   1060 <err@plt>
cmpq   $0x0,-0x418(%rbp)
jbe    126b <readenv+0x10b>
mov    -0x418(%rbp),%rax
sub    $0x1,%rax
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    126b <readenv+0x10b>
mov    -0x418(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
mov    -0x420(%rbp),%rcx
sub    $0x1,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
mov    -0x420(%rbp),%rcx
sub    $0x1,%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    12b8 <readenv+0x158>
mov    $0x1,%edi
lea    0xd78(%rip),%rsi
call   1060 <err@plt>
jmp    117e <readenv+0x1e>
cmpq   $0x0,-0x8(%rbp)
jne    12d9 <readenv+0x179>
mov    $0x1,%edi
lea    0xd5e(%rip),%rsi
call   1040 <errx@plt>
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readenv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    0x2e70(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     126c <readenv+0x10c>
xor    %r13d,%r13d
xor    %ebx,%ebx
jmp    11ce <readenv+0x6e>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1080 <strdup@plt>
mov    %rax,(%rbx,%r13,8)
test   %rax,%rax
je     1220 <readenv+0xc0>
mov    (%r12),%edx
mov    %r15,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    %r14,%r13
test   %rax,%rax
je     127f <readenv+0x11f>
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    $0x2,%rax
jb     1267 <readenv+0x107>
mov    %rax,%rbp
lea    0x1(%r13),%r14
cmp    $0x21,%r14
jb     11fe <readenv+0x9e>
mov    $0x1,%edi
lea    0xe07(%rip),%rsi
call   1040 <errx@plt>
lea    0x0(,%r14,8),%rsi
mov    %rbx,%rdi
call   1070 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1240 <readenv+0xe0>
cmpb   $0xa,-0x1(%rbp,%r15,1)
jne    11a0 <readenv+0x40>
jmp    125d <readenv+0xfd>
mov    $0x1,%edi
lea    0xdff(%rip),%rsi
call   1060 <err@plt>
jmp    11b1 <readenv+0x51>
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
lea    0xdd7(%rip),%rsi
call   1060 <err@plt>
cmpb   $0xa,-0x1(%rbp,%r15,1)
jne    11a0 <readenv+0x40>
movb   $0x0,-0x1(%rsp,%rbp,1)
jmp    11a0 <readenv+0x40>
test   %rbx,%rbx
jne    127f <readenv+0x11f>
lea    0xdbf(%rip),%rsi
mov    $0x1,%edi
call   1040 <errx@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [35 x i8] c"can't allocate that much variables\00"
@.str.1 = [8 x i8] c"realloc\00"
@.str.2 = [7 x i8] c"strdup\00"
@.str.3 = [17 x i8] c"no variable set\0A\00"
define i8** @readenv() {
%1 = alloca i8**
%2 = alloca [1024 x i8]
%3 = alloca i64
%4 = alloca i64
store i8** null, i8*** %1
store i64 0, i64* %4
br label %5
5: ; preds = %0, %61
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = load i32, i32* @stdin
%8 = call i32* @fgets(i8* %6, i32 1024, i32 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %5
br label %62
11: ; preds = %5
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = icmp ule i64 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %11
br label %62
17: ; preds = %11
%18 = load i64, i64* %4
%19 = add i64 %18, 1
store i64 %19, i64* %4
%20 = icmp ugt i64 %19, 32
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @errx(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i8**, i8*** %1
%25 = load i64, i64* %4
%26 = mul i64 %25, 8
%27 = call i8** @realloc(i8** %24, i64 %26)
store i8** %27, i8*** %1
%28 = load i8**, i8*** %1
%29 = icmp eq i8** %28, null
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %23
%33 = load i64, i64* %3
%34 = icmp ugt i64 %33, 0
br i1 %34, label %35, label %46
35: ; preds = %32
%36 = load i64, i64* %3
%37 = sub i64 %36, 1
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %46
42: ; preds = %35
%43 = load i64, i64* %3
%44 = sub i64 %43, 1
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %42, %35, %32
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%48 = call i8* @strdup(i8* %47)
%49 = load i8**, i8*** %1
%50 = load i64, i64* %4
%51 = sub i64 %50, 1
%52 = getelementptr inbounds i8*, i8** %49, i64 %51
store i8* %48, i8** %52
%53 = load i8**, i8*** %1
%54 = load i64, i64* %4
%55 = sub i64 %54, 1
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %61
59: ; preds = %46
%60 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %61
61: ; preds = %59, %46
br label %5
62: ; preds = %16, %10
%63 = load i8**, i8*** %1
%64 = icmp eq i8** %63, null
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = call i32 @errx(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %67
67: ; preds = %65, %62
%68 = load i8**, i8*** %1
ret i8** %68
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [35 x i8] c"can't allocate that much variables\00"
@.str.1 = [8 x i8] c"realloc\00"
@.str.2 = [7 x i8] c"strdup\00"
@.str.3 = [17 x i8] c"no variable set\0A\00"
define i8** @readenv() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i32* @fgets(i8* %2, i32 1024, i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %40, label %6
6: ; preds = %0, %34
%7 = phi i64 [ %12, %34 ], [ 0, %0 ]
%8 = phi i8** [ %18, %34 ], [ null, %0 ]
%9 = call i64 @strlen(i8* %2)
%10 = icmp ult i64 %9, 2
br i1 %10, label %38, label %11
11: ; preds = %6
%12 = add i64 %7, 1
%13 = icmp ugt i64 %12, 32
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @errx(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = shl i64 %12, 3
%18 = call i8** @realloc(i8** %8, i64 %17)
%19 = icmp eq i8** %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %16, %20
%23 = add i64 %9, -1
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 10
br i1 %26, label %27, label %28
27: ; preds = %22
store i8 0, i8* %24
br label %28
28: ; preds = %27, %22
%29 = call i8* @strdup(i8* %2)
%30 = getelementptr inbounds i8*, i8** %18, i64 %7
store i8* %29, i8** %30
%31 = icmp eq i8* %29, null
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = load i32, i32* @stdin
%36 = call i32* @fgets(i8* %2, i32 1024, i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %42, label %6
38: ; preds = %6
%39 = icmp eq i8** %8, null
br i1 %39, label %40, label %42
40: ; preds = %0, %38
%41 = call i32 @errx(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %34, %40, %38
%43 = phi i8** [ null, %40 ], [ %8, %38 ], [ %18, %34 ]
ret i8** %43
}