#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 connect_unix_socket(char *, int, int);
char *getenv(char *);
int /*<<< orphan*/ memset(char *, char, int);
char *strchr(char *, char);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int dbus_connect_session(void) {
  char *session_address = getenv("DBUS_SESSION_BUS_ADDRESS");
  if (!session_address) {
    return -1;
  }
  if (strncmp("unix:", session_address, 5) == 0) {
    char abstract[128];
    char *flags = session_address + 5;
    memset(abstract, '\0', 128);
    for (; *flags != '\0';) {
      char *end, *flagval;
      end = strchr(flags, ',');
      if (!end)
        end = flags + strlen(flags);
      if (strncmp("abstract=", flags, 9) == 0) {
        flagval = flags + 9;
        strncpy(abstract, flagval, end - flagval);
      }
      if (*end != ',')
        break;
      flags = end + 1;
    }
    return connect_unix_socket(abstract, 1, strlen(abstract));
  }
  return -1;
}


x86 assembly (clang, -O0):
<dbus_connect_session>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
lea    0xe7e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a2 <dbus_connect_session+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1311 <dbus_connect_session+0x1a1>
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdi
mov    $0x5,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    130a <dbus_connect_session+0x19a>
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1090 <memset@plt>
mov    -0x98(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12d5 <dbus_connect_session+0x165>
mov    -0x98(%rbp),%rdi
mov    $0x2c,%esi
call   1080 <strchr@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
jne    124c <dbus_connect_session+0xdc>
mov    -0x98(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x98(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0xb0(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0xa0(%rbp)
mov    -0x98(%rbp),%rsi
lea    0xdc5(%rip),%rdi
mov    $0x9,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
jne    12a6 <dbus_connect_session+0x136>
mov    -0x98(%rbp),%rax
add    $0x9,%rax
mov    %rax,-0xa8(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
mov    -0xa0(%rbp),%rax
mov    -0xa8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <strncpy@plt>
mov    -0xa0(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2c,%eax
je     12be <dbus_connect_session+0x14e>
jmp    12d5 <dbus_connect_session+0x165>
mov    -0xa0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x98(%rbp)
jmp    11e3 <dbus_connect_session+0x73>
lea    -0x90(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0x90(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0xb8(%rbp),%rdi
mov    %eax,%edx
mov    $0x1,%esi
call   1040 <connect_unix_socket@plt>
mov    %eax,-0x4(%rbp)
jmp    1311 <dbus_connect_session+0x1a1>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dbus_connect_session>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
lea    0xe7b(%rip),%rdi
call   1030 <getenv@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     125a <dbus_connect_session+0xea>
mov    %rax,%r12
lea    0xe77(%rip),%rdi
mov    %rax,%rsi
mov    $0x5,%edx
call   1060 <strncmp@plt>
test   %rax,%rax
jne    125a <dbus_connect_session+0xea>
add    $0x5,%r12
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1090 <memset@plt>
lea    0xe4a(%rip),%r15
jmp    11e9 <dbus_connect_session+0x79>
nopw   0x0(%rax,%rax,1)
lea    0x1(%rbx),%r12
cmpb   $0x2c,(%rbx)
jne    123e <dbus_connect_session+0xce>
cmpb   $0x0,(%r12)
je     123e <dbus_connect_session+0xce>
mov    %r12,%rdi
mov    $0x2c,%esi
call   1080 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1213 <dbus_connect_session+0xa3>
mov    %r12,%rdi
call   1070 <strlen@plt>
movslq %eax,%rbx
add    %r12,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x9,%edx
call   1060 <strncmp@plt>
test   %rax,%rax
jne    11e0 <dbus_connect_session+0x70>
add    $0x9,%r12
mov    %ebx,%edx
sub    %r12d,%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <strncpy@plt>
jmp    11e0 <dbus_connect_session+0x70>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   1040 <connect_unix_socket@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [25 x i8] c"DBUS_SESSION_BUS_ADDRESS\00"
@.str.1 = [6 x i8] c"unix:\00"
@.str.2 = [10 x i8] c"abstract=\00"
define i32 @dbus_connect_session() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [128 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = call i8* @getenv(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %0
store i32 -1, i32* %1
br label %67
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %66
15: ; preds = %11
%16 = load i8*, i8** %2
%17 = getelementptr inbounds i8, i8* %16, i64 5
store i8* %17, i8** %4
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i8 signext 0, i32 128)
br label %20
20: ; preds = %58, %15
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %61
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = call i8* @strchr(i8* %26, i8 signext 44)
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %36, label %30
30: ; preds = %25
%31 = load i8*, i8** %4
%32 = load i8*, i8** %4
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8* %35, i8** %5
br label %36
36: ; preds = %30, %25
%37 = load i8*, i8** %4
%38 = call i64 @strncmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %37, i32 9)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %52
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i64 9
store i8* %42, i8** %6
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%44 = load i8*, i8** %6
%45 = load i8*, i8** %5
%46 = load i8*, i8** %6
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = trunc i64 %49 to i32
%51 = call i32 @strncpy(i8* %43, i8* %44, i32 %50)
br label %52
52: ; preds = %40, %36
%53 = load i8*, i8** %5
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 44
br i1 %56, label %57, label %58
57: ; preds = %52
br label %61
58: ; preds = %52
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i64 1
store i8* %60, i8** %4
br label %20
61: ; preds = %57, %20
%62 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%63 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = call i32 @connect_unix_socket(i8* %62, i32 1, i32 %64)
store i32 %65, i32* %1
br label %67
66: ; preds = %11
store i32 -1, i32* %1
br label %67
67: ; preds = %66, %61, %10
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@.str = [25 x i8] c"DBUS_SESSION_BUS_ADDRESS\00"
@.str.1 = [6 x i8] c"unix:\00"
@.str.2 = [10 x i8] c"abstract=\00"
define i32 @dbus_connect_session() {
%1 = alloca [128 x i8]
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %40, label %4
4: ; preds = %0
%5 = tail call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32 5)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %40
7: ; preds = %4
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%9 = getelementptr inbounds i8, i8* %2, i64 5
%10 = call i32 @memset(i8* %8, i8 signext 0, i32 128)
br label %11
11: ; preds = %33, %7
%12 = phi i8* [ %9, %7 ], [ %36, %33 ]
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %37, label %15
15: ; preds = %11
%16 = call i8* @strchr(i8* %12, i8 signext 44)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = call i32 @strlen(i8* %12)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %12, i64 %20
br label %22
22: ; preds = %18, %15
%23 = phi i8* [ %16, %15 ], [ %21, %18 ]
%24 = call i64 @strncmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %12, i32 9)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = getelementptr inbounds i8, i8* %12, i64 9
%28 = ptrtoint i8* %23 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = trunc i64 %30 to i32
%32 = call i32 @strncpy(i8* %8, i8* %27, i32 %31)
br label %33
33: ; preds = %26, %22
%34 = load i8, i8* %23
%35 = icmp eq i8 %34, 44
%36 = getelementptr inbounds i8, i8* %23, i64 1
br i1 %35, label %11, label %37
37: ; preds = %33, %11
%38 = call i32 @strlen(i8* %8)
%39 = call i32 @connect_unix_socket(i8* %8, i32 1, i32 %38)
br label %40
40: ; preds = %4, %0, %37
%41 = phi i32 [ %39, %37 ], [ -1, %0 ], [ -1, %4 ]
ret i32 %41
}