#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*/ abort();
scalar_t__ clearenv();
scalar_t__ setenv(char *, char *, int);
char *strchr(char *, char);
int set_environ(char *const *restrict new_environ, size_t n) {
  size_t i;
  if (clearenv() != 0)
    return -1;
  for (i = 0; i < n; i++) {
    char *var = new_environ[i];
    char *val = strchr(var, '=');
    if (val == NULL)
      abort();
    *val++ = '\0';
    if (setenv(var, val, 1) < 0)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_environ>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1050 <clearenv@plt>
cmp    $0x0,%rax
je     116d <set_environ+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <set_environ+0xce>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1207 <set_environ+0xc7>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3d,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b7 <set_environ+0x77>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0x0,%rax
jge    11f1 <set_environ+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <set_environ+0xce>
jmp    11f6 <set_environ+0xb6>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1175 <set_environ+0x35>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_environ>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <clearenv@plt>
test   %rax,%rax
je     116d <set_environ+0x2d>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r15,%r15
je     1160 <set_environ+0x20>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14,%rbx,8),%rbp
mov    %rbp,%rdi
mov    $0x3d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11bf <set_environ+0x7f>
mov    %rax,%rsi
add    $0x1,%rsi
movb   $0x0,(%rax)
mov    %rbp,%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
test   %rax,%rax
js     115b <set_environ+0x1b>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1180 <set_environ+0x40>
xor    %ebp,%ebp
jmp    1160 <set_environ+0x20>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
define i32 @set_environ(i8** noalias %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
store i8** %0, i8*** %4
store i64 %1, i64* %5
%9 = call i64 (...) @clearenv()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %41
12: ; preds = %2
store i64 0, i64* %6
br label %13
13: ; preds = %37, %12
%14 = load i64, i64* %6
%15 = load i64, i64* %5
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %40
17: ; preds = %13
%18 = load i8**, i8*** %4
%19 = load i64, i64* %6
%20 = getelementptr inbounds i8*, i8** %18, i64 %19
%21 = load i8*, i8** %20
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = call i8* @strchr(i8* %22, i8 signext 61)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %17
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %8
store i8 0, i8* %29
%31 = load i8*, i8** %7
%32 = load i8*, i8** %8
%33 = call i64 @setenv(i8* %31, i8* %32, i32 1)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %28
store i32 -1, i32* %3
br label %41
36: ; preds = %28
br label %37
37: ; preds = %36
%38 = load i64, i64* %6
%39 = add i64 %38, 1
store i64 %39, i64* %6
br label %13
40: ; preds = %13
store i32 0, i32* %3
br label %41
41: ; preds = %40, %35, %11
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
define i32 @set_environ(i8** noalias nocapture %0, i64 %1) {
%3 = tail call i64 (...) @clearenv()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %22
5: ; preds = %2
%6 = icmp eq i64 %1, 0
br i1 %6, label %22, label %9
7: ; preds = %17
%8 = icmp eq i64 %21, %1
br i1 %8, label %22, label %9
9: ; preds = %5, %7
%10 = phi i64 [ %21, %7 ], [ 0, %5 ]
%11 = getelementptr inbounds i8*, i8** %0, i64 %10
%12 = load i8*, i8** %11
%13 = tail call i8* @strchr(i8* dereferenceable(1) %12, i8 signext 61)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 (...) @abort()
unreachable
17: ; preds = %9
%18 = getelementptr inbounds i8, i8* %13, i64 1
store i8 0, i8* %13
%19 = tail call i64 @setenv(i8* %12, i8* %18, i32 1)
%20 = icmp slt i64 %19, 0
%21 = add nuw i64 %10, 1
br i1 %20, label %22, label %7
22: ; preds = %17, %7, %5, %2
%23 = phi i32 [ -1, %2 ], [ 0, %5 ], [ -1, %17 ], [ 0, %7 ]
ret i32 %23
}