#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 */
typedef int pid_t;

/* Variables and functions */
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WNOHANG;
int /*<<< orphan*/ WTERMSIG(int);
int WUNTRACED;
int decreasepriv(int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ errx(int, char *, scalar_t__, ...);
int execve(char const *const, char *const *, int /*<<< orphan*/);
int fork();
scalar_t__ geteuid();
int /*<<< orphan*/ strsignal(int /*<<< orphan*/);
int waitpid(int, int *, int);
int /*<<< orphan*/ warn(char *, int, int, int);
int runit(const char *const argv[], int dropprivs) {
  pid_t child;
  int ret;
  child = fork();
  if (child == 0) {
    if (dropprivs && (geteuid() == 0)) {
      ret = decreasepriv(0);
      if (ret) {
        err(1, "failed to decrease privileges");
      }
    }
    ret = execve(argv[0], (char *const *)argv, 0);
    if (ret) {
      err(1, "execve(%s) failed", argv[0]);
    }
  } else {
    int loop_counter = 0;
    do {
      int status = 0;
      int wait_opts = WUNTRACED;
      pid_t rch = waitpid(child, &status, wait_opts);
      if (rch == -1) {
        err(1, "waitpid(%d, &%d, %d) failed (code %d) after %d attempt(s)",
            child, status, wait_opts, errno, loop_counter);
      } else if ((rch == 0) && (wait_opts & WNOHANG)) {
        warn("waitpid(%d, &%d, %d) child failed before reporting back", child,
             status, wait_opts);
      }
      if (WIFSTOPPED(status)) {
        continue;
      }
      if (WIFSIGNALED(status)) {
        errx(1, "child exited on signal %d (i.e. \"%s\").", WTERMSIG(status),
             strsignal(WTERMSIG(status)));
      }
      if (WIFEXITED(status)) {
        if (WEXITSTATUS(status) == 0) {
          break;
        } else {
          errx(1, "child exited with status %d", WEXITSTATUS(status));
        }
      }
      loop_counter++;
    } while (1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<runit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1288 <runit+0xa8>
cmpl   $0x0,-0xc(%rbp)
je     124a <runit+0x6a>
mov    $0x0,%al
call   1060 <geteuid@plt>
cmp    $0x0,%rax
jne    124a <runit+0x6a>
xor    %edi,%edi
call   1030 <decreasepriv@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1245 <runit+0x65>
mov    $0x1,%edi
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
jmp    124a <runit+0x6a>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1070 <execve@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1283 <runit+0xa3>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    $0x1,%edi
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
jmp    13f1 <runit+0x211>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%edx
lea    -0x1c(%rbp),%rsi
call   10e0 <waitpid@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12f0 <runit+0x110>
mov    -0x10(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    0x2d01(%rip),%rax
mov    (%rax),%r9d
mov    -0x18(%rbp),%eax
mov    $0x1,%edi
lea    0xd4f(%rip),%rsi
mov    %eax,(%rsp)
mov    $0x0,%al
call   10a0 <err@plt>
jmp    1329 <runit+0x149>
cmpl   $0x0,-0x24(%rbp)
jne    1324 <runit+0x144>
mov    -0x20(%rbp),%eax
mov    0x2ce4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1324 <runit+0x144>
mov    -0x10(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xd4b(%rip),%rdi
call   1040 <warn@plt>
jmp    1329 <runit+0x149>
mov    -0x1c(%rbp),%edi
call   1090 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1340 <runit+0x160>
jmp    13dd <runit+0x1fd>
mov    -0x1c(%rbp),%edi
call   1080 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1388 <runit+0x1a8>
mov    -0x1c(%rbp),%edi
call   10b0 <WTERMSIG@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   10b0 <WTERMSIG@plt>
mov    %eax,%edi
call   10c0 <strsignal@plt>
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xd21(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
mov    -0x1c(%rbp),%edi
call   10f0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     13d4 <runit+0x1f4>
mov    -0x1c(%rbp),%edi
call   10d0 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    13b1 <runit+0x1d1>
jmp    13ec <runit+0x20c>
mov    -0x1c(%rbp),%edi
call   10d0 <WEXITSTATUS@plt>
mov    %rax,%rdx
mov    $0x1,%edi
lea    0xd01(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
jmp    13d4 <runit+0x1f4>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    128f <runit+0xaf>
jmp    13ec <runit+0x20c>
jmp    13f1 <runit+0x211>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     133a <runit+0x15a>
mov    %eax,%ebx
mov    0x2dcd(%rip),%r13
lea    0x14(%rsp),%r14
xor    %r12d,%r12d
jmp    122e <runit+0x4e>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x14(%rsp),%edi
call   1090 <WIFSTOPPED@plt>
test   %rax,%rax
je     1280 <runit+0xa0>
movl   $0x0,0x14(%rsp)
mov    0x0(%r13),%r15d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   10e0 <waitpid@plt>
test   %eax,%eax
je     1310 <runit+0x130>
cmp    $0xffffffff,%eax
jne    1220 <runit+0x40>
mov    0x14(%rsp),%ecx
mov    0x2d71(%rip),%rax
mov    (%rax),%r9d
mov    %r12d,(%rsp)
mov    $0x1,%edi
lea    0xdbe(%rip),%rsi
mov    %ebx,%edx
mov    %r15d,%r8d
xor    %eax,%eax
call   10a0 <err@plt>
jmp    1220 <runit+0x40>
mov    0x14(%rsp),%edi
call   1080 <WIFSIGNALED@plt>
test   %rax,%rax
je     12c2 <runit+0xe2>
mov    0x14(%rsp),%edi
call   10b0 <WTERMSIG@plt>
movslq %eax,%rbp
mov    0x14(%rsp),%edi
call   10b0 <WTERMSIG@plt>
mov    %eax,%edi
call   10c0 <strsignal@plt>
mov    $0x1,%edi
lea    0xdec(%rip),%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <errx@plt>
mov    0x14(%rsp),%edi
call   10f0 <WIFEXITED@plt>
test   %rax,%rax
je     1301 <runit+0x121>
mov    0x14(%rsp),%edi
call   10d0 <WEXITSTATUS@plt>
test   %rax,%rax
je     138f <runit+0x1af>
mov    0x14(%rsp),%edi
call   10d0 <WEXITSTATUS@plt>
mov    $0x1,%edi
lea    0xdd2(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <errx@plt>
add    $0x1,%r12d
jmp    122e <runit+0x4e>
nopw   0x0(%rax,%rax,1)
mov    0x2cd1(%rip),%rax
test   %r15d,(%rax)
je     1220 <runit+0x40>
mov    0x14(%rsp),%edx
lea    0xd3f(%rip),%rdi
mov    %ebx,%esi
mov    %r15d,%ecx
call   1040 <warn@plt>
jmp    1220 <runit+0x40>
test   %ebp,%ebp
je     1368 <runit+0x188>
xor    %eax,%eax
call   1060 <geteuid@plt>
test   %rax,%rax
jne    1368 <runit+0x188>
xor    %edi,%edi
call   1030 <decreasepriv@plt>
test   %eax,%eax
je     1368 <runit+0x188>
lea    0xca4(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <err@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <execve@plt>
test   %eax,%eax
je     138f <runit+0x1af>
mov    (%r14),%rdx
lea    0xc9b(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <err@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"failed to decrease privileges\00"
@.str.1 = [18 x i8] c"execve(%s) failed\00"
@WUNTRACED = global i32 0
@.str.2 = [58 x i8] c"waitpid(%d, &%d, %d) failed (code %d) after %d attempt(s)\00"
@errno = global i32 0
@WNOHANG = global i32 0
@.str.3 = [56 x i8] c"waitpid(%d, &%d, %d) child failed before reporting back\00"
@.str.4 = [39 x i8] c"child exited on signal %d (i.e. \22%s\22).\00"
@.str.5 = [28 x i8] c"child exited with status %d\00"
define i32 @runit(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%11 = call i32 (...) @fork()
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %41
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = call i64 (...) @geteuid()
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = call i32 @decreasepriv(i32 0)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
br label %27
27: ; preds = %26, %17, %14
%28 = load i8**, i8*** %3
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = load i8**, i8*** %3
%32 = call i32 @execve(i8* %30, i8** %31, i32 0)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %27
%36 = load i8**, i8*** %3
%37 = getelementptr inbounds i8*, i8** %36, i64 0
%38 = load i8*, i8** %37
%39 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %35, %27
br label %106
41: ; preds = %2
store i32 0, i32* %7
br label %42
42: ; preds = %104, %41
store i32 0, i32* %8
%43 = load i32, i32* @WUNTRACED
store i32 %43, i32* %9
%44 = load i32, i32* %5
%45 = load i32, i32* %9
%46 = call i32 @waitpid(i32 %44, i32* %8, i32 %45)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %56
49: ; preds = %42
%50 = load i32, i32* %5
%51 = load i32, i32* %8
%52 = load i32, i32* %9
%53 = load i32, i32* @errno
%54 = load i32, i32* %7
%55 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i32 %50, i32 %51, i32 %52, i32 %53, i32 %54)
br label %70
56: ; preds = %42
%57 = load i32, i32* %10
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %56
%60 = load i32, i32* %9
%61 = load i32, i32* @WNOHANG
%62 = and i32 %60, %61
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %59
%65 = load i32, i32* %5
%66 = load i32, i32* %8
%67 = load i32, i32* %9
%68 = call i32 @warn(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %65, i32 %66, i32 %67)
br label %69
69: ; preds = %64, %59, %56
br label %70
70: ; preds = %69, %49
%71 = load i32, i32* %8
%72 = call i64 @WIFSTOPPED(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %70
br label %104
75: ; preds = %70
%76 = load i32, i32* %8
%77 = call i64 @WIFSIGNALED(i32 %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %87
79: ; preds = %75
%80 = load i32, i32* %8
%81 = call i32 @WTERMSIG(i32 %80)
%82 = sext i32 %81 to i64
%83 = load i32, i32* %8
%84 = call i32 @WTERMSIG(i32 %83)
%85 = call i32 @strsignal(i32 %84)
%86 = call i32 (i32, i8*, i64, ...) @errx(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %82, i32 %85)
br label %87
87: ; preds = %79, %75
%88 = load i32, i32* %8
%89 = call i64 @WIFEXITED(i32 %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %101
91: ; preds = %87
%92 = load i32, i32* %8
%93 = call i64 @WEXITSTATUS(i32 %92)
%94 = icmp eq i64 %93, 0
br i1 %94, label %95, label %96
95: ; preds = %91
br label %105
96: ; preds = %91
%97 = load i32, i32* %8
%98 = call i64 @WEXITSTATUS(i32 %97)
%99 = call i32 (i32, i8*, i64, ...) @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %98)
br label %100
100: ; preds = %96
br label %101
101: ; preds = %100, %87
%102 = load i32, i32* %7
%103 = add nsw i32 %102, 1
store i32 %103, i32* %7
br label %104
104: ; preds = %101, %74
br i1 true, label %42, label %105
105: ; preds = %104, %95
br label %106
106: ; preds = %105, %40
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"failed to decrease privileges\00"
@.str.1 = [18 x i8] c"execve(%s) failed\00"
@WUNTRACED = global i32 0
@.str.2 = [58 x i8] c"waitpid(%d, &%d, %d) failed (code %d) after %d attempt(s)\00"
@errno = global i32 0
@WNOHANG = global i32 0
@.str.3 = [56 x i8] c"waitpid(%d, &%d, %d) child failed before reporting back\00"
@.str.4 = [39 x i8] c"child exited on signal %d (i.e. \22%s\22).\00"
@.str.5 = [28 x i8] c"child exited with status %d\00"
define i32 @runit(i8** %0, i32 %1) {
%3 = alloca i32
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = bitcast i32* %3 to i8*
br label %25
8: ; preds = %2
%9 = icmp eq i32 %1, 0
br i1 %9, label %18, label %10
10: ; preds = %8
%11 = tail call i64 (...) @geteuid()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = tail call i32 @decreasepriv(i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %13, %16, %10, %8
%19 = load i8*, i8** %0
%20 = tail call i32 @execve(i8* %19, i8** %0, i32 0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %73, label %22
22: ; preds = %18
%23 = load i8*, i8** %0
%24 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %73
25: ; preds = %6, %71
%26 = phi i32 [ %72, %71 ], [ 0, %6 ]
store i32 0, i32* %3
%27 = load i32, i32* @WUNTRACED
%28 = call i32 @waitpid(i32 %4, i32* %3, i32 %27)
switch i32 %28, label %40 [
i32 -1, label %29
i32 0, label %33
]
29: ; preds = %25
%30 = load i32, i32* %3
%31 = load i32, i32* @errno
%32 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 %30, i32 %27, i32 %31, i32 %26)
br label %40
33: ; preds = %25
%34 = load i32, i32* @WNOHANG
%35 = and i32 %34, %27
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = load i32, i32* %3
%39 = call i32 @warn(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %4, i32 %38, i32 %27)
br label %40
40: ; preds = %25, %33, %37, %29
%41 = load i32, i32* %3
%42 = call i64 @WIFSTOPPED(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %71
44: ; preds = %40
%45 = load i32, i32* %3
%46 = call i64 @WIFSIGNALED(i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %56, label %48
48: ; preds = %44
%49 = load i32, i32* %3
%50 = call i32 @WTERMSIG(i32 %49)
%51 = sext i32 %50 to i64
%52 = load i32, i32* %3
%53 = call i32 @WTERMSIG(i32 %52)
%54 = call i32 @strsignal(i32 %53)
%55 = call i32 (i32, i8*, i64, ...) @errx(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %51, i32 %54)
br label %56
56: ; preds = %48, %44
%57 = load i32, i32* %3
%58 = call i64 @WIFEXITED(i32 %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %69, label %60
60: ; preds = %56
%61 = load i32, i32* %3
%62 = call i64 @WEXITSTATUS(i32 %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %60
br label %73
65: ; preds = %60
%66 = load i32, i32* %3
%67 = call i64 @WEXITSTATUS(i32 %66)
%68 = call i32 (i32, i8*, i64, ...) @errx(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %67)
br label %69
69: ; preds = %65, %56
%70 = add nsw i32 %26, 1
br label %71
71: ; preds = %40, %69
%72 = phi i32 [ %70, %69 ], [ %26, %40 ]
br label %25
73: ; preds = %64, %18, %22
ret i32 0
}