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

/* Variables and functions */
scalar_t__ chown(char *, int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
scalar_t__ strtoul(char *, char **, int /*<<< orphan*/);
int chown_main(int argc, char **argv) {
  int i;
  if (argc < 3) {
    fprintf(stderr, "Usage: chown <USER>[.GROUP] <FILE1> [FILE2] ...\n");
    return 10;
  }
  char user[32];
  char *group = NULL;
  strncpy(user, argv[1], sizeof(user));
  if ((group = strchr(user, '.')) != NULL) {
    *group++ = '\0';
  }
  struct passwd *pw;
  struct group *grp = NULL;
  uid_t uid;
  gid_t gid = -1;
  pw = getpwnam(user);
  if (pw != NULL) {
    uid = pw->pw_uid;
  } else {
    char *endptr;
    uid = (int)strtoul(user, &endptr, 0);
    if (endptr == user) {
      fprintf(stderr, "No such user '%s'\n", user);
      return 10;
    }
  }
  if (group != NULL) {
    grp = getgrnam(group);
    if (grp != NULL) {
      gid = grp->gr_gid;
    } else {
      char *endptr;
      gid = (int)strtoul(group, &endptr, 0);
      if (endptr == group) {
        fprintf(stderr, "No such group '%s'\n", group);
        return 10;
      }
    }
  }
  for (i = 2; i < argc; i++) {
    if (chown(argv[i], uid, gid) < 0) {
      fprintf(stderr, "Unable to chmod %s: %s\n", argv[i], strerror(errno));
      return 10;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<chown_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11bf <chown_main+0x3f>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    13ad <chown_main+0x22d>
movq   $0x0,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x20,%edx
call   1030 <strncpy@plt>
lea    -0x40(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     120b <chown_main+0x8b>
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x48(%rbp)
movb   $0x0,(%rax)
movq   $0x0,-0x58(%rbp)
movl   $0xffffffff,-0x60(%rbp)
lea    -0x40(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1240 <chown_main+0xc0>
mov    -0x50(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
jmp    128f <chown_main+0x10f>
lea    -0x40(%rbp),%rdi
lea    -0x68(%rbp),%rsi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x68(%rbp),%rax
lea    -0x40(%rbp),%rcx
cmp    %rcx,%rax
jne    128a <chown_main+0x10a>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    13ad <chown_main+0x22d>
jmp    128f <chown_main+0x10f>
cmpq   $0x0,-0x48(%rbp)
je     1311 <chown_main+0x191>
mov    -0x48(%rbp),%rdi
call   1070 <getgrnam@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12c0 <chown_main+0x140>
mov    -0x58(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
jmp    130c <chown_main+0x18c>
mov    -0x48(%rbp),%rdi
lea    -0x70(%rbp),%rsi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x60(%rbp)
mov    -0x70(%rbp),%rax
cmp    -0x48(%rbp),%rax
jne    1307 <chown_main+0x187>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xd50(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    13ad <chown_main+0x22d>
jmp    130c <chown_main+0x18c>
jmp    1311 <chown_main+0x191>
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    13a6 <chown_main+0x226>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x5c(%rbp),%esi
mov    -0x60(%rbp),%edx
call   1080 <chown@plt>
cmp    $0x0,%rax
jge    1393 <chown_main+0x213>
mov    0x2c94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x78(%rbp)
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    %rax,%rcx
lea    0xcd8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xa,-0x4(%rbp)
jmp    13ad <chown_main+0x22d>
jmp    1398 <chown_main+0x218>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1318 <chown_main+0x198>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chown_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0x2,%edi
jg     11af <chown_main+0x2f>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12f7 <chown_main+0x177>
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x8(%rsi),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11e8 <chown_main+0x68>
mov    %rax,%r15
add    $0x1,%r15
movb   $0x0,(%rax)
jmp    11eb <chown_main+0x6b>
xor    %r15d,%r15d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     1217 <chown_main+0x97>
mov    (%rax),%r13d
test   %r15,%r15
je     1239 <chown_main+0xb9>
mov    %r15,%rdi
call   1070 <getgrnam@plt>
test   %rax,%rax
je     1241 <chown_main+0xc1>
mov    (%rax),%r14d
jmp    125e <chown_main+0xde>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <strtoul@plt>
cmp    %rbx,0x8(%rsp)
je     12c6 <chown_main+0x146>
mov    %rax,%r13
test   %r15,%r15
jne    1205 <chown_main+0x85>
mov    $0xffffffff,%r14d
jmp    125e <chown_main+0xde>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   1090 <strtoul@plt>
cmp    %r15,0x8(%rsp)
je     12dd <chown_main+0x15d>
mov    %rax,%r14
mov    %ebp,%ebx
mov    $0x2,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12,%rbp,8),%rdi
mov    %r13d,%esi
mov    %r14d,%edx
call   1080 <chown@plt>
test   %rax,%rax
js     1291 <chown_main+0x111>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1270 <chown_main+0xf0>
xor    %eax,%eax
jmp    12fc <chown_main+0x17c>
mov    %ebp,%eax
mov    0x2d46(%rip),%rcx
mov    (%rcx),%ebx
mov    (%r12,%rax,8),%rbp
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xda3(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12f7 <chown_main+0x177>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xd5b(%rip),%rsi
lea    0x10(%rsp),%rdx
jmp    12f0 <chown_main+0x170>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xa,%eax
add    $0x38,%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 }
@stderr = global i32 0
@.str = [49 x i8] c"Usage: chown <USER>[.GROUP] <FILE1> [FILE2] ...\0A\00"
@.str.1 = [19 x i8] c"No such user '%s'\0A\00"
@.str.2 = [20 x i8] c"No such group '%s'\0A\00"
@.str.3 = [24 x i8] c"Unable to chmod %s: %s\0A\00"
@errno = global i32 0
define i32 @chown_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca i8*
%9 = alloca %struct.passwd*
%10 = alloca %struct.group*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
store i32 10, i32* %3
br label %108
20: ; preds = %2
store i8* null, i8** %8
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @strncpy(i8* %21, i8* %24, i32 32)
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%27 = call i8* @strchr(i8* %26, i8 signext 46)
store i8* %27, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i8*, i8** %8
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %8
store i8 0, i8* %30
br label %32
32: ; preds = %29, %20
store %struct.group* null, %struct.group** %10
store i32 -1, i32* %12
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%34 = call %struct.passwd* @getpwnam(i8* %33)
store %struct.passwd* %34, %struct.passwd** %9
%35 = load %struct.passwd*, %struct.passwd** %9
%36 = icmp ne %struct.passwd* %35, null
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load %struct.passwd*, %struct.passwd** %9
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 0
%40 = load i32, i32* %39
store i32 %40, i32* %11
br label %53
41: ; preds = %32
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%43 = call i64 @strtoul(i8* %42, i8** %13, i32 0)
%44 = trunc i64 %43 to i32
store i32 %44, i32* %11
%45 = load i8*, i8** %13
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%47 = icmp eq i8* %45, %46
br i1 %47, label %48, label %52
48: ; preds = %41
%49 = load i32, i32* @stderr
%50 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %50)
store i32 10, i32* %3
br label %108
52: ; preds = %41
br label %53
53: ; preds = %52, %37
%54 = load i8*, i8** %8
%55 = icmp ne i8* %54, null
br i1 %55, label %56, label %78
56: ; preds = %53
%57 = load i8*, i8** %8
%58 = call %struct.group* @getgrnam(i8* %57)
store %struct.group* %58, %struct.group** %10
%59 = load %struct.group*, %struct.group** %10
%60 = icmp ne %struct.group* %59, null
br i1 %60, label %61, label %65
61: ; preds = %56
%62 = load %struct.group*, %struct.group** %10
%63 = getelementptr inbounds %struct.group, %struct.group* %62, i32 0, i32 0
%64 = load i32, i32* %63
store i32 %64, i32* %12
br label %77
65: ; preds = %56
%66 = load i8*, i8** %8
%67 = call i64 @strtoul(i8* %66, i8** %14, i32 0)
%68 = trunc i64 %67 to i32
store i32 %68, i32* %12
%69 = load i8*, i8** %14
%70 = load i8*, i8** %8
%71 = icmp eq i8* %69, %70
br i1 %71, label %72, label %76
72: ; preds = %65
%73 = load i32, i32* @stderr
%74 = load i8*, i8** %8
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %74)
store i32 10, i32* %3
br label %108
76: ; preds = %65
br label %77
77: ; preds = %76, %61
br label %78
78: ; preds = %77, %53
store i32 2, i32* %6
br label %79
79: ; preds = %104, %78
%80 = load i32, i32* %6
%81 = load i32, i32* %4
%82 = icmp slt i32 %80, %81
br i1 %82, label %83, label %107
83: ; preds = %79
%84 = load i8**, i8*** %5
%85 = load i32, i32* %6
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8*, i8** %84, i64 %86
%88 = load i8*, i8** %87
%89 = load i32, i32* %11
%90 = load i32, i32* %12
%91 = call i64 @chown(i8* %88, i32 %89, i32 %90)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %103
93: ; preds = %83
%94 = load i32, i32* @stderr
%95 = load i8**, i8*** %5
%96 = load i32, i32* %6
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i8*, i8** %95, i64 %97
%99 = load i8*, i8** %98
%100 = load i32, i32* @errno
%101 = call i8* @strerror(i32 %100)
%102 = call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %99, i8* %101)
store i32 10, i32* %3
br label %108
103: ; preds = %83
br label %104
104: ; preds = %103
%105 = load i32, i32* %6
%106 = add nsw i32 %105, 1
store i32 %106, i32* %6
br label %79
107: ; preds = %79
store i32 0, i32* %3
br label %108
108: ; preds = %107, %93, %72, %48, %17
%109 = load i32, i32* %3
ret i32 %109
}

LLVM IR (-O2):
%struct.group = type { i32 }
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [49 x i8] c"Usage: chown <USER>[.GROUP] <FILE1> [FILE2] ...\0A\00"
@.str.1 = [19 x i8] c"No such user '%s'\0A\00"
@.str.2 = [20 x i8] c"No such group '%s'\0A\00"
@.str.3 = [24 x i8] c"Unable to chmod %s: %s\0A\00"
@errno = global i32 0
define i32 @chown_main(i32 %0, i8** nocapture %1) {
%3 = alloca [32 x i8]
%4 = alloca i8*
%5 = alloca i8*
%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 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %77
10: ; preds = %2
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @strncpy(i8* %11, i8* %13, i32 32)
%15 = call i8* @strchr(i8* %11, i8 signext 46)
%16 = icmp eq i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %10
%18 = getelementptr inbounds i8, i8* %15, i64 1
store i8 0, i8* %15
br label %19
19: ; preds = %17, %10
%20 = phi i8* [ %18, %17 ], [ null, %10 ]
%21 = call %struct.passwd* @getpwnam(i8* %11)
%22 = icmp eq %struct.passwd* %21, null
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i64 0, i32 0
%25 = load i32, i32* %24
br label %36
26: ; preds = %19
%27 = bitcast i8** %4 to i8*
%28 = call i64 @strtoul(i8* %11, i8** %4, i32 0)
%29 = load i8*, i8** %4
%30 = icmp eq i8* %29, %11
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %75
34: ; preds = %26
%35 = trunc i64 %28 to i32
br label %36
36: ; preds = %34, %23
%37 = phi i32 [ %25, %23 ], [ %35, %34 ]
%38 = icmp eq i8* %20, null
br i1 %38, label %55, label %39
39: ; preds = %36
%40 = call %struct.group* @getgrnam(i8* %20)
%41 = icmp eq %struct.group* %40, null
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = getelementptr inbounds %struct.group, %struct.group* %40, i64 0, i32 0
%44 = load i32, i32* %43
br label %55
45: ; preds = %39
%46 = bitcast i8** %5 to i8*
%47 = call i64 @strtoul(i8* %20, i8** %5, i32 0)
%48 = load i8*, i8** %5
%49 = icmp eq i8* %48, %20
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %75
53: ; preds = %45
%54 = trunc i64 %47 to i32
br label %55
55: ; preds = %36, %42, %53
%56 = phi i32 [ %44, %42 ], [ -1, %36 ], [ %54, %53 ]
%57 = zext i32 %0 to i64
br label %58
58: ; preds = %55, %72
%59 = phi i64 [ 2, %55 ], [ %73, %72 ]
%60 = getelementptr inbounds i8*, i8** %1, i64 %59
%61 = load i8*, i8** %60
%62 = call i64 @chown(i8* %61, i32 %37, i32 %56)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %72
64: ; preds = %58
%65 = and i64 %59, 4294967295
%66 = getelementptr inbounds i8*, i8** %1, i64 %65
%67 = load i32, i32* @stderr
%68 = load i8*, i8** %66
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %68, i8* %70)
br label %75
72: ; preds = %58
%73 = add nuw nsw i64 %59, 1
%74 = icmp eq i64 %73, %57
br i1 %74, label %75, label %58
75: ; preds = %72, %50, %31, %64
%76 = phi i32 [ 10, %64 ], [ 10, %50 ], [ 10, %31 ], [ 0, %72 ]
br label %77
77: ; preds = %75, %7
%78 = phi i32 [ 10, %7 ], [ %76, %75 ]
ret i32 %78
}