#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*/ EINVAL;
int /*<<< orphan*/ debug(char *);
int /*<<< orphan*/ errno;
scalar_t__ fileinpath(char const *, char *);
scalar_t__ getenv(char *);
int sys_setenv(char const *, char const *, int);
int setenv(const char *name, const char *value, int overwrite) {
  if (fileinpath(name, "SUDO_TOUCHENV") || getenv("SYS_SETENV")) {
    return sys_setenv(name, value, overwrite);
  }
  debug("setenv: ");
  errno = EINVAL;
  return -1;
}


x86 assembly (clang, -O0):
<setenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fileinpath@plt>
cmp    $0x0,%rax
jne    1183 <setenv+0x43>
lea    0xe9a(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     119b <setenv+0x5b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <sys_setenv@plt>
mov    %eax,-0x4(%rbp)
jmp    11c0 <setenv+0x80>
lea    0xe77(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e29(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setenv>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xeac(%rip),%rsi
call   1060 <fileinpath@plt>
test   %rax,%rax
jne    116f <setenv+0x2f>
lea    0xea9(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1181 <setenv+0x41>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <sys_setenv@plt>
lea    0xe91(%rip),%rdi
call   1040 <debug@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    0x2e43(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"SUDO_TOUCHENV\00"
@.str.1 = [11 x i8] c"SYS_SETENV\00"
@.str.2 = [9 x i8] c"setenv: \00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @setenv(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i8*, i8** %5
%9 = call i64 @fileinpath(i8* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = call i64 @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11, %3
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %7
%18 = call i32 @sys_setenv(i8* %15, i8* %16, i32 %17)
store i32 %18, i32* %4
br label %22
19: ; preds = %11
%20 = call i32 @debug(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EINVAL
store i32 %21, i32* @errno
store i32 -1, i32* %4
br label %22
22: ; preds = %19, %14
%23 = load i32, i32* %4
ret i32 %23
}

LLVM IR (-O2):
@.str = [14 x i8] c"SUDO_TOUCHENV\00"
@.str.1 = [11 x i8] c"SYS_SETENV\00"
@.str.2 = [9 x i8] c"setenv: \00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @setenv(i8* %0, i8* %1, i32 %2) {
%4 = tail call i64 @fileinpath(i8* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i64 @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6, %3
%10 = tail call i32 @sys_setenv(i8* %0, i8* %1, i32 %2)
br label %14
11: ; preds = %6
%12 = tail call i32 @debug(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
br label %14
14: ; preds = %11, %9
%15 = phi i32 [ %10, %9 ], [ -1, %11 ]
ret i32 %15
}