#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 uid_t;
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ check_fsid(int, int);
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ msg(char *, int, int);
int /*<<< orphan*/ msg_exit(char *);
int setfsgid(int);
int setfsuid(int);
void change_fsid(uid_t olduid, gid_t oldgid, uid_t uid, gid_t gid) {
  int u;
  int g;
  if (uid != olduid) {
    u = setfsuid(uid);
    if (u == -1)
      err("setfsuid");
    else if (u != olduid)
      msg("setfsuid returned %d (wanted %d)", u, olduid);
  }
  if (gid != oldgid) {
    g = setfsgid(gid);
    if (g == -1)
      err("setfsgid");
    else if (g != oldgid)
      msg("setfsgid returned %d (wanted %d)", g, oldgid);
  }
  if (!check_fsid(uid, gid))
    msg_exit("setfsuid/setfsgid failed");
  msg("fsid changed to %d:%d", uid, gid);
}


x86 assembly (clang, -O0):
<change_fsid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     11ce <change_fsid+0x6e>
mov    -0xc(%rbp),%edi
call   1030 <setfsuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a6 <change_fsid+0x46>
lea    0xe64(%rip),%rdi
call   1050 <err@plt>
jmp    11c9 <change_fsid+0x69>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     11c4 <change_fsid+0x64>
mov    -0x14(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0xe4a(%rip),%rdi
call   1080 <msg@plt>
jmp    11c9 <change_fsid+0x69>
jmp    11ce <change_fsid+0x6e>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1228 <change_fsid+0xc8>
mov    -0x10(%rbp),%edi
call   1060 <setfsgid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1200 <change_fsid+0xa0>
lea    0xe34(%rip),%rdi
call   1050 <err@plt>
jmp    1223 <change_fsid+0xc3>
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     121e <change_fsid+0xbe>
mov    -0x18(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe1a(%rip),%rdi
call   1080 <msg@plt>
jmp    1223 <change_fsid+0xc3>
jmp    1228 <change_fsid+0xc8>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1070 <check_fsid@plt>
cmp    $0x0,%eax
jne    1248 <change_fsid+0xe8>
lea    0xe11(%rip),%rdi
call   1040 <msg_exit@plt>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe18(%rip),%rdi
call   1080 <msg@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_fsid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esi,%r14d
cmp    %edi,%edx
je     1195 <change_fsid+0x35>
mov    %edi,%ebx
mov    %ebp,%edi
call   1030 <setfsuid@plt>
cmp    $0xffffffff,%eax
je     11bf <change_fsid+0x5f>
cmp    %ebx,%eax
je     1195 <change_fsid+0x35>
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
call   1080 <msg@plt>
cmp    %r14d,%r15d
je     11de <change_fsid+0x7e>
mov    %r15d,%edi
call   1060 <setfsgid@plt>
cmp    $0xffffffff,%eax
je     11d2 <change_fsid+0x72>
cmp    %r14d,%eax
je     11de <change_fsid+0x7e>
lea    0xe80(%rip),%rdi
mov    %eax,%esi
mov    %r14d,%edx
call   1080 <msg@plt>
jmp    11de <change_fsid+0x7e>
lea    0xe3a(%rip),%rdi
call   1050 <err@plt>
cmp    %r14d,%r15d
jne    119a <change_fsid+0x3a>
jmp    11de <change_fsid+0x7e>
lea    0xe51(%rip),%rdi
call   1050 <err@plt>
mov    %ebp,%edi
mov    %r15d,%esi
call   1070 <check_fsid@plt>
test   %eax,%eax
jne    11f8 <change_fsid+0x98>
lea    0xe61(%rip),%rdi
call   1040 <msg_exit@plt>
lea    0xe6e(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <msg@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"setfsuid\00"
@.str.1 = [33 x i8] c"setfsuid returned %d (wanted %d)\00"
@.str.2 = [9 x i8] c"setfsgid\00"
@.str.3 = [33 x i8] c"setfsgid returned %d (wanted %d)\00"
@.str.4 = [25 x i8] c"setfsuid/setfsgid failed\00"
@.str.5 = [22 x i8] c"fsid changed to %d:%d\00"
define void @change_fsid(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %31
14: ; preds = %4
%15 = load i32, i32* %7
%16 = call i32 @setfsuid(i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @err(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %30
21: ; preds = %14
%22 = load i32, i32* %9
%23 = load i32, i32* %5
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* %9
%27 = load i32, i32* %5
%28 = call i32 @msg(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %25, %21
br label %30
30: ; preds = %29, %19
br label %31
31: ; preds = %30, %4
%32 = load i32, i32* %8
%33 = load i32, i32* %6
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %52
35: ; preds = %31
%36 = load i32, i32* %8
%37 = call i32 @setfsgid(i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @err(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %51
42: ; preds = %35
%43 = load i32, i32* %10
%44 = load i32, i32* %6
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load i32, i32* %10
%48 = load i32, i32* %6
%49 = call i32 @msg(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %47, i32 %48)
br label %50
50: ; preds = %46, %42
br label %51
51: ; preds = %50, %40
br label %52
52: ; preds = %51, %31
%53 = load i32, i32* %7
%54 = load i32, i32* %8
%55 = call i32 @check_fsid(i32 %53, i32 %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %52
%58 = call i32 @msg_exit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %57, %52
%60 = load i32, i32* %7
%61 = load i32, i32* %8
%62 = call i32 @msg(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %60, i32 %61)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"setfsuid\00"
@.str.1 = [33 x i8] c"setfsuid returned %d (wanted %d)\00"
@.str.2 = [9 x i8] c"setfsgid\00"
@.str.3 = [33 x i8] c"setfsgid returned %d (wanted %d)\00"
@.str.4 = [25 x i8] c"setfsuid/setfsgid failed\00"
@.str.5 = [22 x i8] c"fsid changed to %d:%d\00"
define void @change_fsid(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %2, %0
br i1 %5, label %15, label %6
6: ; preds = %4
%7 = tail call i32 @setfsuid(i32 %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @err(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %15
11: ; preds = %6
%12 = icmp eq i32 %7, %0
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @msg(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %0)
br label %15
15: ; preds = %9, %13, %11, %4
%16 = icmp eq i32 %3, %1
br i1 %16, label %26, label %17
17: ; preds = %15
%18 = tail call i32 @setfsgid(i32 %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @err(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %26
22: ; preds = %17
%23 = icmp eq i32 %18, %1
br i1 %23, label %26, label %24
24: ; preds = %22
%25 = tail call i32 @msg(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %18, i32 %1)
br label %26
26: ; preds = %20, %24, %22, %15
%27 = tail call i32 @check_fsid(i32 %2, i32 %3)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @msg_exit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = tail call i32 @msg(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %2, i32 %3)
ret void
}