#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*/ free(char *);
int /*<<< orphan*/ logerrx(char *);
char *malloc(size_t);
int /*<<< orphan*/ strlcpy(char *, char *, size_t);
int strlen(char *);
char *strrchr(char *, char);
int symset(char *, char *, int);
int cmdline_symset(char *s) {
  char *sym, *val;
  int ret;
  size_t len;
  if ((val = strrchr(s, '=')) == NULL)
    return (-1);
  len = strlen(s) - strlen(val) + 1;
  if ((sym = malloc(len)) == NULL)
    logerrx("cmdline_symset: malloc");
  strlcpy(sym, s, len);
  ret = symset(sym, val + 1, 1);
  free(sym);
  return (ret);
}


x86 assembly (clang, -O0):
<cmdline_symset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3d,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a4 <cmdline_symset+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <cmdline_symset+0xb5>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ec <cmdline_symset+0x7c>
lea    0xe19(%rip),%rdi
call   1090 <logerrx@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <strlcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edx
call   1060 <symset@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmdline_symset>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    $0x3d,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11f3 <cmdline_symset+0x83>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
add    $0x1,%ebp
movslq %ebp,%r12
mov    %r12,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11c3 <cmdline_symset+0x53>
lea    0xe42(%rip),%rdi
call   1090 <logerrx@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1080 <strlcpy@plt>
add    $0x1,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <symset@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    11f8 <cmdline_symset+0x88>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"cmdline_symset: malloc\00"
define i32 @cmdline_symset(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strrchr(i8* %8, i8 signext 61)
store i8* %9, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %37
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
%17 = sub nsw i32 %14, %16
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = call i32 @logerrx(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %12
%26 = load i8*, i8** %4
%27 = load i8*, i8** %3
%28 = load i64, i64* %7
%29 = call i32 @strlcpy(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %4
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = call i32 @symset(i8* %30, i8* %32, i32 1)
store i32 %33, i32* %6
%34 = load i8*, i8** %4
%35 = call i32 @free(i8* %34)
%36 = load i32, i32* %6
store i32 %36, i32* %2
br label %37
37: ; preds = %25, %11
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [23 x i8] c"cmdline_symset: malloc\00"
define i32 @cmdline_symset(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 61)
%3 = icmp eq i8* %2, null
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = tail call i32 @strlen(i8* %0)
%6 = tail call i32 @strlen(i8* %2)
%7 = add i32 %5, 1
%8 = sub i32 %7, %6
%9 = sext i32 %8 to i64
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 @logerrx(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %4
%15 = tail call i32 @strlcpy(i8* %10, i8* %0, i64 %9)
%16 = getelementptr inbounds i8, i8* %2, i64 1
%17 = tail call i32 @symset(i8* %10, i8* %16, i32 1)
%18 = tail call i32 @free(i8* %10)
br label %19
19: ; preds = %1, %14
%20 = phi i32 [ %17, %14 ], [ -1, %1 ]
ret i32 %20
}