#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 */
scalar_t__ cmalloc(int);
char *scopy(char *);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
void fix_environment() {
  int i, j, k;
  char **com_env;
  extern char **environ;
  static char *elist[] = {"TERM=", "DISPLAY=", "WINDOWID=", NULL};
  char buf[500];
  for (i = 0; environ[i] != NULL; i++)
    ;
  com_env = (char **)cmalloc((i + 4) * sizeof(char *));
  for (i = j = 0; environ[i] != NULL; i++) {
    for (k = 0; elist[k] != NULL; k++)
      if (strncmp(elist[k], environ[i], strlen(elist[k])) == 0)
        break;
    if (elist[k] == NULL)
      com_env[j++] = environ[i];
  }
  com_env[j++] = scopy("TERM=xterm-r6");
  sprintf(buf, "DISPLAY=0");
  com_env[j++] = scopy(buf);
  sprintf(buf, "WINDOWID=0");
  com_env[j++] = scopy(buf);
  com_env[j] = NULL;
  environ = com_env;
}


x86 assembly (clang, -O0):
<fix_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%rax
movslq -0x4(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     118e <fix_environment+0x3e>
jmp    1180 <fix_environment+0x30>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1162 <fix_environment+0x12>
mov    -0x4(%rbp),%eax
add    $0x4,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <cmalloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x4(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
movslq -0x4(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     12b4 <fix_environment+0x164>
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rcx
lea    0x2e72(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
je     1263 <fix_environment+0x113>
movslq -0xc(%rbp),%rcx
lea    0x2e5c(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x220(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x218(%rbp)
movslq -0xc(%rbp),%rcx
lea    0x2e2d(%rip),%rax
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    -0x220(%rbp),%rdi
mov    -0x218(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1250 <fix_environment+0x100>
jmp    1263 <fix_environment+0x113>
jmp    1255 <fix_environment+0x105>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11d3 <fix_environment+0x83>
movslq -0xc(%rbp),%rcx
lea    0x2de2(%rip),%rax
cmpq   $0x0,(%rax,%rcx,8)
jne    12a1 <fix_environment+0x151>
mov    0x2d68(%rip),%rax
mov    (%rax),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    12a6 <fix_environment+0x156>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11b3 <fix_environment+0x63>
lea    0xd5e(%rip),%rdi
call   1050 <scopy@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
lea    -0x210(%rbp),%rdi
lea    0xd40(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x210(%rbp),%rdi
call   1050 <scopy@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
lea    -0x210(%rbp),%rdi
lea    0xd12(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x210(%rbp),%rdi
call   1050 <scopy@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x8(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rcx
mov    0x2c84(%rip),%rax
mov    %rcx,(%rax)
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fix_environment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    0x2e90(%rip),%r15
mov    (%r15),%rax
xor    %edi,%edi
nopl   (%rax)
cmpq   $0x0,(%rax,%rdi,1)
lea    0x8(%rdi),%rdi
jne    1160 <fix_environment+0x20>
add    $0x18,%edi
call   1060 <cmalloc@plt>
mov    %rax,0x8(%rsp)
mov    (%r15),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
je     1246 <fix_environment+0x106>
xor    %r12d,%r12d
lea    0xe6f(%rip),%r13
xor    %r14d,%r14d
jmp    11b8 <fix_environment+0x78>
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%r15
mov    (%r15),%rax
mov    0x8(%rax,%r12,1),%rbx
add    $0x8,%r12
test   %rbx,%rbx
je     1249 <fix_environment+0x109>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11a3 <fix_environment+0x63>
mov    (%r15),%rax
mov    (%rax,%r12,1),%rbx
lea    0xe26(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11a3 <fix_environment+0x63>
mov    (%r15),%rax
mov    (%rax,%r12,1),%rbx
mov    %r15,%rbp
lea    0xe04(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11a0 <fix_environment+0x60>
mov    0x0(%rbp),%rax
mov    (%rax,%r12,1),%rax
movslq %r14d,%rcx
add    $0x1,%r14d
mov    0x8(%rsp),%rdx
mov    %rax,(%rdx,%rcx,8)
jmp    11a0 <fix_environment+0x60>
xor    %r14d,%r14d
lea    0xdc9(%rip),%rdi
call   1050 <scopy@plt>
mov    %r15,%rbp
movslq %r14d,%r15
mov    0x8(%rsp),%rbx
mov    %rax,(%rbx,%r15,8)
movabs $0x3d59414c50534944,%rax
41 59 3d
mov    %rax,0x10(%rsp)
movw   $0x30,0x18(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <scopy@plt>
mov    %rax,0x8(%rbx,%r15,8)
movabs $0x4449574f444e4957,%rax
57 49 44
mov    %rax,0x10(%rsp)
movl   $0x303d44,0x17(%rsp)
mov    %r14,%rdi
call   1050 <scopy@plt>
mov    %rax,0x10(%rbx,%r15,8)
movq   $0x0,0x18(%rbx,%r15,8)
mov    %rbx,0x0(%rbp)
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@fix_environment.elist = internal global [4 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str = [6 x i8] c"TERM=\00"
@.str.1 = [9 x i8] c"DISPLAY=\00"
@.str.2 = [10 x i8] c"WINDOWID=\00"
@environ = external global i8**
@.str.3 = [14 x i8] c"TERM=xterm-r6\00"
@.str.4 = [10 x i8] c"DISPLAY=0\00"
@.str.5 = [11 x i8] c"WINDOWID=0\00"
define void @fix_environment() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [500 x i8]
store i32 0, i32* %1
br label %6
6: ; preds = %14, %0
%7 = load i8**, i8*** @environ
%8 = load i32, i32* %1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8*, i8** %7, i64 %9
%11 = load i8*, i8** %10
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %6
br label %14
14: ; preds = %13
%15 = load i32, i32* %1
%16 = add nsw i32 %15, 1
store i32 %16, i32* %1
br label %6
17: ; preds = %6
%18 = load i32, i32* %1
%19 = add nsw i32 %18, 4
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 8
%22 = trunc i64 %21 to i32
%23 = call i64 @cmalloc(i32 %22)
%24 = inttoptr i64 %23 to i8**
store i8** %24, i8*** %4
store i32 0, i32* %2
store i32 0, i32* %1
br label %25
25: ; preds = %79, %17
%26 = load i8**, i8*** @environ
%27 = load i32, i32* %1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
%30 = load i8*, i8** %29
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %82
32: ; preds = %25
store i32 0, i32* %3
br label %33
33: ; preds = %58, %32
%34 = load i32, i32* %3
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [4 x i8*], [4 x i8*]* @fix_environment.elist, i64 0, i64 %35
%37 = load i8*, i8** %36
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %61
39: ; preds = %33
%40 = load i32, i32* %3
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [4 x i8*], [4 x i8*]* @fix_environment.elist, i64 0, i64 %41
%43 = load i8*, i8** %42
%44 = load i8**, i8*** @environ
%45 = load i32, i32* %1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
%48 = load i8*, i8** %47
%49 = load i32, i32* %3
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [4 x i8*], [4 x i8*]* @fix_environment.elist, i64 0, i64 %50
%52 = load i8*, i8** %51
%53 = call i32 @strlen(i8* %52)
%54 = call i64 @strncmp(i8* %43, i8* %48, i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %39
br label %61
57: ; preds = %39
br label %58
58: ; preds = %57
%59 = load i32, i32* %3
%60 = add nsw i32 %59, 1
store i32 %60, i32* %3
br label %33
61: ; preds = %56, %33
%62 = load i32, i32* %3
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [4 x i8*], [4 x i8*]* @fix_environment.elist, i64 0, i64 %63
%65 = load i8*, i8** %64
%66 = icmp eq i8* %65, null
br i1 %66, label %67, label %78
67: ; preds = %61
%68 = load i8**, i8*** @environ
%69 = load i32, i32* %1
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8*, i8** %68, i64 %70
%72 = load i8*, i8** %71
%73 = load i8**, i8*** %4
%74 = load i32, i32* %2
%75 = add nsw i32 %74, 1
store i32 %75, i32* %2
%76 = sext i32 %74 to i64
%77 = getelementptr inbounds i8*, i8** %73, i64 %76
store i8* %72, i8** %77
br label %78
78: ; preds = %67, %61
br label %79
79: ; preds = %78
%80 = load i32, i32* %1
%81 = add nsw i32 %80, 1
store i32 %81, i32* %1
br label %25
82: ; preds = %25
%83 = call i8* @scopy(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%84 = load i8**, i8*** %4
%85 = load i32, i32* %2
%86 = add nsw i32 %85, 1
store i32 %86, i32* %2
%87 = sext i32 %85 to i64
%88 = getelementptr inbounds i8*, i8** %84, i64 %87
store i8* %83, i8** %88
%89 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%90 = call i32 @sprintf(i8* %89, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%91 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%92 = call i8* @scopy(i8* %91)
%93 = load i8**, i8*** %4
%94 = load i32, i32* %2
%95 = add nsw i32 %94, 1
store i32 %95, i32* %2
%96 = sext i32 %94 to i64
%97 = getelementptr inbounds i8*, i8** %93, i64 %96
store i8* %92, i8** %97
%98 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%99 = call i32 @sprintf(i8* %98, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%100 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%101 = call i8* @scopy(i8* %100)
%102 = load i8**, i8*** %4
%103 = load i32, i32* %2
%104 = add nsw i32 %103, 1
store i32 %104, i32* %2
%105 = sext i32 %103 to i64
%106 = getelementptr inbounds i8*, i8** %102, i64 %105
store i8* %101, i8** %106
%107 = load i8**, i8*** %4
%108 = load i32, i32* %2
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds i8*, i8** %107, i64 %109
store i8* null, i8** %110
%111 = load i8**, i8*** %4
store i8** %111, i8*** @environ
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"TERM=\00"
@.str.1 = [9 x i8] c"DISPLAY=\00"
@.str.2 = [10 x i8] c"WINDOWID=\00"
@environ = external global i8**
@.str.3 = [14 x i8] c"TERM=xterm-r6\00"
@.str.4 = [10 x i8] c"DISPLAY=0\00"
@.str.5 = [11 x i8] c"WINDOWID=0\00"
define void @fix_environment() {
%1 = alloca [500 x i8]
%2 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%3 = load i8**, i8*** @environ
br label %4
4: ; preds = %4, %0
%5 = phi i64 [ %9, %4 ], [ 0, %0 ]
%6 = getelementptr inbounds i8*, i8** %3, i64 %5
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
%9 = add nuw i64 %5, 1
br i1 %8, label %10, label %4
10: ; preds = %4
%11 = trunc i64 %5 to i32
%12 = shl i32 %11, 3
%13 = add i32 %12, 32
%14 = tail call i64 @cmalloc(i32 %13)
%15 = inttoptr i64 %14 to i8**
%16 = load i8**, i8*** @environ
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %54, label %19
19: ; preds = %10, %47
%20 = phi i8* [ %52, %47 ], [ %17, %10 ]
%21 = phi i64 [ %49, %47 ], [ 0, %10 ]
%22 = phi i32 [ %48, %47 ], [ 0, %10 ]
%23 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%24 = tail call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %47, label %26
26: ; preds = %19
%27 = load i8**, i8*** @environ
%28 = getelementptr inbounds i8*, i8** %27, i64 %21
%29 = load i8*, i8** %28
%30 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%31 = tail call i64 @strncmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %47, label %33
33: ; preds = %26
%34 = load i8**, i8*** @environ
%35 = getelementptr inbounds i8*, i8** %34, i64 %21
%36 = load i8*, i8** %35
%37 = tail call i32 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%38 = tail call i64 @strncmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %36, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %47, label %40
40: ; preds = %33
%41 = load i8**, i8*** @environ
%42 = getelementptr inbounds i8*, i8** %41, i64 %21
%43 = load i8*, i8** %42
%44 = add nsw i32 %22, 1
%45 = sext i32 %22 to i64
%46 = getelementptr inbounds i8*, i8** %15, i64 %45
store i8* %43, i8** %46
br label %47
47: ; preds = %19, %26, %33, %40
%48 = phi i32 [ %44, %40 ], [ %22, %33 ], [ %22, %26 ], [ %22, %19 ]
%49 = add nuw i64 %21, 1
%50 = load i8**, i8*** @environ
%51 = getelementptr inbounds i8*, i8** %50, i64 %49
%52 = load i8*, i8** %51
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %19
54: ; preds = %47, %10
%55 = phi i32 [ 0, %10 ], [ %48, %47 ]
%56 = tail call i8* @scopy(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%57 = add nsw i32 %55, 1
%58 = sext i32 %55 to i64
%59 = getelementptr inbounds i8*, i8** %15, i64 %58
store i8* %56, i8** %59
%60 = call i8* @scopy(i8* %2)
%61 = add nsw i32 %55, 2
%62 = sext i32 %57 to i64
%63 = getelementptr inbounds i8*, i8** %15, i64 %62
store i8* %60, i8** %63
%64 = call i8* @scopy(i8* %2)
%65 = add nsw i32 %55, 3
%66 = sext i32 %61 to i64
%67 = getelementptr inbounds i8*, i8** %15, i64 %66
store i8* %64, i8** %67
%68 = sext i32 %65 to i64
%69 = getelementptr inbounds i8*, i8** %15, i64 %68
store i8* null, i8** %69
store i8** %15, i8*** @environ
ret void
}