#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 uid_t;

/* Variables and functions */
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int geteuid();
int getuid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  uid_t uid;
  uid_t euid;
  char *user_name;
  uid = getuid();
  euid = geteuid();
  if ((uid != 500) && (uid != 0)) {
    fprintf(stderr, "DAVRAP: uid not httpd uid (%i), but %i.\n", 500, uid);
  } else if (euid != 0) {
    fprintf(stderr, "DAVRAP: euid not 0, but %i.\n", euid);
  } else if (setgid(0) != 0) {
    fprintf(stderr, "DAVRAP: Can't setgid()\n");
    perror("setgid");
  } else if (setuid(0) != 0) {
    fprintf(stderr, "DAVRAP: Can't setuid()\n");
  } else {
    fprintf(stderr, "DAVRAP: OK, exe %s\n", "/bin/mount");
    execv("/bin/mount", argv);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1f4,-0x14(%rbp)
je     11d5 <main+0x65>
cmpl   $0x0,-0x14(%rbp)
je     11d5 <main+0x65>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%ecx
lea    0xe3c(%rip),%rsi
mov    $0x1f4,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12a1 <main+0x131>
cmpl   $0x0,-0x18(%rbp)
je     11fe <main+0x8e>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    129c <main+0x12c>
xor    %edi,%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
je     1237 <main+0xc7>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
jmp    1297 <main+0x127>
xor    %edi,%edi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     1264 <main+0xf4>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1292 <main+0x122>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
lea    0xe16(%rip),%rdx
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe04(%rip),%rdi
call   1090 <execv@plt>
jmp    1297 <main+0x127>
jmp    129c <main+0x12c>
jmp    12a1 <main+0x131>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <geteuid@plt>
test   %ebp,%ebp
je     1192 <main+0x22>
cmp    $0x1f4,%ebp
jne    11b1 <main+0x41>
test   %eax,%eax
je     11d1 <main+0x61>
mov    0x2e43(%rip),%rcx
mov    (%rcx),%edi
lea    0xe83(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1225 <main+0xb5>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
mov    $0x1f4,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1225 <main+0xb5>
xor    %edi,%edi
call   1060 <setgid@plt>
test   %rax,%rax
jne    122e <main+0xbe>
xor    %edi,%edi
call   1080 <setuid@plt>
mov    0x2df5(%rip),%rcx
mov    (%rcx),%edi
test   %rax,%rax
je     1202 <main+0x92>
lea    0xe6c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1225 <main+0xb5>
lea    0xe74(%rip),%rsi
lea    0xe81(%rip),%rbp
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <execv@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
jmp    1225 <main+0xb5>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"DAVRAP: uid not httpd uid (%i), but %i.\0A\00"
@.str.1 = [29 x i8] c"DAVRAP: euid not 0, but %i.\0A\00"
@.str.2 = [24 x i8] c"DAVRAP: Can't setgid()\0A\00"
@.str.3 = [7 x i8] c"setgid\00"
@.str.4 = [24 x i8] c"DAVRAP: Can't setuid()\0A\00"
@.str.5 = [20 x i8] c"DAVRAP: OK, exe %s\0A\00"
@.str.6 = [11 x i8] c"/bin/mount\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @getuid()
store i32 %9, i32* %6
%10 = call i32 (...) @geteuid()
store i32 %10, i32* %7
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 500
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* %6
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 500, i32 %18)
br label %48
20: ; preds = %13, %2
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i32, i32* %7
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %47
27: ; preds = %20
%28 = call i64 @setgid(i32 0)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %46
34: ; preds = %27
%35 = call i64 @setuid(i32 0)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %45
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%43 = load i8**, i8*** %5
%44 = call i32 @execv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8** %43)
br label %45
45: ; preds = %40, %37
br label %46
46: ; preds = %45, %30
br label %47
47: ; preds = %46, %23
br label %48
48: ; preds = %47, %16
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"DAVRAP: uid not httpd uid (%i), but %i.\0A\00"
@.str.1 = [29 x i8] c"DAVRAP: euid not 0, but %i.\0A\00"
@.str.2 = [24 x i8] c"DAVRAP: Can't setgid()\0A\00"
@.str.3 = [7 x i8] c"setgid\00"
@.str.4 = [24 x i8] c"DAVRAP: Can't setuid()\0A\00"
@.str.5 = [20 x i8] c"DAVRAP: OK, exe %s\0A\00"
@.str.6 = [11 x i8] c"/bin/mount\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 (...) @getuid()
%4 = tail call i32 (...) @geteuid()
switch i32 %3, label %5 [
i32 500, label %8
i32 0, label %8
]
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 500, i32 %3)
br label %29
8: ; preds = %2, %2
%9 = icmp eq i32 %4, 0
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %29
13: ; preds = %8
%14 = tail call i64 @setgid(i32 0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %29
20: ; preds = %13
%21 = tail call i64 @setuid(i32 0)
%22 = icmp eq i64 %21, 0
%23 = load i32, i32* @stderr
br i1 %22, label %26, label %24
24: ; preds = %20
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %29
26: ; preds = %20
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%28 = tail call i32 @execv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8** %1)
br label %29
29: ; preds = %10, %24, %26, %16, %5
ret i32 0
}