#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 passwd {
  int /*<<< orphan*/ pw_uid;
};
struct group {
  int /*<<< orphan*/ gr_gid;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sld_daemon_start();
int /*<<< orphan*/ sld_daemon_write_pid_file(char *);
char *strerror(int /*<<< orphan*/);
void sld_daemon_daemonize(char *pid_file_name, char *runas_user,
                          char *runas_group) {
  struct passwd *pwent;
  struct group *grpent;
  pid_t pid = fork();
  if (pid >= 0) {
    if (pid == 0) {
      sld_daemon_write_pid_file(pid_file_name);
      if (runas_user != NULL) {
        if ((pwent = getpwnam(runas_user)) == NULL) {
          printf("Unable to get user entry %s for setuid(): %s\n", runas_user,
                 strerror(errno));
          exit(EXIT_FAILURE);
        } else {
          setuid(pwent->pw_uid);
        }
      }
      if (runas_group != NULL) {
        if ((grpent = getgrnam(runas_group)) == NULL) {
          printf("Unable to get group entry %s for setgid(): %s\n", runas_group,
                 strerror(errno));
          exit(EXIT_FAILURE);
        } else {
          setgid(grpent->gr_gid);
        }
      }
      sld_daemon_start();
    }
  } else {
    perror("fork() failed:");
  }
  return;
}


x86 assembly (clang, -O0):
<sld_daemon_daemonize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jl     12cc <sld_daemon_daemonize+0x11c>
cmpq   $0x0,-0x30(%rbp)
jne    12c7 <sld_daemon_daemonize+0x117>
mov    -0x8(%rbp),%rdi
call   1030 <sld_daemon_write_pid_file@plt>
cmpq   $0x0,-0x10(%rbp)
je     1257 <sld_daemon_daemonize+0xa7>
mov    -0x10(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1247 <sld_daemon_daemonize+0x97>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xdcc(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   10b0 <setuid@plt>
jmp    1257 <sld_daemon_daemonize+0xa7>
cmpq   $0x0,-0x18(%rbp)
je     12c0 <sld_daemon_daemonize+0x110>
mov    -0x18(%rbp),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12b0 <sld_daemon_daemonize+0x100>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xd91(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1070 <setgid@plt>
jmp    12c0 <sld_daemon_daemonize+0x110>
mov    $0x0,%al
call   1090 <sld_daemon_start@plt>
jmp    12d8 <sld_daemon_daemonize+0x128>
lea    0xd8a(%rip),%rdi
call   1080 <perror@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sld_daemon_daemonize>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1218 <sld_daemon_daemonize+0x68>
je     11d2 <sld_daemon_daemonize+0x22>
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1030 <sld_daemon_write_pid_file@plt>
test   %r15,%r15
je     11f3 <sld_daemon_daemonize+0x43>
mov    %r15,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     1229 <sld_daemon_daemonize+0x79>
mov    (%rax),%edi
call   10b0 <setuid@plt>
test   %r14,%r14
je     120c <sld_daemon_daemonize+0x5c>
mov    %r14,%rdi
call   1060 <getgrnam@plt>
test   %rax,%rax
je     1243 <sld_daemon_daemonize+0x93>
mov    (%rax),%edi
call   1070 <setgid@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <sld_daemon_start@plt>
lea    0xe3e(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <perror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdc2(%rip),%rdi
mov    %r15,%rsi
jmp    125b <sld_daemon_daemonize+0xab>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdd6(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@.str = [46 x i8] c"Unable to get user entry %s for setuid(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [47 x i8] c"Unable to get group entry %s for setgid(): %s\0A\00"
@.str.2 = [15 x i8] c"fork() failed:\00"
define void @sld_daemon_daemonize(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.passwd*
%8 = alloca %struct.group*
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = call i64 (...) @fork()
store i64 %10, i64* %9
%11 = load i64, i64* %9
%12 = icmp sge i64 %11, 0
br i1 %12, label %13, label %61
13: ; preds = %3
%14 = load i64, i64* %9
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %60
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = call i32 @sld_daemon_write_pid_file(i8* %17)
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %38
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = call %struct.passwd* @getpwnam(i8* %22)
store %struct.passwd* %23, %struct.passwd** %7
%24 = icmp eq %struct.passwd* %23, null
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %21
%33 = load %struct.passwd*, %struct.passwd** %7
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @setuid(i32 %35)
br label %37
37: ; preds = %32
br label %38
38: ; preds = %37, %16
%39 = load i8*, i8** %6
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %58
41: ; preds = %38
%42 = load i8*, i8** %6
%43 = call %struct.group* @getgrnam(i8* %42)
store %struct.group* %43, %struct.group** %8
%44 = icmp eq %struct.group* %43, null
br i1 %44, label %45, label %52
45: ; preds = %41
%46 = load i8*, i8** %6
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %46, i8* %48)
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %41
%53 = load %struct.group*, %struct.group** %8
%54 = getelementptr inbounds %struct.group, %struct.group* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @setgid(i32 %55)
br label %57
57: ; preds = %52
br label %58
58: ; preds = %57, %38
%59 = call i32 (...) @sld_daemon_start()
br label %60
60: ; preds = %58, %13
br label %63
61: ; preds = %3
%62 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %61, %60
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@.str = [46 x i8] c"Unable to get user entry %s for setuid(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [47 x i8] c"Unable to get group entry %s for setgid(): %s\0A\00"
@.str.2 = [15 x i8] c"fork() failed:\00"
define void @sld_daemon_daemonize(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp sgt i64 %4, -1
br i1 %5, label %6, label %41
6: ; preds = %3
%7 = icmp eq i64 %4, 0
br i1 %7, label %8, label %43
8: ; preds = %6
%9 = tail call i32 @sld_daemon_write_pid_file(i8* %0)
%10 = icmp eq i8* %1, null
br i1 %10, label %24, label %11
11: ; preds = %8
%12 = tail call %struct.passwd* @getpwnam(i8* %1)
%13 = icmp eq %struct.passwd* %12, null
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %11
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @setuid(i32 %22)
br label %24
24: ; preds = %20, %8
%25 = icmp eq i8* %2, null
br i1 %25, label %39, label %26
26: ; preds = %24
%27 = tail call %struct.group* @getgrnam(i8* %2)
%28 = icmp eq %struct.group* %27, null
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %26
%36 = getelementptr inbounds %struct.group, %struct.group* %27, i64 0, i32 0
%37 = load i32, i32* %36
%38 = tail call i32 @setgid(i32 %37)
br label %39
39: ; preds = %35, %24
%40 = tail call i32 (...) @sld_daemon_start()
br label %43
41: ; preds = %3
%42 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %6, %39, %41
ret void
}