#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 find_inenv(char **, char *);
char *my_strcat(char *, char *);
int /*<<< orphan*/ pprint(char *, int);
int /*<<< orphan*/ printenv(char **);
char **strto_wordtab(char *, char *);
char **set_environ(char *buff, char **environ) {
  static int i = 0;
  char *tmp;
  char **buffer;
  buffer = strto_wordtab(buff, " ");
  if (!(buffer[2])) {
    printenv(environ);
    return (environ);
  }
  if ((buffer[3])) {
    pprint("setenv: Too many arguments.\n", 2);
    return (environ);
  }
  tmp = my_strcat(buffer[1], "=");
  i = find_inenv(environ, buffer[1]);
  if (!(i)) {
    while (environ[i])
      i++;
  }
  environ[i] = my_strcat(tmp, buffer[2]);
  environ[i + 1] = 0;
  return (environ);
}


x86 assembly (clang, -O0):
<set_environ>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <strto_wordtab@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,0x10(%rax)
jne    1199 <set_environ+0x49>
mov    -0x18(%rbp),%rdi
call   1050 <printenv@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1279 <set_environ+0x129>
mov    -0x28(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     11c6 <set_environ+0x76>
lea    0xe53(%rip),%rdi
mov    $0x2,%esi
call   1060 <pprint@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1279 <set_environ+0x129>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4a(%rip),%rsi
call   1040 <my_strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <find_inenv@plt>
mov    %eax,0x2e57(%rip)
cmpl   $0x0,0x2e50(%rip)
jne    1236 <set_environ+0xe6>
jmp    1207 <set_environ+0xb7>
mov    -0x18(%rbp),%rax
movslq 0x2e3a(%rip),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1231 <set_environ+0xe1>
mov    0x2e29(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e20(%rip)
jmp    1207 <set_environ+0xb7>
jmp    1236 <set_environ+0xe6>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rsi
call   1040 <my_strcat@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq 0x2df7(%rip),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    0x2de9(%rip),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_environ>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
lea    0xea1(%rip),%rsi
call   1070 <strto_wordtab@plt>
cmpq   $0x0,0x10(%rax)
je     1188 <set_environ+0x38>
mov    %rax,%r14
cmpq   $0x0,0x18(%rax)
je     1192 <set_environ+0x42>
lea    0xe86(%rip),%rdi
mov    $0x2,%esi
call   1060 <pprint@plt>
jmp    1202 <set_environ+0xb2>
mov    %rbx,%rdi
call   1050 <printenv@plt>
jmp    1202 <set_environ+0xb2>
mov    0x8(%r14),%rdi
lea    0xe82(%rip),%rsi
call   1040 <my_strcat@plt>
mov    %rax,%r15
mov    0x8(%r14),%rsi
mov    %rbx,%rdi
call   1030 <find_inenv@plt>
mov    %eax,0x2e95(%rip)
test   %eax,%eax
jne    11e2 <set_environ+0x92>
cmpq   $0x0,(%rbx)
je     11e2 <set_environ+0x92>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpq   $0x0,0x8(%rbx,%rax,8)
lea    0x1(%rax),%rax
jne    11d0 <set_environ+0x80>
mov    %eax,0x2e6a(%rip)
mov    0x10(%r14),%rsi
mov    %r15,%rdi
call   1040 <my_strcat@plt>
movslq 0x2e57(%rip),%rcx
mov    %rax,(%rbx,%rcx,8)
movq   $0x0,0x8(%rbx,%rcx,8)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@set_environ.i = internal global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [29 x i8] c"setenv: Too many arguments.\0A\00"
@.str.2 = [2 x i8] c"=\00"
define i8** @set_environ(i8* %0, i8** %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8**
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i8*, i8** %4
%9 = call i8** @strto_wordtab(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8** %9, i8*** %7
%10 = load i8**, i8*** %7
%11 = getelementptr inbounds i8*, i8** %10, i64 2
%12 = load i8*, i8** %11
%13 = icmp ne i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = call i32 @printenv(i8** %15)
%17 = load i8**, i8*** %5
store i8** %17, i8*** %3
br label %66
18: ; preds = %2
%19 = load i8**, i8*** %7
%20 = getelementptr inbounds i8*, i8** %19, i64 3
%21 = load i8*, i8** %20
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @pprint(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 2)
%25 = load i8**, i8*** %5
store i8** %25, i8*** %3
br label %66
26: ; preds = %18
%27 = load i8**, i8*** %7
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i8* @my_strcat(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %30, i8** %6
%31 = load i8**, i8*** %5
%32 = load i8**, i8*** %7
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @find_inenv(i8** %31, i8* %34)
store i32 %35, i32* @set_environ.i
%36 = load i32, i32* @set_environ.i
%37 = icmp ne i32 %36, 0
br i1 %37, label %50, label %38
38: ; preds = %26
br label %39
39: ; preds = %46, %38
%40 = load i8**, i8*** %5
%41 = load i32, i32* @set_environ.i
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %40, i64 %42
%44 = load i8*, i8** %43
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* @set_environ.i
%48 = add nsw i32 %47, 1
store i32 %48, i32* @set_environ.i
br label %39
49: ; preds = %39
br label %50
50: ; preds = %49, %26
%51 = load i8*, i8** %6
%52 = load i8**, i8*** %7
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = call i8* @my_strcat(i8* %51, i8* %54)
%56 = load i8**, i8*** %5
%57 = load i32, i32* @set_environ.i
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
store i8* %55, i8** %59
%60 = load i8**, i8*** %5
%61 = load i32, i32* @set_environ.i
%62 = add nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %60, i64 %63
store i8* null, i8** %64
%65 = load i8**, i8*** %5
store i8** %65, i8*** %3
br label %66
66: ; preds = %50, %23, %14
%67 = load i8**, i8*** %3
ret i8** %67
}

LLVM IR (-O2):
@set_environ.i = internal unnamed_addr global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [29 x i8] c"setenv: Too many arguments.\0A\00"
@.str.2 = [2 x i8] c"=\00"
define i8** @set_environ(i8* %0, i8** returned %1) {
%3 = tail call i8** @strto_wordtab(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds i8*, i8** %3, i64 2
%5 = load i8*, i8** %4
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @printenv(i8** %1)
br label %42
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %3, i64 3
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @pprint(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 2)
br label %42
15: ; preds = %9
%16 = getelementptr inbounds i8*, i8** %3, i64 1
%17 = load i8*, i8** %16
%18 = tail call i8* @my_strcat(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = load i8*, i8** %16
%20 = tail call i32 @find_inenv(i8** %1, i8* %19)
store i32 %20, i32* @set_environ.i
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %15
%23 = load i8*, i8** %1
%24 = icmp eq i8* %23, null
br i1 %24, label %33, label %25
25: ; preds = %22, %25
%26 = phi i64 [ %27, %25 ], [ 0, %22 ]
%27 = add i64 %26, 1
%28 = getelementptr inbounds i8*, i8** %1, i64 %27
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %25
31: ; preds = %25
%32 = trunc i64 %27 to i32
store i32 %32, i32* @set_environ.i
br label %33
33: ; preds = %22, %31, %15
%34 = load i8*, i8** %4
%35 = tail call i8* @my_strcat(i8* %18, i8* %34)
%36 = load i32, i32* @set_environ.i
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %1, i64 %37
store i8* %35, i8** %38
%39 = add nsw i32 %36, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %1, i64 %40
store i8* null, i8** %41
br label %42
42: ; preds = %33, %13, %7
ret i8** %1
}