#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_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 *);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stderr, "chgrp: not enough arguments\n");
    return 1;
  }
  struct group *grp = getgrnam(argv[1]);
  if (!grp) {
    perror("chgrp");
    return 1;
  }
  int i, ret = 0;
  for (i = 2; i < argc; i++) {
    char *path = argv[i];
    struct stat st;
    if (stat(path, &st) < 0) {
      perror("chgrp");
      ret = 1;
      continue;
    }
    uid_t setuid = st.st_uid;
    gid_t setgid = grp->gr_gid;
    if (chown(path, setuid, setgid) < 0) {
      perror("chgrp");
      ret = 1;
      continue;
    }
  }
  return ret;
}


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)
jge    1191 <main+0x41>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <main+0x127>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c5 <main+0x75>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1277 <main+0x127>
movl   $0x0,-0x20(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1271 <main+0x121>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    121e <main+0xce>
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x20(%rbp)
jmp    1263 <main+0x113>
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x38(%rbp),%edx
call   1060 <chown@plt>
cmp    $0x0,%rax
jge    125e <main+0x10e>
lea    0xdd0(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x20(%rbp)
jmp    1263 <main+0x113>
jmp    1263 <main+0x113>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d3 <main+0x83>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jg     1193 <main+0x43>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe8d(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,0xc(%rsp)
mov    0xc(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   1050 <getgrnam@plt>
test   %rax,%rax
je     1212 <main+0xc2>
mov    %rax,%rbx
mov    %ebp,%r12d
movl   $0x0,0xc(%rsp)
mov    $0x2,%r15d
lea    0x10(%rsp),%r13
jmp    11e9 <main+0x99>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r15
cmp    %r15,%r12
je     1180 <main+0x30>
mov    (%r14,%r15,8),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     11c1 <main+0x71>
mov    0x10(%rsp),%esi
mov    (%rbx),%edx
mov    %rbp,%rdi
call   1060 <chown@plt>
test   %rax,%rax
jns    11e0 <main+0x90>
jmp    11c1 <main+0x71>
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
jmp    1178 <main+0x28>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"chgrp: not enough arguments\0A\00"
@.str.1 = [6 x i8] c"chgrp\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %38
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call %struct.group* @getgrnam(i8* %10)
%12 = icmp eq %struct.group* %11, null
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = bitcast %struct.stat* %3 to i8*
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 0
%17 = zext i32 %0 to i64
br label %20
18: ; preds = %8
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %38
20: ; preds = %13, %34
%21 = phi i64 [ 2, %13 ], [ %36, %34 ]
%22 = phi i32 [ 0, %13 ], [ %35, %34 ]
%23 = getelementptr inbounds i8*, i8** %1, i64 %21
%24 = load i8*, i8** %23
%25 = call i64 @stat(i8* %24, %struct.stat* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %20
%28 = load i32, i32* %15
%29 = load i32, i32* %16
%30 = call i64 @chown(i8* %24, i32 %28, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27, %20
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = phi i32 [ %22, %27 ], [ 1, %32 ]
%36 = add nuw nsw i64 %21, 1
%37 = icmp eq i64 %36, %17
br i1 %37, label %38, label %20
38: ; preds = %34, %18, %5
%39 = phi i32 [ 1, %5 ], [ 1, %18 ], [ %35, %34 ]
ret i32 %39
}