#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 {
  int gr_gid;
};
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ chown(char *, int, int);
int /*<<< orphan*/ display_perms(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  if (argc < 4) {
    fprintf(stderr, "Usage: %s <file> <user> <group>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  uid_t uid = -1;
  struct passwd *pwd;
  gid_t gid = -1;
  struct group *grp;
  if (strcmp(argv[2], "-1") != 0) {
    pwd = getpwnam(argv[2]);
    if (pwd == NULL) {
      printf("'%s' not found\n", argv[2]);
      exit(EXIT_FAILURE);
    }
    uid = pwd->pw_uid;
  }
  if (strcmp(argv[3], "-1") != 0) {
    grp = getgrnam(argv[3]);
    if (grp == NULL) {
      printf("'%s' not found\n", argv[3]);
      exit(EXIT_FAILURE);
    }
    gid = grp->gr_gid;
  }
  printf("Before: ");
  display_perms(argv[1]);
  if (chown(argv[1], uid, gid) < 0) {
    perror("chown");
    goto fail;
  }
  printf("After: ");
  display_perms(argv[1]);
  exit(0);
fail:
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11da <main+0x4a>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe2a(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     124f <main+0xbf>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1246 <main+0xb6>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xdc3(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     12b6 <main+0x126>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <getgrnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12ad <main+0x11d>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <display_perms@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1090 <chown@plt>
cmp    $0x0,%rax
jge    12ff <main+0x16f>
lea    0xd48(%rip),%rdi
call   10a0 <perror@plt>
jmp    1321 <main+0x191>
lea    0xd3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <display_perms@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x3,%edi
jg     11b9 <main+0x29>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe51(%rip),%rsi
call   1060 <fprintf@plt>
jmp    125f <main+0xcf>
mov    0x10(%rbx),%rdi
lea    0xe5d(%rip),%rsi
call   1040 <strcmp@plt>
mov    $0xffffffff,%r14d
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11ef <main+0x5f>
mov    0x10(%rbx),%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
jne    11ed <main+0x5d>
mov    0x10(%rbx),%rsi
jmp    1216 <main+0x86>
mov    (%rax),%ebp
mov    0x18(%rbx),%rdi
lea    0xe27(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1229 <main+0x99>
mov    0x18(%rbx),%rdi
call   1070 <getgrnam@plt>
test   %rax,%rax
jne    1226 <main+0x96>
mov    0x18(%rbx),%rsi
lea    0xe07(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    125f <main+0xcf>
mov    (%rax),%r14d
lea    0xe04(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x8(%rbx),%rdi
call   1080 <display_perms@plt>
mov    0x8(%rbx),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1090 <chown@plt>
test   %rax,%rax
jns    126d <main+0xdd>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xdcf(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x8(%rbx),%rdi
call   1080 <display_perms@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [33 x i8] c"Usage: %s <file> <user> <group>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"-1\00"
@.str.2 = [16 x i8] c"'%s' not found\0A\00"
@.str.3 = [9 x i8] c"Before: \00"
@.str.4 = [6 x i8] c"chown\00"
@.str.5 = [8 x i8] c"After: \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %27, label %15
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call %struct.passwd* @getpwnam(i8* %16)
%18 = icmp eq %struct.passwd* %17, null
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i8*, i8** %11
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i64 0, i32 0
%26 = load i32, i32* %25
br label %27
27: ; preds = %24, %10
%28 = phi i32 [ %26, %24 ], [ -1, %10 ]
%29 = getelementptr inbounds i8*, i8** %1, i64 3
%30 = load i8*, i8** %29
%31 = tail call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %45, label %33
33: ; preds = %27
%34 = load i8*, i8** %29
%35 = tail call %struct.group* @getgrnam(i8* %34)
%36 = icmp eq %struct.group* %35, null
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i8*, i8** %29
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
%43 = getelementptr inbounds %struct.group, %struct.group* %35, i64 0, i32 0
%44 = load i32, i32* %43
br label %45
45: ; preds = %42, %27
%46 = phi i32 [ %44, %42 ], [ -1, %27 ]
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%48 = getelementptr inbounds i8*, i8** %1, i64 1
%49 = load i8*, i8** %48
%50 = tail call i32 @display_perms(i8* %49)
%51 = load i8*, i8** %48
%52 = tail call i64 @chown(i8* %51, i32 %28, i32 %46)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %45
%55 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = tail call i32 @exit(i32 %56)
unreachable
58: ; preds = %45
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%60 = load i8*, i8** %48
%61 = tail call i32 @display_perms(i8* %60)
%62 = tail call i32 @exit(i32 0)
unreachable
}