#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 {
  scalar_t__ pw_uid;
  int /*<<< orphan*/ pw_gid;
  int /*<<< orphan*/ pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ execv(char *, char **);
char *getenv(char *);
struct passwd *getpwnam(char *);
scalar_t__ initgroups(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(scalar_t__);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strtok(char *, char *);
int main(int argc, char *argv[]) {
  struct passwd *pw = NULL;
  char *remote_user = getenv("WEBDAV_USER");
  if (remote_user == NULL)
    remote_user = getenv("REDIRECT_WEBDAV_USER");
  if (remote_user == NULL)
    remote_user = getenv("REMOTE_USER");
  if (remote_user == NULL)
    remote_user = getenv("REDIRECT_REMOTE_USER");
  char *user = NULL;
  if (remote_user != NULL) {
    char buf[2000];
    snprintf(buf, 2000, "%s", remote_user);
    user = strtok(buf, "@");
    pw = getpwnam(user);
  }
  if ((pw != NULL) && (pw->pw_uid != 0)) {
    if (initgroups(pw->pw_name, pw->pw_gid) == 0 && setgid(pw->pw_gid) == 0 &&
        setuid(pw->pw_uid) == 0)
      execv("webdav.pl", argv);
    else
      printf("Status: 500 Internal Sever Error");
  } else {
    printf("Status: 404 Not Found\r\n");
    printf("Content-Type: text/plain\r\n\r\n");
    printf("404 Not Found - your wrapper\n");
    printf("remote_user: %s\n", remote_user);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x800,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dc <main+0x4c>
lea    0xe2d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f7 <main+0x67>
lea    0xe30(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1212 <main+0x82>
lea    0xe0c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1265 <main+0xd5>
lea    -0x800(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x7d0,%esi
lea    0xdee(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x800(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ef <main+0x15f>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     12ef <main+0x15f>
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
call   10a0 <initgroups@plt>
cmp    $0x0,%rax
jne    12dc <main+0x14c>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
jne    12dc <main+0x14c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1090 <setuid@plt>
cmp    $0x0,%rax
jne    12dc <main+0x14c>
mov    -0x10(%rbp),%rsi
lea    0xd5d(%rip),%rdi
call   10b0 <execv@plt>
jmp    12ea <main+0x15a>
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132b <main+0x19b>
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x800,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x7d0,%rsp
mov    %rsi,%r14
lea    0xe53(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11ff <main+0x5f>
lea    0xe36(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11ff <main+0x5f>
lea    0xe40(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11ff <main+0x5f>
lea    0xe23(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
test   %rax,%rax
je     1278 <main+0xd8>
lea    0xe24(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x7d0,%esi
mov    %r15,%rcx
call   1060 <snprintf@plt>
lea    0xe0d(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strtok@plt>
mov    %rax,%rdi
call   1070 <getpwnam@plt>
test   %rax,%rax
je     127b <main+0xdb>
mov    %rax,%rbx
cmpq   $0x0,(%rax)
je     127b <main+0xdb>
mov    0x8(%rbx),%esi
mov    0xc(%rbx),%edi
call   10b0 <initgroups@plt>
test   %rax,%rax
jne    1268 <main+0xc8>
mov    0x8(%rbx),%edi
call   1080 <setgid@plt>
test   %rax,%rax
jne    1268 <main+0xc8>
mov    (%rbx),%rdi
call   10a0 <setuid@plt>
test   %rax,%rax
je     12bf <main+0x11f>
lea    0xdca(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12b0 <main+0x110>
xor    %r15d,%r15d
lea    0xde9(%rip),%rdi
call   1040 <puts@plt>
lea    0xdf4(%rip),%rdi
call   1040 <puts@plt>
lea    0xe04(%rip),%rdi
call   1040 <puts@plt>
lea    0xdb4(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x7d0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd69(%rip),%rdi
mov    %r14,%rsi
call   10c0 <execv@plt>
jmp    12b0 <main+0x110>

LLVM IR (-O0):
%struct.passwd = type { i64, i32, i32 }
@.str = [12 x i8] c"WEBDAV_USER\00"
@.str.1 = [21 x i8] c"REDIRECT_WEBDAV_USER\00"
@.str.2 = [12 x i8] c"REMOTE_USER\00"
@.str.3 = [21 x i8] c"REDIRECT_REMOTE_USER\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [2 x i8] c"@\00"
@.str.6 = [10 x i8] c"webdav.pl\00"
@.str.7 = [33 x i8] c"Status: 500 Internal Sever Error\00"
@.str.8 = [24 x i8] c"Status: 404 Not Found\0D\0A\00"
@.str.9 = [29 x i8] c"Content-Type: text/plain\0D\0A\0D\0A\00"
@.str.10 = [30 x i8] c"404 Not Found - your wrapper\0A\00"
@.str.11 = [17 x i8] c"remote_user: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [2000 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.passwd* null, %struct.passwd** %6
%10 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i8* %14, i8** %7
br label %15
15: ; preds = %13, %2
%16 = load i8*, i8** %7
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i8* %19, i8** %7
br label %20
20: ; preds = %18, %15
%21 = load i8*, i8** %7
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i8* %24, i8** %7
br label %25
25: ; preds = %23, %20
store i8* null, i8** %8
%26 = load i8*, i8** %7
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = getelementptr inbounds [2000 x i8], [2000 x i8]* %9, i64 0, i64 0
%30 = load i8*, i8** %7
%31 = call i32 @snprintf(i8* %29, i32 2000, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds [2000 x i8], [2000 x i8]* %9, i64 0, i64 0
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = call %struct.passwd* @getpwnam(i8* %34)
store %struct.passwd* %35, %struct.passwd** %6
br label %36
36: ; preds = %28, %25
%37 = load %struct.passwd*, %struct.passwd** %6
%38 = icmp ne %struct.passwd* %37, null
br i1 %38, label %39, label %71
39: ; preds = %36
%40 = load %struct.passwd*, %struct.passwd** %6
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %71
44: ; preds = %39
%45 = load %struct.passwd*, %struct.passwd** %6
%46 = getelementptr inbounds %struct.passwd, %struct.passwd* %45, i32 0, i32 2
%47 = load i32, i32* %46
%48 = load %struct.passwd*, %struct.passwd** %6
%49 = getelementptr inbounds %struct.passwd, %struct.passwd* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i64 @initgroups(i32 %47, i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %68
53: ; preds = %44
%54 = load %struct.passwd*, %struct.passwd** %6
%55 = getelementptr inbounds %struct.passwd, %struct.passwd* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = call i64 @setgid(i32 %56)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %68
59: ; preds = %53
%60 = load %struct.passwd*, %struct.passwd** %6
%61 = getelementptr inbounds %struct.passwd, %struct.passwd* %60, i32 0, i32 0
%62 = load i64, i64* %61
%63 = call i64 @setuid(i64 %62)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %59
%66 = load i8**, i8*** %5
%67 = call i32 @execv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8** %66)
br label %70
68: ; preds = %59, %53, %44
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %70
70: ; preds = %68, %65
br label %77
71: ; preds = %39, %36
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.10, i64 0, i64 0))
%75 = load i8*, i8** %7
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.11, i64 0, i64 0), i8* %75)
br label %77
77: ; preds = %71, %70
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.passwd = type { i64, i32, i32 }
@.str = [12 x i8] c"WEBDAV_USER\00"
@.str.1 = [21 x i8] c"REDIRECT_WEBDAV_USER\00"
@.str.2 = [12 x i8] c"REMOTE_USER\00"
@.str.3 = [21 x i8] c"REDIRECT_REMOTE_USER\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [2 x i8] c"@\00"
@.str.6 = [10 x i8] c"webdav.pl\00"
@.str.7 = [33 x i8] c"Status: 500 Internal Sever Error\00"
@.str.11 = [17 x i8] c"remote_user: %s\0A\00"
@str = [23 x i8] c"Status: 404 Not Found\0D\00"
@str.12 = [28 x i8] c"Content-Type: text/plain\0D\0A\0D\00"
@str.13 = [29 x i8] c"404 Not Found - your wrapper\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [2000 x i8]
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %15
6: ; preds = %2
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %45, label %15
15: ; preds = %2, %6, %9, %12
%16 = phi i8* [ %13, %12 ], [ %10, %9 ], [ %7, %6 ], [ %4, %2 ]
%17 = getelementptr inbounds [2000 x i8], [2000 x i8]* %3, i64 0, i64 0
%18 = call i32 @snprintf(i8* %17, i32 2000, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%19 = call i8* @strtok(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%20 = call %struct.passwd* @getpwnam(i8* %19)
%21 = icmp eq %struct.passwd* %20, null
br i1 %21, label %45, label %22
22: ; preds = %15
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i64 0, i32 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %45, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i64 0, i32 2
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i64 0, i32 1
%30 = load i32, i32* %29
%31 = call i64 @initgroups(i32 %28, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %26
%34 = load i32, i32* %29
%35 = call i64 @setgid(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i64, i64* %23
%39 = call i64 @setuid(i64 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @execv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8** %1)
br label %51
43: ; preds = %37, %33, %26
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %51
45: ; preds = %12, %22, %15
%46 = phi i8* [ %16, %22 ], [ %16, %15 ], [ null, %12 ]
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.12, i64 0, i64 0))
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.13, i64 0, i64 0))
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.11, i64 0, i64 0), i8* %46)
br label %51
51: ; preds = %41, %43, %45
ret i32 0
}