#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 stat {
  int /*<<< orphan*/ st_gid;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ downgrade();
scalar_t__ fchown(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ getgid();
int /*<<< orphan*/ upgrade();
scalar_t__ userhasgid(int /*<<< orphan*/);
bool fsteal(int fd) {
  bool ret;
  struct stat stat;
  uid_t uid;
  gid_t gid;
  if (fstat(fd, &stat) != 0) {
    return false;
  }
  uid = geteuid();
  if (userhasgid(stat.st_gid)) {
    gid = stat.st_gid;
  } else {
    gid = getgid();
  }
  assert(upgrade());
  ret = (fchown(fd, uid, gid) == 0);
  assert(downgrade());
  return ret;
}


x86 assembly (clang, -O0):
<fsteal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11ad <fsteal+0x2d>
movl   $0x0,-0x4(%rbp)
jmp    121d <fsteal+0x9d>
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
call   10a0 <userhasgid@plt>
cmp    $0x0,%rax
je     11d4 <fsteal+0x54>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11de <fsteal+0x5e>
mov    $0x0,%al
call   1060 <getgid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <upgrade@plt>
mov    %eax,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1080 <fchown@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1070 <downgrade@plt>
mov    %eax,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsteal>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1090 <fstat@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    11ef <fsteal+0x6f>
xor    %eax,%eax
call   1030 <geteuid@plt>
mov    %eax,%r14d
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   10a0 <userhasgid@plt>
test   %rax,%rax
jne    11be <fsteal+0x3e>
xor    %eax,%eax
call   1060 <getgid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <upgrade@plt>
mov    %eax,%edi
call   1040 <assert@plt>
mov    %ebx,%edi
mov    %r14d,%esi
mov    %ebp,%edx
call   1080 <fchown@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
xor    %eax,%eax
call   1070 <downgrade@plt>
mov    %eax,%edi
call   1040 <assert@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @fsteal(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i64 @fstat(i32 %8, %struct.stat* %5)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* %2
br label %35
12: ; preds = %1
%13 = call i32 (...) @geteuid()
store i32 %13, i32* %6
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @userhasgid(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i32, i32* %19
store i32 %20, i32* %7
br label %23
21: ; preds = %12
%22 = call i32 (...) @getgid()
store i32 %22, i32* %7
br label %23
23: ; preds = %21, %18
%24 = call i32 (...) @upgrade()
%25 = call i32 @assert(i32 %24)
%26 = load i32, i32* %3
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = call i64 @fchown(i32 %26, i32 %27, i32 %28)
%30 = icmp eq i64 %29, 0
%31 = zext i1 %30 to i32
store i32 %31, i32* %4
%32 = call i32 (...) @downgrade()
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %4
store i32 %34, i32* %2
br label %35
35: ; preds = %23, %11
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @fsteal(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @fstat(i32 %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %23
6: ; preds = %1
%7 = tail call i32 (...) @geteuid()
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i64 @userhasgid(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 (...) @getgid()
br label %14
14: ; preds = %6, %12
%15 = phi i32 [ %13, %12 ], [ %9, %6 ]
%16 = tail call i32 (...) @upgrade()
%17 = tail call i32 @assert(i32 %16)
%18 = tail call i64 @fchown(i32 %0, i32 %7, i32 %15)
%19 = icmp eq i64 %18, 0
%20 = zext i1 %19 to i32
%21 = tail call i32 (...) @downgrade()
%22 = tail call i32 @assert(i32 %21)
br label %23
23: ; preds = %1, %14
%24 = phi i32 [ %20, %14 ], [ 0, %1 ]
ret i32 %24
}