#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 /*<<< orphan*/ uid_t;
struct portal_cred {
  int dummy;
};
typedef int /*<<< orphan*/ gid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ENAMETOOLONG;
int /*<<< orphan*/ LOG_WARNING;
int errno;
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fill_cmd(char **, char *, char *, int);
scalar_t__ getuid();
int lose_credentials(struct portal_cred *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
scalar_t__ setegid(int /*<<< orphan*/);
scalar_t__ seteuid(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int portal_wfilter(struct portal_cred *pcr, char *key, char **v, int *fdp) {
  char cmd[8192];
  char *path;
  FILE *fp;
  int error = 0;
  int cred_change_err = 0;
  cred_change_err = lose_credentials(pcr);
  if (cred_change_err != 0)
    return cred_change_err;
  path = key + (v[1] ? strlen(v[1]) : 0);
  fill_cmd(v + 2, path, cmd, 8192);
  if (strlen(cmd) >= 8192) {
    syslog(LOG_WARNING,
           "Warning:  potential overflow on string!  Length was %lu\n",
           (unsigned long)strlen(cmd));
    return ENAMETOOLONG;
  }
  fp = popen(cmd, "w");
  if (fp == NULL) {
    return errno;
  }
  if (getuid() == 0) {
    if ((seteuid((uid_t)0) < 0) || (setegid((gid_t)0) < 0)) {
      error = errno;
      syslog(LOG_WARNING, "setcred: %m");
      pclose(fp);
      fp = NULL;
    }
  }
  if (fp)
    fdp[0] = fileno(fp);
  return error;
}


x86 assembly (clang, -O0):
<portal_wfilter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2060,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2044(%rbp)
movl   $0x0,-0x2048(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <lose_credentials@plt>
mov    %eax,-0x2048(%rbp)
cmpl   $0x0,-0x2048(%rbp)
je     11f9 <portal_wfilter+0x59>
mov    -0x2048(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <portal_wfilter+0x205>
mov    -0x18(%rbp),%rax
mov    %rax,-0x2050(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     122b <portal_wfilter+0x8b>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2054(%rbp)
jmp    1238 <portal_wfilter+0x98>
xor    %eax,%eax
mov    %eax,-0x2054(%rbp)
jmp    1238 <portal_wfilter+0x98>
mov    -0x2050(%rbp),%rax
mov    -0x2054(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x2038(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x10,%rdi
mov    -0x2038(%rbp),%rsi
lea    -0x2030(%rbp),%rdx
mov    $0x2000,%ecx
call   10b0 <fill_cmd@plt>
lea    -0x2030(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x2000,%eax
jl     12cc <portal_wfilter+0x12c>
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2058(%rbp)
lea    -0x2030(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x2058(%rbp),%edi
movslq %eax,%rdx
lea    0xd4c(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <portal_wfilter+0x205>
lea    -0x2030(%rbp),%rdi
lea    0xd5f(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x2040(%rbp)
cmpq   $0x0,-0x2040(%rbp)
jne    1305 <portal_wfilter+0x165>
mov    0x2cdd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a5 <portal_wfilter+0x205>
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
jne    137a <portal_wfilter+0x1da>
xor    %edi,%edi
call   10c0 <seteuid@plt>
cmp    $0x0,%rax
jl     1338 <portal_wfilter+0x198>
xor    %edi,%edi
call   10a0 <setegid@plt>
cmp    $0x0,%rax
jge    1375 <portal_wfilter+0x1d5>
mov    0x2c99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2044(%rbp)
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
lea    0xce4(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    -0x2040(%rbp),%rdi
call   1050 <pclose@plt>
movq   $0x0,-0x2040(%rbp)
jmp    137a <portal_wfilter+0x1da>
cmpq   $0x0,-0x2040(%rbp)
je     139c <portal_wfilter+0x1fc>
mov    -0x2040(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2044(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<portal_wfilter>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
call   1060 <lose_credentials@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    12b1 <portal_wfilter+0x111>
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
je     11d7 <portal_wfilter+0x37>
call   1030 <strlen@plt>
cltq
jmp    11d9 <portal_wfilter+0x39>
xor    %eax,%eax
add    %rax,%r14
add    $0x10,%rbx
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    $0x2000,%ecx
call   10b0 <fill_cmd@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
cmp    $0x2000,%eax
jl     1234 <portal_wfilter+0x94>
mov    0x2ddc(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdx
lea    0xde0(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1080 <syslog@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%ebp
jmp    12b1 <portal_wfilter+0x111>
lea    0xdfe(%rip),%rsi
mov    %rsp,%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     1266 <portal_wfilter+0xc6>
mov    %rax,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     1271 <portal_wfilter+0xd1>
mov    %r14,%rdi
call   1070 <fileno@plt>
mov    %eax,(%r15)
jmp    12b1 <portal_wfilter+0x111>
mov    0x2d6b(%rip),%rax
mov    (%rax),%ebp
jmp    12b1 <portal_wfilter+0x111>
xor    %edi,%edi
call   10c0 <seteuid@plt>
test   %rax,%rax
js     1289 <portal_wfilter+0xe9>
xor    %edi,%edi
call   10a0 <setegid@plt>
test   %rax,%rax
jns    1259 <portal_wfilter+0xb9>
mov    0x2d48(%rip),%rax
mov    (%rax),%ebp
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xd99(%rip),%rsi
xor    %eax,%eax
call   1080 <syslog@plt>
mov    %r14,%rdi
call   1050 <pclose@plt>
mov    %ebp,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.portal_cred = type { i32 }
@LOG_WARNING = global i32 0
@.str = [57 x i8] c"Warning: potential overflow on string! Length was %lu\0A\00"
@ENAMETOOLONG = global i32 0
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"setcred: %m\00"
define i32 @portal_wfilter(%struct.portal_cred* %0, i8* %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.portal_cred*
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca [8192 x i8]
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
store %struct.portal_cred* %0, %struct.portal_cred** %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
store i32 0, i32* %13
store i32 0, i32* %14
%15 = load %struct.portal_cred*, %struct.portal_cred** %6
%16 = call i32 @lose_credentials(%struct.portal_cred* %15)
store i32 %16, i32* %14
%17 = load i32, i32* %14
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = load i32, i32* %14
store i32 %20, i32* %5
br label %85
21: ; preds = %4
%22 = load i8*, i8** %7
%23 = load i8**, i8*** %8
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i8**, i8*** %8
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @strlen(i8* %30)
br label %33
32: ; preds = %21
br label %33
33: ; preds = %32, %27
%34 = phi i32 [ %31, %27 ], [ 0, %32 ]
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %22, i64 %35
store i8* %36, i8** %11
%37 = load i8**, i8*** %8
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %11
%40 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%41 = call i32 @fill_cmd(i8** %38, i8* %39, i8* %40, i32 8192)
%42 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = icmp sge i32 %43, 8192
br i1 %44, label %45, label %52
45: ; preds = %33
%46 = load i32, i32* @LOG_WARNING
%47 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = sext i32 %48 to i64
%50 = call i32 (i32, i8*, ...) @syslog(i32 %46, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i64 %49)
%51 = load i32, i32* @ENAMETOOLONG
store i32 %51, i32* %5
br label %85
52: ; preds = %33
%53 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%54 = call i32* @popen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %54, i32** %12
%55 = load i32*, i32** %12
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = load i32, i32* @errno
store i32 %58, i32* %5
br label %85
59: ; preds = %52
%60 = call i64 (...) @getuid()
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %75
62: ; preds = %59
%63 = call i64 @seteuid(i32 0)
%64 = icmp slt i64 %63, 0
br i1 %64, label %68, label %65
65: ; preds = %62
%66 = call i64 @setegid(i32 0)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %74
68: ; preds = %65, %62
%69 = load i32, i32* @errno
store i32 %69, i32* %13
%70 = load i32, i32* @LOG_WARNING
%71 = call i32 (i32, i8*, ...) @syslog(i32 %70, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%72 = load i32*, i32** %12
%73 = call i32 @pclose(i32* %72)
store i32* null, i32** %12
br label %74
74: ; preds = %68, %65
br label %75
75: ; preds = %74, %59
%76 = load i32*, i32** %12
%77 = icmp ne i32* %76, null
br i1 %77, label %78, label %83
78: ; preds = %75
%79 = load i32*, i32** %12
%80 = call i32 @fileno(i32* %79)
%81 = load i32*, i32** %9
%82 = getelementptr inbounds i32, i32* %81, i64 0
store i32 %80, i32* %82
br label %83
83: ; preds = %78, %75
%84 = load i32, i32* %13
store i32 %84, i32* %5
br label %85
85: ; preds = %83, %57, %45, %19
%86 = load i32, i32* %5
ret i32 %86
}

LLVM IR (-O2):
%struct.portal_cred = type { i32 }
@LOG_WARNING = global i32 0
@.str = [57 x i8] c"Warning: potential overflow on string! Length was %lu\0A\00"
@ENAMETOOLONG = global i32 0
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"setcred: %m\00"
define i32 @portal_wfilter(%struct.portal_cred* %0, i8* %1, i8** %2, i32* nocapture writeonly %3) {
%5 = alloca [8192 x i8]
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%7 = tail call i32 @lose_credentials(%struct.portal_cred* %0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %50
9: ; preds = %4
%10 = getelementptr inbounds i8*, i8** %2, i64 1
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = tail call i32 @strlen(i8* %11)
%15 = sext i32 %14 to i64
br label %16
16: ; preds = %9, %13
%17 = phi i64 [ %15, %13 ], [ 0, %9 ]
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = getelementptr inbounds i8*, i8** %2, i64 2
%20 = call i32 @fill_cmd(i8** %19, i8* %18, i8* %6, i32 8192)
%21 = call i32 @strlen(i8* %6)
%22 = icmp sgt i32 %21, 8191
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = load i32, i32* @LOG_WARNING
%25 = call i32 @strlen(i8* %6)
%26 = sext i32 %25 to i64
%27 = call i32 (i32, i8*, ...) @syslog(i32 %24, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i64 %26)
%28 = load i32, i32* @ENAMETOOLONG
br label %50
29: ; preds = %16
%30 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i32, i32* @errno
br label %50
34: ; preds = %29
%35 = call i64 (...) @getuid()
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %48
37: ; preds = %34
%38 = call i64 @seteuid(i32 0)
%39 = icmp slt i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = call i64 @setegid(i32 0)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %37, %40
%44 = load i32, i32* @errno
%45 = load i32, i32* @LOG_WARNING
%46 = call i32 (i32, i8*, ...) @syslog(i32 %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @pclose(i32* %30)
br label %50
48: ; preds = %40, %34
%49 = call i32 @fileno(i32* %30)
store i32 %49, i32* %3
br label %50
50: ; preds = %43, %48, %4, %32, %23
%51 = phi i32 [ %28, %23 ], [ %33, %32 ], [ %7, %4 ], [ 0, %48 ], [ %44, %43 ]
ret i32 %51
}