#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*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
scalar_t__ geteuid();
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *,
                            char *);
int /*<<< orphan*/ stderr;
const char *getdwd(char *suffix) {
  const char *daemon_name = getenv("DAEMONNAME");
  static char workingpath[128];
  const char *pathprefix = NULL;
  if (!daemon_name) {
    fprintf(stderr, "can't get env DAEMONNAME\n");
    return NULL;
  }
  if (!suffix)
    suffix = "";
  if (geteuid() != 0) {
    pathprefix = "/tmp/";
  } else {
    pathprefix = "/run/";
  }
  snprintf(workingpath, 128 - 1, "%s%s/%s", pathprefix, daemon_name, suffix);
  return workingpath;
}


x86 assembly (clang, -O0):
<getdwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1191 <getdwd+0x51>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1202 <getdwd+0xc2>
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <getdwd+0x67>
lea    0xe81(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
cmp    $0x0,%rax
je     11c8 <getdwd+0x88>
lea    0xe66(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11d3 <getdwd+0x93>
lea    0xe5c(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0x2e6a(%rip),%rdi
mov    $0x7f,%esi
lea    0xe3f(%rip),%rdx
call   1040 <snprintf@plt>
lea    0x2e52(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdwd>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a9 <getdwd+0x69>
mov    %rax,%r14
test   %rbx,%rbx
lea    0xebe(%rip),%r15
cmovne %rbx,%r15
xor    %eax,%eax
call   1050 <geteuid@plt>
test   %rax,%rax
lea    0xeb0(%rip),%rax
lea    0xea3(%rip),%rcx
cmove  %rax,%rcx
lea    0x2ec3(%rip),%rbx
lea    0xe9d(%rip),%rdx
mov    %rbx,%rdi
mov    $0x7f,%esi
mov    %r14,%r8
mov    %r15,%r9
call   1040 <snprintf@plt>
jmp    11c0 <getdwd+0x80>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"DAEMONNAME\00"
@getdwd.workingpath = internal global [128 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [26 x i8] c"can't get env DAEMONNAME\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [6 x i8] c"/tmp/\00"
@.str.4 = [6 x i8] c"/run/\00"
@.str.5 = [8 x i8] c"%s%s/%s\00"
define i8* @getdwd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %4
store i8* null, i8** %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %26
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8** %3
br label %16
16: ; preds = %15, %12
%17 = call i64 (...) @geteuid()
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %5
br label %21
20: ; preds = %16
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8** %5
br label %21
21: ; preds = %20, %19
%22 = load i8*, i8** %5
%23 = load i8*, i8** %4
%24 = load i8*, i8** %3
%25 = call i32 @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @getdwd.workingpath, i64 0, i64 0), i32 127, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %22, i8* %23, i8* %24)
store i8* getelementptr inbounds ([128 x i8], [128 x i8]* @getdwd.workingpath, i64 0, i64 0), i8** %2
br label %26
26: ; preds = %21, %9
%27 = load i8*, i8** %2
ret i8* %27
}

LLVM IR (-O2):
@.str = [11 x i8] c"DAEMONNAME\00"
@getdwd.workingpath = internal global [128 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [26 x i8] c"can't get env DAEMONNAME\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [6 x i8] c"/tmp/\00"
@.str.4 = [6 x i8] c"/run/\00"
@.str.5 = [8 x i8] c"%s%s/%s\00"
define i8* @getdwd(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %14
7: ; preds = %1
%8 = icmp eq i8* %0, null
%9 = select i1 %8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* %0
%10 = tail call i64 (...) @geteuid()
%11 = icmp eq i64 %10, 0
%12 = select i1 %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%13 = tail call i32 @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @getdwd.workingpath, i64 0, i64 0), i32 127, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %12, i8* %2, i8* %9)
br label %14
14: ; preds = %7, %4
%15 = phi i8* [ getelementptr inbounds ([128 x i8], [128 x i8]* @getdwd.workingpath, i64 0, i64 0), %7 ], [ null, %4 ]
ret i8* %15
}