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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int chown(char *, int, int);
int close(int);
int errno;
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int setegid(int);
int setgroups(int, int *);
int setuid(int);
int unlink(char *);
int test_chown(void) {
  int fd, rc;
  gid_t gidset[1];
  char *tmpname = "test-chmod.dat";
  fd = open(tmpname, O_CREAT | O_RDWR, S_IRWXU);
  if (fd < 0) {
    printf("Fail to create file %s, errno = %d at line %d!\n", tmpname, errno,
           267);
    return (1);
  }
  rc = close(fd);
  if (rc < 0) {
    printf("Fail to close file %s, errno = %d at line %d!\n", tmpname, errno,
           272);
    return (1);
  }
  rc = chown(tmpname, 65534, 65533);
  if (rc < 0) {
    printf("Fail to chown file %s, errno = %d at line %d!\n", tmpname, errno,
           277);
    return (1);
  }
  gidset[0] = 65531;
  rc = setgroups(1, gidset);
  if (rc < 0) {
    printf("Fail to set groups, errno = %d at line %d!\n", errno, 283);
    return (1);
  }
  rc = setegid(gidset[0]);
  if (rc < 0) {
    printf("Fail to set effective groups, errno = %d at line %d!\n", errno,
           288);
    return (1);
  }
  rc = setuid(65532);
  if (rc < 0) {
    printf("Fail to set user ID, errno = %d at line %d!\n", errno, 293);
    return (1);
  }
  rc = chown(tmpname, 65535, 65535);
  if (rc < 0) {
    printf("Fail to chown file %s, errno = %d at line %d!\n", tmpname, errno,
           298);
    return (1);
  }
  rc = unlink(tmpname);
  if (rc) {
    printf("Fail to remove file %s, errno = %d at line %d!\n", tmpname, errno,
           303);
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_chown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f0 <test_chown+0x70>
mov    -0x18(%rbp),%rsi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    0xe37(%rip),%rdi
mov    $0x10b,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <test_chown+0x255>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1231 <test_chown+0xb1>
mov    -0x18(%rbp),%rsi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    0xe26(%rip),%rdi
mov    $0x110,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <test_chown+0x255>
mov    -0x18(%rbp),%rdi
mov    $0xfffe,%esi
mov    $0xfffd,%edx
call   1070 <chown@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    127d <test_chown+0xfd>
mov    -0x18(%rbp),%rsi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
lea    0xe09(%rip),%rdi
mov    $0x115,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <test_chown+0x255>
movl   $0xfffb,-0x10(%rbp)
lea    -0x10(%rbp),%rsi
mov    $0x1,%edi
call   1060 <setgroups@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12c7 <test_chown+0x147>
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
lea    0xdee(%rip),%rdi
mov    $0x11b,%edx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <test_chown+0x255>
mov    -0x10(%rbp),%edi
call   10a0 <setegid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1304 <test_chown+0x184>
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
lea    0xddd(%rip),%rdi
mov    $0x120,%edx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <test_chown+0x255>
mov    $0xfffc,%edi
call   1090 <setuid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1343 <test_chown+0x1c3>
mov    0x2ca6(%rip),%rax
mov    (%rax),%esi
lea    0xdd4(%rip),%rdi
mov    $0x125,%edx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <test_chown+0x255>
mov    -0x18(%rbp),%rdi
mov    $0xffff,%edx
mov    %edx,%esi
call   1070 <chown@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    138c <test_chown+0x20c>
mov    -0x18(%rbp),%rsi
mov    0x2c5d(%rip),%rax
mov    (%rax),%edx
lea    0xcfa(%rip),%rdi
mov    $0x12a,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <test_chown+0x255>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     13ce <test_chown+0x24e>
mov    -0x18(%rbp),%rsi
mov    0x2c1b(%rip),%rax
mov    (%rax),%edx
lea    0xd76(%rip),%rdi
mov    $0x12f,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d5 <test_chown+0x255>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_chown>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e6c(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
lea    0xe59(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1278 <test_chown+0xf8>
mov    %eax,%edi
call   1050 <close@plt>
test   %eax,%eax
js     1296 <test_chown+0x116>
lea    0xe36(%rip),%rdi
mov    $0xfffe,%esi
mov    $0xfffd,%edx
call   1070 <chown@plt>
test   %eax,%eax
js     12b4 <test_chown+0x134>
movl   $0xfffb,0xc(%rsp)
mov    $0x1,%ebx
lea    0xc(%rsp),%rsi
mov    $0x1,%edi
call   1060 <setgroups@plt>
test   %eax,%eax
js     12e4 <test_chown+0x164>
mov    0xc(%rsp),%edi
call   10a0 <setegid@plt>
test   %eax,%eax
js     1302 <test_chown+0x182>
mov    $0xfffc,%edi
call   1090 <setuid@plt>
test   %eax,%eax
js     1319 <test_chown+0x199>
lea    0xdd1(%rip),%rdi
mov    $0xffff,%esi
mov    $0xffff,%edx
call   1070 <chown@plt>
test   %eax,%eax
js     1337 <test_chown+0x1b7>
lea    0xdb3(%rip),%rdi
call   1030 <unlink@plt>
test   %eax,%eax
je     1358 <test_chown+0x1d8>
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
lea    0xec2(%rip),%rdi
lea    0xd8f(%rip),%rsi
mov    $0x12f,%ecx
jmp    12d0 <test_chown+0x150>
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
lea    0xd87(%rip),%rdi
lea    0xd71(%rip),%rsi
mov    $0x10b,%ecx
jmp    12d0 <test_chown+0x150>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edx
lea    0xd99(%rip),%rdi
lea    0xd53(%rip),%rsi
mov    $0x110,%ecx
jmp    12d0 <test_chown+0x150>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edx
lea    0xdaa(%rip),%rdi
lea    0xd35(%rip),%rsi
mov    $0x115,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2cdd(%rip),%rax
mov    (%rax),%esi
lea    0xda9(%rip),%rdi
mov    $0x11b,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12dc <test_chown+0x15c>
mov    0x2cbf(%rip),%rax
mov    (%rax),%esi
lea    0xdb7(%rip),%rdi
mov    $0x120,%edx
jmp    132e <test_chown+0x1ae>
mov    0x2ca8(%rip),%rax
mov    (%rax),%esi
lea    0xdd6(%rip),%rdi
mov    $0x125,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12d7 <test_chown+0x157>
mov    0x2c8a(%rip),%rax
mov    (%rax),%edx
lea    0xd27(%rip),%rdi
lea    0xcb2(%rip),%rsi
mov    $0x12a,%ecx
jmp    12d0 <test_chown+0x150>
xor    %ebx,%ebx
jmp    12dc <test_chown+0x15c>

LLVM IR (-O0):
@.str = [15 x i8] c"test-chmod.dat\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [48 x i8] c"Fail to create file %s, errno = %d at line %d!\0A\00"
@errno = global i32 0
@.str.2 = [47 x i8] c"Fail to close file %s, errno = %d at line %d!\0A\00"
@.str.3 = [47 x i8] c"Fail to chown file %s, errno = %d at line %d!\0A\00"
@.str.4 = [44 x i8] c"Fail to set groups, errno = %d at line %d!\0A\00"
@.str.5 = [54 x i8] c"Fail to set effective groups, errno = %d at line %d!\0A\00"
@.str.6 = [45 x i8] c"Fail to set user ID, errno = %d at line %d!\0A\00"
@.str.7 = [48 x i8] c"Fail to remove file %s, errno = %d at line %d!\0A\00"
define i32 @test_chown() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1 x i32]
%5 = alloca i8*
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = call i32 @open(i8* %6, i32 %9, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = load i8*, i8** %5
%16 = load i32, i32* @errno
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %16, i32 267)
store i32 1, i32* %1
br label %80
18: ; preds = %0
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = load i32, i32* @errno
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %25, i32 272)
store i32 1, i32* %1
br label %80
27: ; preds = %18
%28 = load i8*, i8** %5
%29 = call i32 @chown(i8* %28, i32 65534, i32 65533)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i8*, i8** %5
%34 = load i32, i32* @errno
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %33, i32 %34, i32 277)
store i32 1, i32* %1
br label %80
36: ; preds = %27
%37 = getelementptr inbounds [1 x i32], [1 x i32]* %4, i64 0, i64 0
store i32 65531, i32* %37
%38 = getelementptr inbounds [1 x i32], [1 x i32]* %4, i64 0, i64 0
%39 = call i32 @setgroups(i32 1, i32* %38)
store i32 %39, i32* %3
%40 = load i32, i32* %3
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = load i32, i32* @errno
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %43, i32 283)
store i32 1, i32* %1
br label %80
45: ; preds = %36
%46 = getelementptr inbounds [1 x i32], [1 x i32]* %4, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @setegid(i32 %47)
store i32 %48, i32* %3
%49 = load i32, i32* %3
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = load i32, i32* @errno
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i32 %52, i32 288)
store i32 1, i32* %1
br label %80
54: ; preds = %45
%55 = call i32 @setuid(i32 65532)
store i32 %55, i32* %3
%56 = load i32, i32* %3
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i32, i32* @errno
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %59, i32 293)
store i32 1, i32* %1
br label %80
61: ; preds = %54
%62 = load i8*, i8** %5
%63 = call i32 @chown(i8* %62, i32 65535, i32 65535)
store i32 %63, i32* %3
%64 = load i32, i32* %3
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %61
%67 = load i8*, i8** %5
%68 = load i32, i32* @errno
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %67, i32 %68, i32 298)
store i32 1, i32* %1
br label %80
70: ; preds = %61
%71 = load i8*, i8** %5
%72 = call i32 @unlink(i8* %71)
store i32 %72, i32* %3
%73 = load i32, i32* %3
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %70
%76 = load i8*, i8** %5
%77 = load i32, i32* @errno
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i8* %76, i32 %77, i32 303)
store i32 1, i32* %1
br label %80
79: ; preds = %70
store i32 0, i32* %1
br label %80
80: ; preds = %79, %75, %66, %58, %51, %42, %32, %23, %14
%81 = load i32, i32* %1
ret i32 %81
}

LLVM IR (-O2):
@.str = [15 x i8] c"test-chmod.dat\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [48 x i8] c"Fail to create file %s, errno = %d at line %d!\0A\00"
@errno = global i32 0
@.str.2 = [47 x i8] c"Fail to close file %s, errno = %d at line %d!\0A\00"
@.str.3 = [47 x i8] c"Fail to chown file %s, errno = %d at line %d!\0A\00"
@.str.4 = [44 x i8] c"Fail to set groups, errno = %d at line %d!\0A\00"
@.str.5 = [54 x i8] c"Fail to set effective groups, errno = %d at line %d!\0A\00"
@.str.6 = [45 x i8] c"Fail to set user ID, errno = %d at line %d!\0A\00"
@.str.7 = [48 x i8] c"Fail to remove file %s, errno = %d at line %d!\0A\00"
define i32 @test_chown() {
%1 = alloca [1 x i32]
%2 = bitcast [1 x i32]* %1 to i8*
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRWXU
%7 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10, i32 267)
br label %56
12: ; preds = %0
%13 = tail call i32 @close(i32 %7)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %16, i32 272)
br label %56
18: ; preds = %12
%19 = tail call i32 @chown(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 65534, i32 65533)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %22, i32 277)
br label %56
24: ; preds = %18
%25 = getelementptr inbounds [1 x i32], [1 x i32]* %1, i64 0, i64 0
store i32 65531, i32* %25
%26 = call i32 @setgroups(i32 1, i32* %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %29, i32 283)
br label %56
31: ; preds = %24
%32 = load i32, i32* %25
%33 = call i32 @setegid(i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @errno
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 288)
br label %56
38: ; preds = %31
%39 = call i32 @setuid(i32 65532)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* @errno
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %42, i32 293)
br label %56
44: ; preds = %38
%45 = call i32 @chown(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 65535, i32 65535)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* @errno
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %48, i32 298)
br label %56
50: ; preds = %44
%51 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%52 = icmp eq i32 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %50
%54 = load i32, i32* @errno
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %54, i32 303)
br label %56
56: ; preds = %50, %53, %47, %41, %35, %28, %21, %15, %9
%57 = phi i32 [ 1, %9 ], [ 1, %15 ], [ 1, %21 ], [ 1, %28 ], [ 1, %35 ], [ 1, %41 ], [ 1, %47 ], [ 1, %53 ], [ 0, %50 ]
ret i32 %57
}