#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 */
struct snck_string {
  int dummy;
};
struct snck_ctxt {
  int dummy;
};

/* Variables and functions */
scalar_t__ setenv(char *, char *, int);
char *snck_string_get(struct snck_ctxt const *const,
                      struct snck_string const *const);
int /*<<< orphan*/ snck_string_put(struct snck_ctxt const *const, char *);
scalar_t__ unsetenv(char *);
char snck_env_set(struct snck_ctxt const *const p_ctxt,
                  struct snck_string const *const p_name,
                  struct snck_string const *const p_value) {
  char b_result;
  char *p_name0;
  p_name0 = snck_string_get(p_ctxt, p_name);
  if (p_name0) {
    if (p_value) {
      char *p_value0;
      p_value0 = snck_string_get(p_ctxt, p_value);
      if (p_value0) {
        if (0 == setenv(p_name0, p_value0, 1)) {
          b_result = 1;
        } else {
          b_result = 0;
        }
        snck_string_put(p_ctxt, p_value0);
      } else {
        b_result = 0;
      }
    } else {
      if (0 == unsetenv(p_name0)) {
        b_result = 1;
      } else {
        b_result = 0;
      }
    }
    snck_string_put(p_ctxt, p_name0);
  } else {
    b_result = 0;
  }
  return b_result;
}


x86 assembly (clang, -O0):
<snck_env_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <snck_string_get@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     121a <snck_env_set+0xda>
cmpq   $0x0,-0x18(%rbp)
je     11df <snck_env_set+0x9f>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <snck_string_get@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d6 <snck_env_set+0x96>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11c0 <snck_env_set+0x80>
movb   $0x1,-0x19(%rbp)
jmp    11c4 <snck_env_set+0x84>
movb   $0x0,-0x19(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <snck_string_put@plt>
jmp    11da <snck_env_set+0x9a>
movb   $0x0,-0x19(%rbp)
jmp    1208 <snck_env_set+0xc8>
mov    -0x28(%rbp),%rdi
call   1050 <unsetenv@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11ff <snck_env_set+0xbf>
movb   $0x1,-0x19(%rbp)
jmp    1203 <snck_env_set+0xc3>
movb   $0x0,-0x19(%rbp)
jmp    1208 <snck_env_set+0xc8>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <snck_string_put@plt>
jmp    121e <snck_env_set+0xde>
movb   $0x0,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<snck_env_set>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rdi,%r14
call   1060 <snck_string_get@plt>
test   %rax,%rax
je     1196 <snck_env_set+0x56>
mov    %rax,%rbx
test   %rbp,%rbp
je     119a <snck_env_set+0x5a>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <snck_string_get@plt>
test   %rax,%rax
je     11ab <snck_env_set+0x6b>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
test   %rax,%rax
sete   %bpl
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <snck_string_put@plt>
jmp    11ad <snck_env_set+0x6d>
xor    %ebp,%ebp
jmp    11b8 <snck_env_set+0x78>
mov    %rbx,%rdi
call   1050 <unsetenv@plt>
test   %rax,%rax
sete   %bpl
jmp    11ad <snck_env_set+0x6d>
xor    %ebp,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <snck_string_put@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.snck_ctxt = type { i32 }
%struct.snck_string = type { i32 }
define signext i8 @snck_env_set(%struct.snck_ctxt* %0, %struct.snck_string* %1, %struct.snck_string* %2) {
%4 = alloca %struct.snck_ctxt*
%5 = alloca %struct.snck_string*
%6 = alloca %struct.snck_string*
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i8*
store %struct.snck_ctxt* %0, %struct.snck_ctxt** %4
store %struct.snck_string* %1, %struct.snck_string** %5
store %struct.snck_string* %2, %struct.snck_string** %6
%10 = load %struct.snck_ctxt*, %struct.snck_ctxt** %4
%11 = load %struct.snck_string*, %struct.snck_string** %5
%12 = call i8* @snck_string_get(%struct.snck_ctxt* %10, %struct.snck_string* %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %48
15: ; preds = %3
%16 = load %struct.snck_string*, %struct.snck_string** %6
%17 = icmp ne %struct.snck_string* %16, null
br i1 %17, label %18, label %37
18: ; preds = %15
%19 = load %struct.snck_ctxt*, %struct.snck_ctxt** %4
%20 = load %struct.snck_string*, %struct.snck_string** %6
%21 = call i8* @snck_string_get(%struct.snck_ctxt* %19, %struct.snck_string* %20)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %35
24: ; preds = %18
%25 = load i8*, i8** %8
%26 = load i8*, i8** %9
%27 = call i64 @setenv(i8* %25, i8* %26, i32 1)
%28 = icmp eq i64 0, %27
br i1 %28, label %29, label %30
29: ; preds = %24
store i8 1, i8* %7
br label %31
30: ; preds = %24
store i8 0, i8* %7
br label %31
31: ; preds = %30, %29
%32 = load %struct.snck_ctxt*, %struct.snck_ctxt** %4
%33 = load i8*, i8** %9
%34 = call i32 @snck_string_put(%struct.snck_ctxt* %32, i8* %33)
br label %36
35: ; preds = %18
store i8 0, i8* %7
br label %36
36: ; preds = %35, %31
br label %44
37: ; preds = %15
%38 = load i8*, i8** %8
%39 = call i64 @unsetenv(i8* %38)
%40 = icmp eq i64 0, %39
br i1 %40, label %41, label %42
41: ; preds = %37
store i8 1, i8* %7
br label %43
42: ; preds = %37
store i8 0, i8* %7
br label %43
43: ; preds = %42, %41
br label %44
44: ; preds = %43, %36
%45 = load %struct.snck_ctxt*, %struct.snck_ctxt** %4
%46 = load i8*, i8** %8
%47 = call i32 @snck_string_put(%struct.snck_ctxt* %45, i8* %46)
br label %49
48: ; preds = %3
store i8 0, i8* %7
br label %49
49: ; preds = %48, %44
%50 = load i8, i8* %7
ret i8 %50
}

LLVM IR (-O2):
%struct.snck_ctxt = type { i32 }
%struct.snck_string = type { i32 }
define signext i8 @snck_env_set(%struct.snck_ctxt* %0, %struct.snck_string* %1, %struct.snck_string* %2) {
%4 = tail call i8* @snck_string_get(%struct.snck_ctxt* %0, %struct.snck_string* %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = icmp eq %struct.snck_string* %2, null
br i1 %7, label %15, label %8
8: ; preds = %6
%9 = tail call i8* @snck_string_get(%struct.snck_ctxt* %0, %struct.snck_string* %2)
%10 = icmp eq i8* %9, null
br i1 %10, label %18, label %11
11: ; preds = %8
%12 = tail call i64 @setenv(i8* %4, i8* %9, i32 1)
%13 = icmp eq i64 %12, 0
%14 = tail call i32 @snck_string_put(%struct.snck_ctxt* %0, i8* %9)
br label %18
15: ; preds = %6
%16 = tail call i64 @unsetenv(i8* %4)
%17 = icmp eq i64 %16, 0
br label %18
18: ; preds = %15, %11, %8
%19 = phi i1 [ %13, %11 ], [ false, %8 ], [ %17, %15 ]
%20 = zext i1 %19 to i8
%21 = tail call i32 @snck_string_put(%struct.snck_ctxt* %0, i8* %4)
br label %22
22: ; preds = %3, %18
%23 = phi i8 [ %20, %18 ], [ 0, %3 ]
ret i8 %23
}