#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 */
int chown(char *, int, scalar_t__);
int /*<<< orphan*/ 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;
scalar_t__ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
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("getpwnam(\"nobody\") failed");
    exit(1);
  }
  if ((ltpgroup = getgrnam("nobody")) == NULL) {
    if ((ltpgroup = getgrnam("nogroup")) == NULL) {
      perror("getgrnam(\"nobody\") failed");
      exit(1);
    }
  }
  user_uid = 0;
  group_gid = 0;
  if (strcmp(test_name, "fchown03") == 0) {
    user_uid = -1;
    group_gid = ltpgroup->gr_gid;
  } else if (strcmp(test_name, "fchown04") == 0) {
    user_uid = ltpuser->pw_uid;
    group_gid = ltpgroup->gr_gid;
  }
  if (chown(path_name, user_uid, group_gid) == -1) {
    fprintf(stderr, "chown(%s, ..) failed: %s", path_name, strerror(errno));
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c1 <main+0x41>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%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    0xe5b(%rip),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1209 <main+0x89>
lea    0xe48(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe2b(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1258 <main+0xd8>
lea    0xe32(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1253 <main+0xd3>
lea    0xe20(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1258 <main+0xd8>
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    1298 <main+0x118>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    12cb <main+0x14b>
mov    -0x38(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    12c6 <main+0x146>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    12cb <main+0x14b>
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rdx
call   1070 <chown@plt>
cmp    $0xffffffff,%eax
jne    1328 <main+0x1a8>
mov    0x2cf5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd79(%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    11bc <main+0x3c>
mov    0x8(%rsi),%r15
mov    0x10(%rsi),%r14
lea    0xe9f(%rip),%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
jne    11dd <main+0x5d>
lea    0xe95(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rax,%rbx
lea    0xe54(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1211 <main+0x91>
lea    0xe61(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1211 <main+0x91>
lea    0xe55(%rip),%rdi
jmp    11ad <main+0x2d>
lea    0xe66(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcmp@plt>
mov    $0xffffffff,%esi
test   %rax,%rax
je     1240 <main+0xc0>
lea    0xe56(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jne    1246 <main+0xc6>
mov    (%rbx),%esi
mov    (%r12),%rdx
jmp    124a <main+0xca>
xor    %edx,%edx
xor    %esi,%esi
mov    %r14,%rdi
call   1070 <chown@plt>
cmp    $0xffffffff,%eax
jne    128e <main+0x10e>
mov    0x2d82(%rip),%rax
mov    (%rax),%ebx
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe1b(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
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 = [26 x i8] c"getpwnam(\22nobody\22) failed\00"
@.str.3 = [8 x i8] c"nogroup\00"
@.str.4 = [26 x i8] c"getgrnam(\22nobody\22) failed\00"
@.str.5 = [9 x i8] c"fchown03\00"
@.str.6 = [9 x i8] c"fchown04\00"
@.str.7 = [25 x i8] c"chown(%s, ..) failed: %s\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 ([26 x i8], [26 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 ([26 x i8], [26 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 i64 @strcmp(i8* %41, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %48
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 i64 @strcmp(i8* %49, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %59
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 i32 @chown(i8* %61, i32 %62, i64 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %73
66: ; preds = %60
%67 = load i32, i32* @stderr
%68 = load i8*, i8** %11
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i8* %68, i8* %70)
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %60
%74 = 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 = [26 x i8] c"getpwnam(\22nobody\22) failed\00"
@.str.3 = [8 x i8] c"nogroup\00"
@.str.4 = [26 x i8] c"getgrnam(\22nobody\22) failed\00"
@.str.5 = [9 x i8] c"fchown03\00"
@.str.6 = [9 x i8] c"fchown04\00"
@.str.7 = [25 x i8] c"chown(%s, ..) failed: %s\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 ([26 x i8], [26 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 ([26 x i8], [26 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 i64 @strcmp(i8* %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %27
%32 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%33 = icmp eq i64 %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 i32 @chown(i8* %12, i32 %43, i64 %42)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %52
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = tail call i8* @strerror(i32 %48)
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i8* %12, i8* %49)
%51 = tail call i32 @exit(i32 1)
unreachable
52: ; preds = %41
%53 = tail call i32 @exit(i32 0)
unreachable
}