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

/* Variables and functions */
scalar_t__ feof(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int strlen(char *);
int read_environment(void) {
  char buf[256];
  char *val;
  for (;;) {
    fgets(buf, sizeof(buf), stdin);
    if (feof(stdin))
      return -1;
    buf[strlen(buf) - 1] = '\0';
    if (!strlen(buf))
      return 0;
    val = strchr(buf, ':');
    if (!val) {
      fprintf(stderr, "Invalid environment: '%s'\n", buf);
      return -1;
    }
    *val = '\0';
    val++;
    val++;
    fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
    setenv(buf, val, 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    -0x110(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1060 <fgets@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     11a9 <read_environment+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <read_environment+0x143>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jne    11e3 <read_environment+0x83>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <read_environment+0x143>
lea    -0x110(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    1233 <read_environment+0xd3>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a3 <read_environment+0x143>
mov    -0x118(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x118(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x118(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
mov    -0x118(%rbp),%rcx
lea    0xd9c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
jmp    116b <read_environment+0xb>
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_environment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    0x2e70(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
mov    (%r12),%edi
call   1080 <feof@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11b5 <read_environment+0x55>
mov    %r14d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e1c(%rip),%r13
lea    0xe58(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     124f <read_environment+0xef>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1257 <read_environment+0xf7>
mov    %rax,%rbp
movb   $0x0,(%rax)
add    $0x2,%rbp
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
mov    (%r12),%edi
call   1080 <feof@plt>
test   %rax,%rax
je     11d0 <read_environment+0x70>
jmp    11a0 <read_environment+0x40>
xor    %r14d,%r14d
jmp    11a0 <read_environment+0x40>
mov    0x0(%r13),%edi
lea    0xd9e(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11a0 <read_environment+0x40>

LLVM IR (-O0):
@stdin = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Invalid environment: '%s'\0A\00"
@.str.1 = [27 x i8] c"Environment: '%s' is '%s'\0A\00"
define i32 @read_environment() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i8*
br label %4
4: ; preds = %31, %0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = load i32, i32* @stdin
%7 = call i32 @fgets(i8* %5, i32 256, i32 %6)
%8 = load i32, i32* @stdin
%9 = call i64 @feof(i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %4
store i32 -1, i32* %1
br label %44
12: ; preds = %4
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %16
store i8 0, i8* %17
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %12
store i32 0, i32* %1
br label %44
22: ; preds = %12
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%24 = call i8* @strchr(i8* %23, i8 signext 58)
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = icmp ne i8* %25, null
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %29)
store i32 -1, i32* %1
br label %44
31: ; preds = %22
%32 = load i8*, i8** %3
store i8 0, i8* %32
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %3
%35 = load i8*, i8** %3
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %3
%37 = load i32, i32* @stderr
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%39 = load i8*, i8** %3
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %39)
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%42 = load i8*, i8** %3
%43 = call i32 @setenv(i8* %41, i8* %42, i32 1)
br label %4
44: ; preds = %27, %21, %11
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@stdin = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Invalid environment: '%s'\0A\00"
@.str.1 = [27 x i8] c"Environment: '%s' is '%s'\0A\00"
define i32 @read_environment() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i32 @fgets(i8* %2, i32 256, i32 %3)
%5 = load i32, i32* @stdin
%6 = call i64 @feof(i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %31
8: ; preds = %0, %21
%9 = call i32 @strlen(i8* %2)
%10 = add nsw i32 %9, -1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i32 @strlen(i8* %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %31, label %15
15: ; preds = %8
%16 = call i8* @strchr(i8* %2, i8 signext 58)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %31
21: ; preds = %15
store i8 0, i8* %16
%22 = getelementptr inbounds i8, i8* %16, i64 2
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %22)
%25 = call i32 @setenv(i8* %2, i8* %22, i32 1)
%26 = load i32, i32* @stdin
%27 = call i32 @fgets(i8* %2, i32 256, i32 %26)
%28 = load i32, i32* @stdin
%29 = call i64 @feof(i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %8, label %31
31: ; preds = %21, %8, %0, %18
%32 = phi i32 [ -1, %18 ], [ -1, %0 ], [ -1, %21 ], [ 0, %8 ]
ret i32 %32
}