#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 */
int /*<<< orphan*/ close(int);
int creat(char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
int putenv(char *s) {
  int f, n;
  char *value;
  char buf[300];
  value = strchr(s, '=');
  if (value) {
    n = value - s;
    if (n <= 0 || n > sizeof(buf) - 6)
      return -1;
    strcpy(buf, "/env/");
    strncpy(buf + 5, s, n);
    buf[n + 5] = 0;
    f = creat(buf, 0666);
    if (f < 0)
      return 1;
    value++;
    n = strlen(value);
    if (write(f, value, n) != n)
      return -1;
    close(f);
    return 0;
  } else
    return -1;
}


x86 assembly (clang, -O0):
<putenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3d,%esi
call   1070 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1284 <putenv+0x114>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11c4 <putenv+0x54>
movslq -0x18(%rbp),%rax
cmp    $0x126,%rax
jbe    11d0 <putenv+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <putenv+0x11b>
lea    -0x150(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x150(%rbp),%rdi
add    $0x5,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%eax
add    $0x5,%eax
cltq
movb   $0x0,-0x150(%rbp,%rax,1)
lea    -0x150(%rbp),%rdi
mov    $0x1b6,%esi
call   1090 <creat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1234 <putenv+0xc4>
movl   $0x1,-0x4(%rbp)
jmp    128b <putenv+0x11b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <write@plt>
cmp    -0x18(%rbp),%eax
je     1270 <putenv+0x100>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <putenv+0x11b>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    128b <putenv+0x11b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putenv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x130,%rsp
mov    %rdi,%rbp
mov    $0x3d,%esi
call   1070 <strchr@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     121f <putenv+0xaf>
mov    %rax,%r15
mov    %rax,%rbx
sub    %rbp,%rbx
lea    -0x127(%rbx),%eax
cmp    $0xfffffeda,%eax
jb     121f <putenv+0xaf>
lea    0xe48(%rip),%rsi
mov    %rsp,%r12
mov    %r12,%rdi
call   1040 <strcpy@plt>
lea    0x5(%rsp),%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
add    $0x5,%ebx
movslq %ebx,%rax
movb   $0x0,(%rsp,%rax,1)
mov    %r12,%rdi
mov    $0x1b6,%esi
call   1090 <creat@plt>
test   %eax,%eax
js     1219 <putenv+0xa9>
mov    %eax,%ebp
add    $0x1,%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    %ebx,%eax
jne    121f <putenv+0xaf>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %r14d,%r14d
jmp    121f <putenv+0xaf>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"/env/\00"
define i32 @putenv(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [300 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strchr(i8* %8, i8 signext 61)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %58
12: ; preds = %1
%13 = load i8*, i8** %6
%14 = load i8*, i8** %3
%15 = ptrtoint i8* %13 to i64
%16 = ptrtoint i8* %14 to i64
%17 = sub i64 %15, %16
%18 = trunc i64 %17 to i32
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp sle i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %12
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = icmp ugt i64 %23, 294
br i1 %24, label %25, label %26
25: ; preds = %21, %12
store i32 -1, i32* %2
br label %59
26: ; preds = %21
%27 = getelementptr inbounds [300 x i8], [300 x i8]* %7, i64 0, i64 0
%28 = call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%29 = getelementptr inbounds [300 x i8], [300 x i8]* %7, i64 0, i64 0
%30 = getelementptr inbounds i8, i8* %29, i64 5
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = call i32 @strncpy(i8* %30, i8* %31, i32 %32)
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [300 x i8], [300 x i8]* %7, i64 0, i64 %36
store i8 0, i8* %37
%38 = getelementptr inbounds [300 x i8], [300 x i8]* %7, i64 0, i64 0
%39 = call i32 @creat(i8* %38, i32 438)
store i32 %39, i32* %4
%40 = load i32, i32* %4
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %26
store i32 1, i32* %2
br label %59
43: ; preds = %26
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %6
%46 = load i8*, i8** %6
%47 = call i32 @strlen(i8* %46)
store i32 %47, i32* %5
%48 = load i32, i32* %4
%49 = load i8*, i8** %6
%50 = load i32, i32* %5
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
%52 = load i32, i32* %5
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %43
store i32 -1, i32* %2
br label %59
55: ; preds = %43
%56 = load i32, i32* %4
%57 = call i32 @close(i32 %56)
store i32 0, i32* %2
br label %59
58: ; preds = %1
store i32 -1, i32* %2
br label %59
59: ; preds = %58, %55, %54, %42, %25
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
@.str = [6 x i8] c"/env/\00"
define i32 @putenv(i8* %0) {
%2 = alloca [300 x i8]
%3 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 61)
%5 = icmp eq i8* %4, null
br i1 %5, label %30, label %6
6: ; preds = %1
%7 = ptrtoint i8* %4 to i64
%8 = ptrtoint i8* %0 to i64
%9 = sub i64 %7, %8
%10 = trunc i64 %9 to i32
%11 = add i32 %10, -295
%12 = icmp ult i32 %11, -294
br i1 %12, label %30, label %13
13: ; preds = %6
%14 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 5
%16 = call i32 @strncpy(i8* %15, i8* %0, i32 %10)
%17 = shl i64 %9, 32
%18 = add i64 %17, 21474836480
%19 = ashr exact i64 %18, 32
%20 = getelementptr inbounds [300 x i8], [300 x i8]* %2, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 @creat(i8* %3, i32 438)
%22 = icmp slt i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %13
%24 = getelementptr inbounds i8, i8* %4, i64 1
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @write(i32 %21, i8* %24, i32 %25)
%27 = icmp eq i32 %26, %25
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @close(i32 %21)
br label %30
30: ; preds = %1, %23, %13, %6, %28
%31 = phi i32 [ 0, %28 ], [ -1, %6 ], [ 1, %13 ], [ -1, %23 ], [ -1, %1 ]
ret i32 %31
}