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

/* Variables and functions */
scalar_t__ chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usrgrp(char *, char **, char **);
int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stderr, "chown: not enough arguments\n");
    return 1;
  }
  char *user = NULL, *group = NULL;
  usrgrp(argv[1], &user, &group);
  struct passwd *usr = getpwnam(user);
  struct group *grp = NULL;
  if (group)
    grp = getgrnam(group);
  if (!usr || (group && !grp)) {
    perror("chown");
    return 1;
  }
  int i, err = 0;
  for (i = 2; i < argc; i++) {
    char *path = argv[i];
    struct stat st;
    if (stat(path, &st) < 0) {
      perror("chown");
      err = 1;
      continue;
    }
    uid_t setuid = usr->pw_uid;
    gid_t setgid = st.st_gid;
    if (grp)
      setgid = grp->gr_gid;
    if (chown(path, setuid, setgid) < 0) {
      perror("chown");
      err = 1;
      continue;
    }
  }
  return err;
}


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)
jge    11b1 <main+0x41>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1302 <main+0x192>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <usrgrp@plt>
mov    -0x18(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1203 <main+0x93>
mov    -0x20(%rbp),%rdi
call   1070 <getgrnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1224 <main+0xb4>
cmpq   $0x0,-0x20(%rbp)
je     123c <main+0xcc>
cmpq   $0x0,-0x30(%rbp)
jne    123c <main+0xcc>
lea    0xdf2(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1302 <main+0x192>
movl   $0x0,-0x38(%rbp)
movl   $0x2,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12fc <main+0x18c>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1295 <main+0x125>
lea    0xd99(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x38(%rbp)
jmp    12ee <main+0x17e>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x50(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12b8 <main+0x148>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    -0x50(%rbp),%edx
call   1080 <chown@plt>
cmp    $0x0,%rax
jge    12e9 <main+0x179>
lea    0xd45(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x38(%rbp)
jmp    12ee <main+0x17e>
jmp    12ee <main+0x17e>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    124a <main+0xda>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jg     11a2 <main+0x32>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    128a <main+0x11a>
mov    %rsi,%r14
mov    %edi,%ebp
movq   $0x0,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x8(%rsi),%rdi
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%rdx
call   1030 <usrgrp@plt>
mov    0x20(%rsp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,%r15
mov    0x18(%rsp),%rdi
test   %rdi,%rdi
je     11f5 <main+0x85>
call   1070 <getgrnam@plt>
mov    %rax,%rbx
test   %r15,%r15
jne    11fc <main+0x8c>
jmp    1272 <main+0x102>
xor    %ebx,%ebx
test   %r15,%r15
je     1272 <main+0x102>
cmpq   $0x0,0x18(%rsp)
je     1209 <main+0x99>
test   %rbx,%rbx
je     1272 <main+0x102>
mov    %ebp,%r12d
movl   $0x0,0xc(%rsp)
mov    $0x2,%ebp
jmp    123b <main+0xcb>
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%edx
mov    (%r15),%esi
mov    %r13,%rdi
call   1080 <chown@plt>
test   %rax,%rax
js     125c <main+0xec>
add    $0x1,%rbp
cmp    %rbp,%r12
je     1286 <main+0x116>
mov    (%r14,%rbp,8),%r13
mov    %r13,%rdi
lea    0x10(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     125c <main+0xec>
test   %rbx,%rbx
jne    1220 <main+0xb0>
mov    0x10(%rsp),%edx
jmp    1222 <main+0xb2>
lea    0xdba(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,0xc(%rsp)
jmp    1232 <main+0xc2>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"chown: not enough arguments\0A\00"
@.str.1 = [6 x i8] c"chown\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.passwd*
%9 = alloca %struct.group*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca %struct.stat
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %86
21: ; preds = %2
store i8* null, i8** %6
store i8* null, i8** %7
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @usrgrp(i8* %24, i8** %6, i8** %7)
%26 = load i8*, i8** %6
%27 = call %struct.passwd* @getpwnam(i8* %26)
store %struct.passwd* %27, %struct.passwd** %8
store %struct.group* null, %struct.group** %9
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i8*, i8** %7
%32 = call %struct.group* @getgrnam(i8* %31)
store %struct.group* %32, %struct.group** %9
br label %33
33: ; preds = %30, %21
%34 = load %struct.passwd*, %struct.passwd** %8
%35 = icmp ne %struct.passwd* %34, null
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i8*, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load %struct.group*, %struct.group** %9
%41 = icmp ne %struct.group* %40, null
br i1 %41, label %44, label %42
42: ; preds = %39, %33
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %86
44: ; preds = %39, %36
store i32 0, i32* %11
store i32 2, i32* %10
br label %45
45: ; preds = %81, %44
%46 = load i32, i32* %10
%47 = load i32, i32* %4
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %84
49: ; preds = %45
%50 = load i8**, i8*** %5
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %50, i64 %52
%54 = load i8*, i8** %53
store i8* %54, i8** %12
%55 = load i8*, i8** %12
%56 = call i64 @stat(i8* %55, %struct.stat* %13)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %49
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %11
br label %81
60: ; preds = %49
%61 = load %struct.passwd*, %struct.passwd** %8
%62 = getelementptr inbounds %struct.passwd, %struct.passwd* %61, i32 0, i32 0
%63 = load i32, i32* %62
store i32 %63, i32* %14
%64 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%65 = load i32, i32* %64
store i32 %65, i32* %15
%66 = load %struct.group*, %struct.group** %9
%67 = icmp ne %struct.group* %66, null
br i1 %67, label %68, label %72
68: ; preds = %60
%69 = load %struct.group*, %struct.group** %9
%70 = getelementptr inbounds %struct.group, %struct.group* %69, i32 0, i32 0
%71 = load i32, i32* %70
store i32 %71, i32* %15
br label %72
72: ; preds = %68, %60
%73 = load i8*, i8** %12
%74 = load i32, i32* %14
%75 = load i32, i32* %15
%76 = call i64 @chown(i8* %73, i32 %74, i32 %75)
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %72
%79 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %11
br label %81
80: ; preds = %72
br label %81
81: ; preds = %80, %78, %58
%82 = load i32, i32* %10
%83 = add nsw i32 %82, 1
store i32 %83, i32* %10
br label %45
84: ; preds = %45
%85 = load i32, i32* %11
store i32 %85, i32* %3
br label %86
86: ; preds = %84, %42, %18
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"chown: not enough arguments\0A\00"
@.str.1 = [6 x i8] c"chown\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = icmp slt i32 %0, 3
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %62
10: ; preds = %2
%11 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%12 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @usrgrp(i8* %14, i8** %3, i8** %4)
%16 = load i8*, i8** %3
%17 = call %struct.passwd* @getpwnam(i8* %16)
%18 = load i8*, i8** %4
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %10
%21 = call %struct.group* @getgrnam(i8* %18)
br label %22
22: ; preds = %20, %10
%23 = phi %struct.group* [ %21, %20 ], [ null, %10 ]
%24 = icmp eq %struct.passwd* %17, null
br i1 %24, label %36, label %25
25: ; preds = %22
%26 = load i8*, i8** %4
%27 = icmp eq i8* %26, null
%28 = icmp ne %struct.group* %23, null
%29 = select i1 %27, i1 true, i1 %28
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = bitcast %struct.stat* %5 to i8*
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i64 0, i32 0
%33 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%34 = getelementptr inbounds %struct.group, %struct.group* %23, i64 0, i32 0
%35 = zext i32 %0 to i64
br label %38
36: ; preds = %25, %22
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %60
38: ; preds = %30, %56
%39 = phi i64 [ 2, %30 ], [ %58, %56 ]
%40 = phi i32 [ 0, %30 ], [ %57, %56 ]
%41 = getelementptr inbounds i8*, i8** %1, i64 %39
%42 = load i8*, i8** %41
%43 = call i64 @stat(i8* %42, %struct.stat* %5)
%44 = icmp slt i64 %43, 0
br i1 %44, label %54, label %45
45: ; preds = %38
%46 = load i32, i32* %32
%47 = load i32, i32* %33
br i1 %28, label %48, label %50
48: ; preds = %45
%49 = load i32, i32* %34
br label %50
50: ; preds = %48, %45
%51 = phi i32 [ %49, %48 ], [ %47, %45 ]
%52 = call i64 @chown(i8* %42, i32 %46, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50, %38
%55 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %56
56: ; preds = %54, %50
%57 = phi i32 [ %40, %50 ], [ 1, %54 ]
%58 = add nuw nsw i64 %39, 1
%59 = icmp eq i64 %58, %35
br i1 %59, label %60, label %38
60: ; preds = %56, %36
%61 = phi i32 [ 1, %36 ], [ %57, %56 ]
br label %62
62: ; preds = %60, %7
%63 = phi i32 [ 1, %7 ], [ %61, %60 ]
ret i32 %63
}