#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 */
struct group {
  int gr_gid;
};

/* Variables and functions */
int chown(char *, int, int);
scalar_t__ errno;
struct group *getgrnam(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ strtol(char *, char **, int);
void cmd_chgrp(int argc, char *argv[]) {
  struct group *grp;
  int gid;
  errno = 0;
  gid = (int)strtol(argv[1], (char **)NULL, 10);
  if (errno) {
    if ((grp = getgrnam(argv[1])) == NULL) {
      perror("getgrnam");
      return;
    }
    gid = grp->gr_gid;
  }
  if (chown(argv[2], -1, gid) == -1)
    perror("chown");
}


x86 assembly (clang, -O0):
<cmd_chgrp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e63(%rip),%rax
cmpq   $0x0,(%rax)
je     11bc <cmd_chgrp+0x7c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <getgrnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b3 <cmd_chgrp+0x73>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
jmp    11e6 <cmd_chgrp+0xa6>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x1c(%rbp),%edx
mov    $0xffffffff,%esi
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
jne    11e6 <cmd_chgrp+0xa6>
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_chgrp>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e92(%rip),%r14
movq   $0x0,(%r14)
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1030 <strtol@plt>
cmpq   $0x0,(%r14)
je     117b <cmd_chgrp+0x3b>
mov    0x8(%rbx),%rdi
call   1040 <getgrnam@plt>
test   %rax,%rax
je     11a1 <cmd_chgrp+0x61>
mov    (%rax),%eax
mov    0x10(%rbx),%rdi
mov    $0xffffffff,%esi
mov    %eax,%edx
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
je     1198 <cmd_chgrp+0x58>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6a(%rip),%rdi
jmp    11a8 <cmd_chgrp+0x68>
lea    0xe58(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <perror@plt>

LLVM IR (-O0):
%struct.group = type { i32 }
@errno = global i64 0
@.str = [9 x i8] c"getgrnam\00"
@.str.1 = [6 x i8] c"chown\00"
define void @cmd_chgrp(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.group*
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i64 0, i64* @errno
%7 = load i8**, i8*** %4
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
%10 = call i64 @strtol(i8* %9, i8** null, i32 10)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %6
%12 = load i64, i64* @errno
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %2
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call %struct.group* @getgrnam(i8* %17)
store %struct.group* %18, %struct.group** %5
%19 = icmp eq %struct.group* %18, null
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %35
22: ; preds = %14
%23 = load %struct.group*, %struct.group** %5
%24 = getelementptr inbounds %struct.group, %struct.group* %23, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %6
br label %26
26: ; preds = %22, %2
%27 = load i8**, i8*** %4
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = load i32, i32* %6
%31 = call i32 @chown(i8* %29, i32 -1, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %20, %33, %26
ret void
}

LLVM IR (-O2):
%struct.group = type { i32 }
@errno = global i64 0
@.str = [9 x i8] c"getgrnam\00"
@.str.1 = [6 x i8] c"chown\00"
define void @cmd_chgrp(i32 %0, i8** nocapture %1) {
store i64 0, i64* @errno
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i64 @strtol(i8* nocapture %4, i8** null, i32 10)
%6 = trunc i64 %5 to i32
%7 = load i64, i64* @errno
%8 = icmp eq i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = load i8*, i8** %3
%11 = tail call %struct.group* @getgrnam(i8* %10)
%12 = icmp eq %struct.group* %11, null
br i1 %12, label %22, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 0
%15 = load i32, i32* %14
br label %16
16: ; preds = %13, %2
%17 = phi i32 [ %15, %13 ], [ %6, %2 ]
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @chown(i8* %19, i32 -1, i32 %17)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16, %9
%23 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %16 ]
%24 = tail call i32 @perror(i8* %23)
br label %25
25: ; preds = %22, %16
ret void
}