#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_uid;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int O_NOFOLLOW;
int O_NONBLOCK;
int O_RDONLY;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchown(int, int, int);
scalar_t__ fstat(int, struct stat *);
int open(char const *, int, int /*<<< orphan*/);
void safe_chown(const char *full, int to_user_id, int to_group_id,
                int from_user_id) {
  int fd = open(full, O_RDONLY | O_NOFOLLOW | O_NONBLOCK, 0);
  if (fd < 0)
    return;
  struct stat stb;
  if (fstat(fd, &stb) < 0) {
    close(fd);
    return;
  }
  if (S_ISDIR(stb.st_mode)) {
    if (stb.st_uid == from_user_id) {
      fchown(fd, to_user_id, to_group_id);
    }
  } else {
    if (stb.st_uid == from_user_id) {
      fchown(fd, to_user_id, to_group_id);
    }
  }
  close(fd);
}


x86 assembly (clang, -O0):
<safe_chown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e7f(%rip),%rax
or     (%rax),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    119d <safe_chown+0x4d>
jmp    1218 <safe_chown+0xc8>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jge    11c0 <safe_chown+0x70>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    1218 <safe_chown+0xc8>
mov    -0x1c(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11f1 <safe_chown+0xa1>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11ec <safe_chown+0x9c>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <fchown@plt>
jmp    1210 <safe_chown+0xc0>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    120b <safe_chown+0xbb>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <fchown@plt>
jmp    1210 <safe_chown+0xc0>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_chown>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7a(%rip),%rbp
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
or     0x0(%rbp),%eax
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %esi,%r15d
mov    0x2e67(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11bb <safe_chown+0x6b>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     11b4 <safe_chown+0x64>
mov    0x4(%rsp),%edi
call   1030 <S_ISDIR@plt>
cmp    %ebp,(%rsp)
jne    11b4 <safe_chown+0x64>
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r14d,%edx
call   1060 <fchown@plt>
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_NOFOLLOW = global i32 0
@O_NONBLOCK = global i32 0
define void @safe_chown(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = load i32, i32* @O_NOFOLLOW
%14 = or i32 %12, %13
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %11, i32 %16, i32 0)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
br label %58
21: ; preds = %4
%22 = load i32, i32* %9
%23 = call i64 @fstat(i32 %22, %struct.stat* %10)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* %9
%27 = call i32 @close(i32 %26)
br label %58
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i64 @S_ISDIR(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* %8
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %43
38: ; preds = %33
%39 = load i32, i32* %9
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = call i32 @fchown(i32 %39, i32 %40, i32 %41)
br label %43
43: ; preds = %38, %33
br label %55
44: ; preds = %28
%45 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%46 = load i32, i32* %45
%47 = load i32, i32* %8
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = load i32, i32* %9
%51 = load i32, i32* %6
%52 = load i32, i32* %7
%53 = call i32 @fchown(i32 %50, i32 %51, i32 %52)
br label %54
54: ; preds = %49, %44
br label %55
55: ; preds = %54, %43
%56 = load i32, i32* %9
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55, %25, %20
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_NOFOLLOW = global i32 0
@O_NONBLOCK = global i32 0
define void @safe_chown(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.stat
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_NOFOLLOW
%8 = or i32 %7, %6
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %28, label %13
13: ; preds = %4
%14 = bitcast %struct.stat* %5 to i8*
%15 = call i64 @fstat(i32 %11, %struct.stat* %5)
%16 = icmp slt i64 %15, 0
br i1 %16, label %26, label %17
17: ; preds = %13
%18 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%19 = load i32, i32* %18
%20 = tail call i64 @S_ISDIR(i32 %19)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, %3
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = tail call i32 @fchown(i32 %11, i32 %1, i32 %2)
br label %26
26: ; preds = %17, %24, %13
%27 = tail call i32 @close(i32 %11)
br label %28
28: ; preds = %4, %26
ret void
}