#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 */
char *file_contents(char const *, char *, char **);
int /*<<< orphan*/ free(char *);
char *sr_strdup(char *);
char *strchrnul(char *, char);
int /*<<< orphan*/ strlen(char *);
char *strstr(char *, char *);
char *desktop_from_dir(const char *directory, char **error_message) {
  char *environ_contents = file_contents(directory, "environ", error_message);
  if (!environ_contents)
    return NULL;
  char *desktop = strstr(environ_contents, "DESKTOP_SESSION=");
  if (!desktop) {
    free(environ_contents);
    return NULL;
  }
  if (desktop != environ_contents && *(desktop - 1) != '\n') {
    free(environ_contents);
    return NULL;
  }
  desktop += strlen("DESKTOP_SESSION=");
  char *newline = strchrnul(desktop, '\n');
  *newline = '\0';
  char *result = sr_strdup(desktop);
  free(environ_contents);
  return result;
}


x86 assembly (clang, -O0):
<desktop_from_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe81(%rip),%rsi
call   1070 <file_contents@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a0 <desktop_from_dir+0x40>
movq   $0x0,-0x8(%rbp)
jmp    125d <desktop_from_dir+0xfd>
mov    -0x20(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d5 <desktop_from_dir+0x75>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    125d <desktop_from_dir+0xfd>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     120a <desktop_from_dir+0xaa>
mov    -0x28(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
je     120a <desktop_from_dir+0xaa>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    125d <desktop_from_dir+0xfd>
lea    0xdf7(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchrnul@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <sr_strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<desktop_from_dir>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rdx
lea    0xe92(%rip),%rsi
call   1070 <file_contents@plt>
test   %rax,%rax
je     11cb <desktop_from_dir+0x6b>
mov    %rax,%r14
lea    0xe86(%rip),%rsi
mov    %rax,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     11cf <desktop_from_dir+0x6f>
mov    %rax,%rbx
cmp    %r14,%rax
je     119d <desktop_from_dir+0x3d>
cmpb   $0xa,-0x1(%rbx)
jne    11cf <desktop_from_dir+0x6f>
lea    0xe64(%rip),%rdi
call   1050 <strlen@plt>
cltq
add    %rax,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strchrnul@plt>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1060 <sr_strdup@plt>
mov    %rax,%rbx
jmp    11d1 <desktop_from_dir+0x71>
xor    %ebx,%ebx
jmp    11d9 <desktop_from_dir+0x79>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [8 x i8] c"environ\00"
@.str.1 = [17 x i8] c"DESKTOP_SESSION=\00"
define i8* @desktop_from_dir(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%10 = load i8*, i8** %4
%11 = load i8**, i8*** %5
%12 = call i8* @file_contents(i8* %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* null, i8** %3
br label %50
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = call i8* @strstr(i8* %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
store i8* null, i8** %3
br label %50
24: ; preds = %16
%25 = load i8*, i8** %7
%26 = load i8*, i8** %6
%27 = icmp ne i8* %25, %26
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 -1
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 10
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = call i32 @free(i8* %35)
store i8* null, i8** %3
br label %50
37: ; preds = %28, %24
%38 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%39 = load i8*, i8** %7
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8* %41, i8** %7
%42 = load i8*, i8** %7
%43 = call i8* @strchrnul(i8* %42, i8 signext 10)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
store i8 0, i8* %44
%45 = load i8*, i8** %7
%46 = call i8* @sr_strdup(i8* %45)
store i8* %46, i8** %9
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %9
store i8* %49, i8** %3
br label %50
50: ; preds = %37, %34, %21, %15
%51 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
@.str = [8 x i8] c"environ\00"
@.str.1 = [17 x i8] c"DESKTOP_SESSION=\00"
define i8* @desktop_from_dir(i8* %0, i8** %1) {
%3 = tail call i8* @file_contents(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %23, label %5
5: ; preds = %2
%6 = tail call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %20, label %8
8: ; preds = %5
%9 = icmp eq i8* %6, %3
br i1 %9, label %14, label %10
10: ; preds = %8
%11 = getelementptr inbounds i8, i8* %6, i64 -1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %20
14: ; preds = %10, %8
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %6, i64 %16
%18 = tail call i8* @strchrnul(i8* %17, i8 signext 10)
store i8 0, i8* %18
%19 = tail call i8* @sr_strdup(i8* %17)
br label %20
20: ; preds = %10, %5, %14
%21 = phi i8* [ %19, %14 ], [ null, %5 ], [ null, %10 ]
%22 = tail call i32 @free(i8* %3)
br label %23
23: ; preds = %20, %2
%24 = phi i8* [ null, %2 ], [ %21, %20 ]
ret i8* %24
}