#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 stat {
  int st_mode;
  int st_uid;
  int st_gid;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int S_ISGID;
scalar_t__ S_ISLNK(int);
int S_ISUID;
int S_IWGRP;
int S_IWOTH;
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *, ...);
int testsafepath(const char *path, int owner, int group) {
  struct stat sbuf;
  int retval = 0;
  ;
  if (lstat(path, &sbuf) != 0) {
    return 1;
  };
  if (S_ISLNK(sbuf.st_mode)) {
    syslog(LOG_ERR, "path %s is a symlink", path);
    retval |= 1;
  }
  if (sbuf.st_mode & S_ISUID) {
    syslog(LOG_ERR, "path %s is setuid", path);
    retval |= 8;
  }
  if (sbuf.st_mode & S_ISGID) {
    syslog(LOG_ERR, "path %s is setgid", path);
    retval |= 16;
  }
  if (sbuf.st_mode & S_IWGRP) {
    syslog(LOG_ERR, "path %s is group writable", path);
    retval |= 2;
  }
  if (sbuf.st_mode & S_IWOTH) {
    syslog(LOG_ERR, "path %s is writable for others", path);
    retval |= 4;
  }
  if (sbuf.st_uid != owner) {
    syslog(LOG_ERR, "path %s is not owned by user %d", path, owner);
    retval |= 32;
  }
  if (sbuf.st_gid != group) {
    syslog(LOG_ERR, "path %s is not owned by group %d", path, group);
    retval |= 64;
  }
  return retval;
}


x86 assembly (clang, -O0):
<testsafepath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
je     116c <testsafepath+0x3c>
movl   $0x1,-0x4(%rbp)
jmp    12f2 <testsafepath+0x1c2>
mov    -0x28(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     11a2 <testsafepath+0x72>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe6e(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x2c(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2e3c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11db <testsafepath+0xab>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x2c(%rbp),%eax
or     $0x8,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2deb(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1214 <testsafepath+0xe4>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x2c(%rbp),%eax
or     $0x10,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2dc2(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     124d <testsafepath+0x11d>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x2c(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2d61(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1286 <testsafepath+0x156>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x2c(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     12b9 <testsafepath+0x189>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x2c(%rbp),%eax
or     $0x20,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     12ec <testsafepath+0x1bc>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
mov    -0x2c(%rbp),%eax
or     $0x40,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testsafepath>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%r12
mov    %rsp,%rsi
call   1040 <lstat@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    127b <testsafepath+0x14b>
mov    (%rsp),%ebx
mov    %ebx,%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
je     118b <testsafepath+0x5b>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
mov    $0x1,%ebp
jmp    118d <testsafepath+0x5d>
xor    %ebp,%ebp
mov    0x2e54(%rip),%rax
test   %ebx,(%rax)
je     11b5 <testsafepath+0x85>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
or     $0x8,%ebp
mov    0x2e14(%rip),%rax
test   %ebx,(%rax)
je     11dd <testsafepath+0xad>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
or     $0x10,%ebp
mov    0x2dfc(%rip),%rax
test   %ebx,(%rax)
je     1205 <testsafepath+0xd5>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
or     $0x2,%ebp
mov    0x2dac(%rip),%rax
test   %ebx,(%rax)
je     122d <testsafepath+0xfd>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <syslog@plt>
or     $0x4,%ebp
cmp    %r15d,0x4(%rsp)
je     1254 <testsafepath+0x124>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
xor    %eax,%eax
call   1050 <syslog@plt>
or     $0x20,%ebp
cmp    %r14d,0x8(%rsp)
je     127b <testsafepath+0x14b>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
mov    %r12,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <syslog@plt>
or     $0x40,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32 }
@LOG_ERR = global i32 0
@.str = [21 x i8] c"path %s is a symlink\00"
@S_ISUID = global i32 0
@.str.1 = [18 x i8] c"path %s is setuid\00"
@S_ISGID = global i32 0
@.str.2 = [18 x i8] c"path %s is setgid\00"
@S_IWGRP = global i32 0
@.str.3 = [26 x i8] c"path %s is group writable\00"
@S_IWOTH = global i32 0
@.str.4 = [31 x i8] c"path %s is writable for others\00"
@.str.5 = [32 x i8] c"path %s is not owned by user %d\00"
@.str.6 = [33 x i8] c"path %s is not owned by group %d\00"
define i32 @testsafepath(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = call i64 @lstat(i8* %10, %struct.stat* %8)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 1, i32* %4
br label %99
14: ; preds = %3
%15 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @S_ISLNK(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32, i32* @LOG_ERR
%21 = load i8*, i8** %5
%22 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* %9
%24 = or i32 %23, 1
store i32 %24, i32* %9
br label %25
25: ; preds = %19, %14
%26 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @S_ISUID
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %25
%32 = load i32, i32* @LOG_ERR
%33 = load i8*, i8** %5
%34 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %32, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = load i32, i32* %9
%36 = or i32 %35, 8
store i32 %36, i32* %9
br label %37
37: ; preds = %31, %25
%38 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load i32, i32* @S_ISGID
%41 = and i32 %39, %40
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %37
%44 = load i32, i32* @LOG_ERR
%45 = load i8*, i8** %5
%46 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %44, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = load i32, i32* %9
%48 = or i32 %47, 16
store i32 %48, i32* %9
br label %49
49: ; preds = %43, %37
%50 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%51 = load i32, i32* %50
%52 = load i32, i32* @S_IWGRP
%53 = and i32 %51, %52
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %49
%56 = load i32, i32* @LOG_ERR
%57 = load i8*, i8** %5
%58 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %56, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = load i32, i32* %9
%60 = or i32 %59, 2
store i32 %60, i32* %9
br label %61
61: ; preds = %55, %49
%62 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = load i32, i32* @S_IWOTH
%65 = and i32 %63, %64
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %61
%68 = load i32, i32* @LOG_ERR
%69 = load i8*, i8** %5
%70 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %68, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %69)
%71 = load i32, i32* %9
%72 = or i32 %71, 4
store i32 %72, i32* %9
br label %73
73: ; preds = %67, %61
%74 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%75 = load i32, i32* %74
%76 = load i32, i32* %6
%77 = icmp ne i32 %75, %76
br i1 %77, label %78, label %85
78: ; preds = %73
%79 = load i32, i32* @LOG_ERR
%80 = load i8*, i8** %5
%81 = load i32, i32* %6
%82 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %79, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %80, i32 %81)
%83 = load i32, i32* %9
%84 = or i32 %83, 32
store i32 %84, i32* %9
br label %85
85: ; preds = %78, %73
%86 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 2
%87 = load i32, i32* %86
%88 = load i32, i32* %7
%89 = icmp ne i32 %87, %88
br i1 %89, label %90, label %97
90: ; preds = %85
%91 = load i32, i32* @LOG_ERR
%92 = load i8*, i8** %5
%93 = load i32, i32* %7
%94 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %91, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %92, i32 %93)
%95 = load i32, i32* %9
%96 = or i32 %95, 64
store i32 %96, i32* %9
br label %97
97: ; preds = %90, %85
%98 = load i32, i32* %9
store i32 %98, i32* %4
br label %99
99: ; preds = %97, %13
%100 = load i32, i32* %4
ret i32 %100
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@LOG_ERR = global i32 0
@.str = [21 x i8] c"path %s is a symlink\00"
@S_ISUID = global i32 0
@.str.1 = [18 x i8] c"path %s is setuid\00"
@S_ISGID = global i32 0
@.str.2 = [18 x i8] c"path %s is setgid\00"
@S_IWGRP = global i32 0
@.str.3 = [26 x i8] c"path %s is group writable\00"
@S_IWOTH = global i32 0
@.str.4 = [31 x i8] c"path %s is writable for others\00"
@.str.5 = [32 x i8] c"path %s is not owned by user %d\00"
@.str.6 = [33 x i8] c"path %s is not owned by group %d\00"
define i32 @testsafepath(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @lstat(i8* %0, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %70
8: ; preds = %3
%9 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISLNK(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = load i32, i32* @LOG_ERR
%15 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %16
16: ; preds = %13, %8
%17 = phi i32 [ 1, %13 ], [ 0, %8 ]
%18 = load i32, i32* @S_ISUID
%19 = and i32 %18, %10
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = load i32, i32* @LOG_ERR
%23 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%24 = or i32 %17, 8
br label %25
25: ; preds = %21, %16
%26 = phi i32 [ %24, %21 ], [ %17, %16 ]
%27 = load i32, i32* @S_ISGID
%28 = and i32 %27, %10
%29 = icmp eq i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %25
%31 = load i32, i32* @LOG_ERR
%32 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %31, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%33 = or i32 %26, 16
br label %34
34: ; preds = %30, %25
%35 = phi i32 [ %33, %30 ], [ %26, %25 ]
%36 = load i32, i32* @S_IWGRP
%37 = and i32 %36, %10
%38 = icmp eq i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %34
%40 = load i32, i32* @LOG_ERR
%41 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%42 = or i32 %35, 2
br label %43
43: ; preds = %39, %34
%44 = phi i32 [ %42, %39 ], [ %35, %34 ]
%45 = load i32, i32* @S_IWOTH
%46 = and i32 %45, %10
%47 = icmp eq i32 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %43
%49 = load i32, i32* @LOG_ERR
%50 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %49, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%51 = or i32 %44, 4
br label %52
52: ; preds = %48, %43
%53 = phi i32 [ %51, %48 ], [ %44, %43 ]
%54 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%55 = load i32, i32* %54
%56 = icmp eq i32 %55, %1
br i1 %56, label %61, label %57
57: ; preds = %52
%58 = load i32, i32* @LOG_ERR
%59 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %58, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %1)
%60 = or i32 %53, 32
br label %61
61: ; preds = %57, %52
%62 = phi i32 [ %60, %57 ], [ %53, %52 ]
%63 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 2
%64 = load i32, i32* %63
%65 = icmp eq i32 %64, %2
br i1 %65, label %70, label %66
66: ; preds = %61
%67 = load i32, i32* @LOG_ERR
%68 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %67, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %0, i32 %2)
%69 = or i32 %62, 64
br label %70
70: ; preds = %61, %66, %3
%71 = phi i32 [ 1, %3 ], [ %69, %66 ], [ %62, %61 ]
ret i32 %71
}