#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;
  int /*<<< orphan*/ pw_gid;
  int /*<<< orphan*/ *pw_dir;
};
typedef int pid_t;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ addProc(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *getProc(int /*<<< orphan*/);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ initgroups(char *, int /*<<< orphan*/);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/ *);
scalar_t__ strlen(int /*<<< orphan*/ *);
pid_t beginRunsv(char *sUsername, gid_t idGroup) {
  char *buf;
  struct passwd *usr = getpwnam(sUsername);
  if (usr == NULL) {
    printf("Cannot load user record for user: %s\n", sUsername);
    return -1;
  }
  if (getProc(usr->pw_uid) != NULL) {
    printf("Already running a service for: %s\n", sUsername);
    return -1;
  }
  if (usr->pw_dir == NULL) {
    printf("No homedir is set for user: %s\n", sUsername);
    return -1;
  }
  buf = malloc(strlen(usr->pw_dir) + 6);
  strcpy(buf, usr->pw_dir);
  strcat(buf, "/"
              ".sv");
  if (access(buf, F_OK) != 0) {
    printf("Cannot find directory: %s\n", buf);
    free(buf);
    return -1;
  }
  pid_t p = fork();
  if (p == 0) {
    initgroups(sUsername, usr->pw_gid);
    setuid(usr->pw_uid);
    execlp("runsvdir", "runsvdir", buf,
           "log: "
           "..................................................................."
           "..................................................................."
           "..................................................................."
           "..................................................................."
           "..................................................................."
           "............................................................",
           NULL);
    perror("Error executing runsv: ");
    exit(2);
  } else if (p == -1) {
    perror("Error forking: ");
    return -1;
  }
  addProc(p, usr->pw_uid);
  return p;
}


x86 assembly (clang, -O0):
<beginRunsv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1243 <beginRunsv+0x43>
mov    -0x10(%rbp),%rsi
lea    0xdce(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <beginRunsv+0x1bd>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1090 <getProc@plt>
cmp    $0x0,%rax
je     1274 <beginRunsv+0x74>
mov    -0x10(%rbp),%rsi
lea    0xdc3(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <beginRunsv+0x1bd>
mov    -0x28(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    129f <beginRunsv+0x9f>
mov    -0x10(%rbp),%rsi
lea    0xdbb(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <beginRunsv+0x1bd>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x6,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xd91(%rip),%rsi
call   10d0 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%rax
je     131e <beginRunsv+0x11e>
mov    -0x20(%rbp),%rsi
lea    0xd6a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <beginRunsv+0x1bd>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1382 <beginRunsv+0x182>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
call   1110 <initgroups@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   10f0 <setuid@plt>
mov    -0x20(%rbp),%rdx
lea    0xd31(%rip),%rsi
lea    0xd33(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1100 <execlp@plt>
lea    0xeb0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    13a4 <beginRunsv+0x1a4>
lea    0xea8(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <beginRunsv+0x1bd>
jmp    13a9 <beginRunsv+0x1a9>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   10c0 <addProc@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<beginRunsv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1070 <getpwnam@plt>
test   %rax,%rax
je     122c <beginRunsv+0x2c>
mov    %rax,%rbx
mov    (%rax),%edi
call   1090 <getProc@plt>
test   %rax,%rax
je     124d <beginRunsv+0x4d>
lea    0xdfc(%rip),%rdi
jmp    1233 <beginRunsv+0x33>
lea    0xdcd(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rbp
test   %rbp,%rbp
je     12b3 <beginRunsv+0xb3>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x6(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
lea    0xded(%rip),%rsi
mov    %r15,%rdi
call   10d0 <strcat@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10a0 <access@plt>
test   %rax,%rax
je     12bf <beginRunsv+0xbf>
lea    0xdcd(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    123b <beginRunsv+0x3b>
lea    0xd8f(%rip),%rdi
jmp    1233 <beginRunsv+0x33>
xor    %eax,%eax
call   1120 <fork@plt>
cmp    $0xffffffff,%eax
je     12df <beginRunsv+0xdf>
mov    %eax,%ebp
test   %eax,%eax
je     12f0 <beginRunsv+0xf0>
mov    (%rbx),%esi
mov    %ebp,%edi
call   10c0 <addProc@plt>
jmp    1240 <beginRunsv+0x40>
lea    0xf55(%rip),%rdi
call   10b0 <perror@plt>
jmp    123b <beginRunsv+0x3b>
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1110 <initgroups@plt>
mov    (%rbx),%edi
call   10f0 <setuid@plt>
lea    0xd80(%rip),%rdi
lea    0xd82(%rip),%rcx
mov    %rdi,%rsi
mov    %r15,%rdx
xor    %r8d,%r8d
call   1100 <execlp@plt>
lea    0xefe(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32, i32* }
@.str = [38 x i8] c"Cannot load user record for user: %s\0A\00"
@.str.1 = [35 x i8] c"Already running a service for: %s\0A\00"
@.str.2 = [32 x i8] c"No homedir is set for user: %s\0A\00"
@.str.3 = [5 x i8] c"/.sv\00"
@F_OK = global i32 0
@.str.4 = [27 x i8] c"Cannot find directory: %s\0A\00"
@.str.5 = [9 x i8] c"runsvdir\00"
@.str.6 = [401 x i8] c"log: ...........................................................................................................................................................................................................................................................................................................................................................................................................\00"
@.str.7 = [24 x i8] c"Error executing runsv: \00"
@.str.8 = [16 x i8] c"Error forking: \00"
define i32 @beginRunsv(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.passwd*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call %struct.passwd* @getpwnam(i8* %9)
store %struct.passwd* %10, %struct.passwd** %7
%11 = load %struct.passwd*, %struct.passwd** %7
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 -1, i32* %3
br label %87
16: ; preds = %2
%17 = load %struct.passwd*, %struct.passwd** %7
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32* @getProc(i32 %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 -1, i32* %3
br label %87
25: ; preds = %16
%26 = load %struct.passwd*, %struct.passwd** %7
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 2
%28 = load i32*, i32** %27
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i8*, i8** %4
%32 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store i32 -1, i32* %3
br label %87
33: ; preds = %25
%34 = load %struct.passwd*, %struct.passwd** %7
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i32 0, i32 2
%36 = load i32*, i32** %35
%37 = call i64 @strlen(i32* %36)
%38 = add nsw i64 %37, 6
%39 = call i8* @malloc(i64 %38)
store i8* %39, i8** %6
%40 = load i8*, i8** %6
%41 = load %struct.passwd*, %struct.passwd** %7
%42 = getelementptr inbounds %struct.passwd, %struct.passwd* %41, i32 0, i32 2
%43 = load i32*, i32** %42
%44 = call i32 @strcpy(i8* %40, i32* %43)
%45 = load i8*, i8** %6
%46 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%47 = load i8*, i8** %6
%48 = load i32, i32* @F_OK
%49 = call i64 @access(i8* %47, i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %33
%52 = load i8*, i8** %6
%53 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %52)
%54 = load i8*, i8** %6
%55 = call i32 @free(i8* %54)
store i32 -1, i32* %3
br label %87
56: ; preds = %33
%57 = call i32 (...) @fork()
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %74
60: ; preds = %56
%61 = load i8*, i8** %4
%62 = load %struct.passwd*, %struct.passwd** %7
%63 = getelementptr inbounds %struct.passwd, %struct.passwd* %62, i32 0, i32 1
%64 = load i32, i32* %63
%65 = call i32 @initgroups(i8* %61, i32 %64)
%66 = load %struct.passwd*, %struct.passwd** %7
%67 = getelementptr inbounds %struct.passwd, %struct.passwd* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @setuid(i32 %68)
%70 = load i8*, i8** %6
%71 = call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %70, i8* getelementptr inbounds ([401 x i8], [401 x i8]* @.str.6, i64 0, i64 0), i32* null)
%72 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%73 = call i32 @exit(i32 2)
unreachable
74: ; preds = %56
%75 = load i32, i32* %8
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %79
77: ; preds = %74
%78 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %3
br label %87
79: ; preds = %74
br label %80
80: ; preds = %79
%81 = load i32, i32* %8
%82 = load %struct.passwd*, %struct.passwd** %7
%83 = getelementptr inbounds %struct.passwd, %struct.passwd* %82, i32 0, i32 0
%84 = load i32, i32* %83
%85 = call i32 @addProc(i32 %81, i32 %84)
%86 = load i32, i32* %8
store i32 %86, i32* %3
br label %87
87: ; preds = %80, %77, %51, %30, %22, %13
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32, i32* }
@.str = [38 x i8] c"Cannot load user record for user: %s\0A\00"
@.str.1 = [35 x i8] c"Already running a service for: %s\0A\00"
@.str.2 = [32 x i8] c"No homedir is set for user: %s\0A\00"
@.str.3 = [5 x i8] c"/.sv\00"
@F_OK = global i32 0
@.str.4 = [27 x i8] c"Cannot find directory: %s\0A\00"
@.str.5 = [9 x i8] c"runsvdir\00"
@.str.6 = [401 x i8] c"log: ...........................................................................................................................................................................................................................................................................................................................................................................................................\00"
@.str.7 = [24 x i8] c"Error executing runsv: \00"
@.str.8 = [16 x i8] c"Error forking: \00"
define i32 @beginRunsv(i8* %0, i32 %1) {
%3 = tail call %struct.passwd* @getpwnam(i8* %0)
%4 = icmp eq %struct.passwd* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %48
7: ; preds = %2
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32* @getProc(i32 %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %7
%13 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %48
14: ; preds = %7
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 2
%16 = load i32*, i32** %15
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %48
20: ; preds = %14
%21 = tail call i64 @strlen(i32* %16)
%22 = add nsw i64 %21, 6
%23 = tail call i8* @malloc(i64 %22)
%24 = tail call i32 @strcpy(i8* %23, i32* %16)
%25 = tail call i32 @strcat(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @F_OK
%27 = tail call i64 @access(i8* %23, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %20
%30 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %23)
%31 = tail call i32 @free(i8* %23)
br label %48
32: ; preds = %20
%33 = tail call i32 (...) @fork()
switch i32 %33, label %45 [
i32 0, label %34
i32 -1, label %43
]
34: ; preds = %32
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 1
%36 = load i32, i32* %35
%37 = tail call i32 @initgroups(i8* %0, i32 %36)
%38 = load i32, i32* %8
%39 = tail call i32 @setuid(i32 %38)
%40 = tail call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %23, i8* getelementptr inbounds ([401 x i8], [401 x i8]* @.str.6, i64 0, i64 0), i32* null)
%41 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%42 = tail call i32 @exit(i32 2)
unreachable
43: ; preds = %32
%44 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
br label %48
45: ; preds = %32
%46 = load i32, i32* %8
%47 = tail call i32 @addProc(i32 %33, i32 %46)
br label %48
48: ; preds = %43, %45, %29, %18, %12, %5
%49 = phi i32 [ -1, %5 ], [ -1, %12 ], [ -1, %18 ], [ -1, %29 ], [ -1, %43 ], [ %33, %45 ]
ret i32 %49
}