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

/* Variables and functions */
int O_NOFOLLOW;
int O_NONBLOCK;
int O_RDONLY;
scalar_t__ S_ISDIR(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchmod(int, 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);
      fchmod(fd, (stb.st_mode & 0777) | 0700);
    }
  } 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    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ad <safe_chown+0x4d>
jmp    123f <safe_chown+0xdf>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    11d0 <safe_chown+0x70>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    123f <safe_chown+0xdf>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1218 <safe_chown+0xb8>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1213 <safe_chown+0xb3>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1070 <fchown@plt>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
and    $0x1ff,%esi
or     $0x1c0,%esi
call   1050 <fchmod@plt>
jmp    1237 <safe_chown+0xd7>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1232 <safe_chown+0xd2>
mov    -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1070 <fchown@plt>
jmp    1237 <safe_chown+0xd7>
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   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e65(%rip),%rbp
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
or     0x0(%rbp),%eax
mov    %ecx,%r12d
mov    %edx,%r14d
mov    %esi,%r15d
mov    0x2e51(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     1207 <safe_chown+0xa7>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     1200 <safe_chown+0xa0>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1030 <S_ISDIR@plt>
mov    0xc(%rsp),%ecx
test   %rax,%rax
je     11ee <safe_chown+0x8e>
cmp    %r12d,%ecx
jne    1200 <safe_chown+0xa0>
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r14d,%edx
call   1070 <fchown@plt>
or     $0x1c0,%ebp
and    $0x1ff,%ebp
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <fchmod@plt>
jmp    1200 <safe_chown+0xa0>
cmp    %r12d,%ecx
jne    1200 <safe_chown+0xa0>
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r14d,%edx
call   1070 <fchown@plt>
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
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 %64
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 %64
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @S_ISDIR(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %28
%34 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%35 = load i32, i32* %34
%36 = load i32, i32* %8
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %49
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)
%43 = load i32, i32* %9
%44 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%45 = load i32, i32* %44
%46 = and i32 %45, 511
%47 = or i32 %46, 448
%48 = call i32 @fchmod(i32 %43, i32 %47)
br label %49
49: ; preds = %38, %33
br label %61
50: ; preds = %28
%51 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%52 = load i32, i32* %51
%53 = load i32, i32* %8
%54 = icmp eq i32 %52, %53
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = load i32, i32* %9
%57 = load i32, i32* %6
%58 = load i32, i32* %7
%59 = call i32 @fchown(i32 %56, i32 %57, i32 %58)
br label %60
60: ; preds = %55, %50
br label %61
61: ; preds = %60, %49
%62 = load i32, i32* %9
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %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 %36, 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 %34, label %17
17: ; preds = %13
%18 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i64 @S_ISDIR(i32 %19)
%21 = icmp eq i64 %20, 0
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, %3
br i1 %21, label %31, label %25
25: ; preds = %17
br i1 %24, label %26, label %34
26: ; preds = %25
%27 = tail call i32 @fchown(i32 %11, i32 %1, i32 %2)
%28 = and i32 %19, 63
%29 = or i32 %28, 448
%30 = tail call i32 @fchmod(i32 %11, i32 %29)
br label %34
31: ; preds = %17
br i1 %24, label %32, label %34
32: ; preds = %31
%33 = tail call i32 @fchown(i32 %11, i32 %1, i32 %2)
br label %34
34: ; preds = %26, %25, %32, %31, %13
%35 = tail call i32 @close(i32 %11)
br label %36
36: ; preds = %4, %34
ret void
}