#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;
struct passwd {
  int pw_uid;
};
struct group {
  scalar_t__ gr_gid;
};
typedef scalar_t__ gid_t;

/* Variables and functions */
scalar_t__ chown(char *, int, scalar_t__);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int main(int argc, char **argv) {
  struct passwd *ltpuser;
  struct group *ltpgroup;
  uid_t user_uid;
  gid_t group_gid;
  char *test_name;
  char *path_name;
  if (argc != 3) {
    fprintf(stderr,
            "This is a helper binary meant for internal LTP usage only\n");
    exit(1);
  }
  test_name = argv[1];
  path_name = argv[2];
  if ((ltpuser = getpwnam("nobody")) == NULL) {
    perror("change_owner: nobody not found in /etc/passwd");
    exit(1);
  }
  if ((ltpgroup = getgrnam("nobody")) == NULL) {
    if ((ltpgroup = getgrnam("nogroup")) == NULL) {
      perror("change_owner: nobody/nogroup's group not found in /etc/group");
      exit(1);
    }
  }
  user_uid = 0;
  group_gid = 0;
  if (!(strcmp(test_name, "fchown03"))) {
    user_uid = -1;
    group_gid = ltpgroup->gr_gid;
  } else if (!(strcmp(test_name, "fchown04"))) {
    user_uid = ltpuser->pw_uid;
    group_gid = ltpgroup->gr_gid;
  }
  if (chown(path_name, user_uid, group_gid) < 0) {
    fprintf(stderr,
            "change_owner: chown() of %s failed, error "
            "%d\n",
            path_name, errno);
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b1 <main+0x41>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
lea    0xe6b(%rip),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f9 <main+0x89>
lea    0xe58(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe3b(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1248 <main+0xd8>
lea    0xe56(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1243 <main+0xd3>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1248 <main+0xd8>
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1287 <main+0x117>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    12b9 <main+0x149>
mov    -0x38(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    12b4 <main+0x144>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    12b9 <main+0x149>
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rdx
call   1070 <chown@plt>
cmp    $0x0,%rax
jge    1301 <main+0x191>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
mov    0x2cf1(%rip),%rax
mov    (%rax),%ecx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    11ac <main+0x3c>
mov    0x8(%rsi),%r15
mov    0x10(%rsi),%r14
lea    0xeaf(%rip),%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
jne    11cd <main+0x5d>
lea    0xea5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rax,%rbx
lea    0xe64(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1201 <main+0x91>
lea    0xe85(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1201 <main+0x91>
lea    0xe79(%rip),%rdi
jmp    119d <main+0x2d>
lea    0xead(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcmp@plt>
mov    $0xffffffff,%esi
test   %eax,%eax
je     122e <main+0xbe>
lea    0xe9e(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
jne    1234 <main+0xc4>
mov    (%rbx),%esi
mov    (%r12),%rdx
jmp    1238 <main+0xc8>
xor    %edx,%edx
xor    %esi,%esi
mov    %r14,%rdi
call   1070 <chown@plt>
test   %rax,%rax
jns    1272 <main+0x102>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    0x2d83(%rip),%rax
mov    (%rax),%ecx
lea    0xe69(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i64 }
@stderr = global i32 0
@.str = [59 x i8] c"This is a helper binary meant for internal LTP usage only\0A\00"
@.str.1 = [7 x i8] c"nobody\00"
@.str.2 = [46 x i8] c"change_owner: nobody not found in /etc/passwd\00"
@.str.3 = [8 x i8] c"nogroup\00"
@.str.4 = [61 x i8] c"change_owner: nobody/nogroup's group not found in /etc/group\00"
@.str.5 = [9 x i8] c"fchown03\00"
@.str.6 = [9 x i8] c"fchown04\00"
@.str.7 = [46 x i8] c"change_owner: chown() of %s failed, error %d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca %struct.group*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %10
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
store i8* %24, i8** %11
%25 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store %struct.passwd* %25, %struct.passwd** %6
%26 = icmp eq %struct.passwd* %25, null
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %18
%31 = call %struct.group* @getgrnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store %struct.group* %31, %struct.group** %7
%32 = icmp eq %struct.group* %31, null
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = call %struct.group* @getgrnam(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store %struct.group* %34, %struct.group** %7
%35 = icmp eq %struct.group* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %33
br label %40
40: ; preds = %39, %30
store i32 0, i32* %8
store i64 0, i64* %9
%41 = load i8*, i8** %10
%42 = call i32 @strcmp(i8* %41, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%43 = icmp ne i32 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %40
store i32 -1, i32* %8
%45 = load %struct.group*, %struct.group** %7
%46 = getelementptr inbounds %struct.group, %struct.group* %45, i32 0, i32 0
%47 = load i64, i64* %46
store i64 %47, i64* %9
br label %60
48: ; preds = %40
%49 = load i8*, i8** %10
%50 = call i32 @strcmp(i8* %49, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%51 = icmp ne i32 %50, 0
br i1 %51, label %59, label %52
52: ; preds = %48
%53 = load %struct.passwd*, %struct.passwd** %6
%54 = getelementptr inbounds %struct.passwd, %struct.passwd* %53, i32 0, i32 0
%55 = load i32, i32* %54
store i32 %55, i32* %8
%56 = load %struct.group*, %struct.group** %7
%57 = getelementptr inbounds %struct.group, %struct.group* %56, i32 0, i32 0
%58 = load i64, i64* %57
store i64 %58, i64* %9
br label %59
59: ; preds = %52, %48
br label %60
60: ; preds = %59, %44
%61 = load i8*, i8** %11
%62 = load i32, i32* %8
%63 = load i64, i64* %9
%64 = call i64 @chown(i8* %61, i32 %62, i64 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %60
%67 = load i32, i32* @stderr
%68 = load i8*, i8** %11
%69 = load i32, i32* @errno
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.7, i64 0, i64 0), i8* %68, i32 %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %60
%73 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.group = type { i64 }
@stderr = global i32 0
@.str = [59 x i8] c"This is a helper binary meant for internal LTP usage only\0A\00"
@.str.1 = [7 x i8] c"nobody\00"
@.str.2 = [46 x i8] c"change_owner: nobody not found in /etc/passwd\00"
@.str.3 = [8 x i8] c"nogroup\00"
@.str.4 = [61 x i8] c"change_owner: nobody/nogroup's group not found in /etc/group\00"
@.str.5 = [9 x i8] c"fchown03\00"
@.str.6 = [9 x i8] c"fchown04\00"
@.str.7 = [46 x i8] c"change_owner: chown() of %s failed, error %d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %8
%19 = tail call %struct.group* @getgrnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq %struct.group* %19, null
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = tail call %struct.group* @getgrnam(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq %struct.group* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %21, %18
%28 = phi %struct.group* [ %22, %21 ], [ %19, %18 ]
%29 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(9) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %27
%32 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(9) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 0
%36 = load i32, i32* %35
br label %37
37: ; preds = %27, %34
%38 = phi i32 [ %36, %34 ], [ -1, %27 ]
%39 = getelementptr inbounds %struct.group, %struct.group* %28, i64 0, i32 0
%40 = load i64, i64* %39
br label %41
41: ; preds = %37, %31
%42 = phi i64 [ 0, %31 ], [ %40, %37 ]
%43 = phi i32 [ 0, %31 ], [ %38, %37 ]
%44 = tail call i64 @chown(i8* %12, i32 %43, i64 %42)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.7, i64 0, i64 0), i8* %12, i32 %48)
%50 = tail call i32 @exit(i32 1)
unreachable
51: ; preds = %41
%52 = tail call i32 @exit(i32 0)
unreachable
}