#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;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ display_perms(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fchown(int, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int open(char *, int /*<<< orphan*/);
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]);
  int fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    perror("open");
    goto fail;
  }
  if (fchown(fd, uid, gid) < 0) {
    perror("fchown");
    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    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11ea <main+0x4a>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe1a(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     125f <main+0xbf>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1256 <main+0xb6>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xdb3(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     12c6 <main+0x126>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <getgrnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12bd <main+0x11d>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xd67(%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    0x2cf8(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1315 <main+0x175>
lea    0xd32(%rip),%rdi
call   10b0 <perror@plt>
jmp    1360 <main+0x1c0>
mov    -0x34(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
call   10a0 <fchown@plt>
cmp    $0x0,%rax
jge    133e <main+0x19e>
lea    0xd0e(%rip),%rdi
call   10b0 <perror@plt>
jmp    1360 <main+0x1c0>
lea    0xd04(%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   10c0 <exit@plt>
mov    0x2c69(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x3,%edi
jg     11c9 <main+0x29>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe41(%rip),%rsi
call   1060 <fprintf@plt>
jmp    128c <main+0xec>
mov    0x10(%rbx),%rdi
lea    0xe4d(%rip),%rsi
call   1040 <strcmp@plt>
mov    $0xffffffff,%r14d
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11ff <main+0x5f>
mov    0x10(%rbx),%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
jne    11fd <main+0x5d>
mov    0x10(%rbx),%rsi
jmp    1226 <main+0x86>
mov    (%rax),%ebp
mov    0x18(%rbx),%rdi
lea    0xe17(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1239 <main+0x99>
mov    0x18(%rbx),%rdi
call   1070 <getgrnam@plt>
test   %rax,%rax
jne    1236 <main+0x96>
mov    0x18(%rbx),%rsi
lea    0xdf7(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    128c <main+0xec>
mov    (%rax),%r14d
lea    0xdf4(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x8(%rbx),%rdi
call   1080 <display_perms@plt>
mov    0x8(%rbx),%rdi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
jns    126f <main+0xcf>
lea    0xdd0(%rip),%rdi
jmp    1287 <main+0xe7>
mov    %eax,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   10a0 <fchown@plt>
test   %rax,%rax
jns    129a <main+0xfa>
lea    0xdbb(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xda8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x8(%rbx),%rdi
call   1080 <display_perms@plt>
xor    %edi,%edi
call   10c0 <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"
@O_RDWR = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [7 x i8] c"fchown\00"
@.str.6 = [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*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 4
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
store i32 -1, i32* %6
store i32 -1, i32* %8
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %45
27: ; preds = %21
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call %struct.passwd* @getpwnam(i8* %30)
store %struct.passwd* %31, %struct.passwd** %7
%32 = load %struct.passwd*, %struct.passwd** %7
%33 = icmp eq %struct.passwd* %32, null
br i1 %33, label %34, label %41
34: ; preds = %27
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %27
%42 = load %struct.passwd*, %struct.passwd** %7
%43 = getelementptr inbounds %struct.passwd, %struct.passwd* %42, i32 0, i32 0
%44 = load i32, i32* %43
store i32 %44, i32* %6
br label %45
45: ; preds = %41, %21
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 3
%48 = load i8*, i8** %47
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %69
51: ; preds = %45
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 3
%54 = load i8*, i8** %53
%55 = call %struct.group* @getgrnam(i8* %54)
store %struct.group* %55, %struct.group** %9
%56 = load %struct.group*, %struct.group** %9
%57 = icmp eq %struct.group* %56, null
br i1 %57, label %58, label %65
58: ; preds = %51
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 3
%61 = load i8*, i8** %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %51
%66 = load %struct.group*, %struct.group** %9
%67 = getelementptr inbounds %struct.group, %struct.group* %66, i32 0, i32 0
%68 = load i32, i32* %67
store i32 %68, i32* %8
br label %69
69: ; preds = %65, %45
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 1
%73 = load i8*, i8** %72
%74 = call i32 @display_perms(i8* %73)
%75 = load i8**, i8*** %5
%76 = getelementptr inbounds i8*, i8** %75, i64 1
%77 = load i8*, i8** %76
%78 = load i32, i32* @O_RDWR
%79 = call i32 @open(i8* %77, i32 %78)
store i32 %79, i32* %10
%80 = load i32, i32* %10
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %69
%83 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %99
84: ; preds = %69
%85 = load i32, i32* %10
%86 = load i32, i32* %6
%87 = load i32, i32* %8
%88 = call i64 @fchown(i32 %85, i32 %86, i32 %87)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %84
%91 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %99
92: ; preds = %84
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%94 = load i8**, i8*** %5
%95 = getelementptr inbounds i8*, i8** %94, i64 1
%96 = load i8*, i8** %95
%97 = call i32 @display_perms(i8* %96)
%98 = call i32 @exit(i32 0)
unreachable
99: ; preds = %90, %82
%100 = load i32, i32* @EXIT_FAILURE
%101 = call i32 @exit(i32 %100)
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"
@O_RDWR = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [7 x i8] c"fchown\00"
@.str.6 = [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 = load i32, i32* @O_RDWR
%53 = tail call i32 @open(i8* %51, i32 %52)
%54 = icmp slt i32 %53, 0
br i1 %54, label %63, label %55
55: ; preds = %45
%56 = tail call i64 @fchown(i32 %53, i32 %28, i32 %46)
%57 = icmp slt i64 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %55
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, 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
63: ; preds = %55, %45
%64 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %45 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %55 ]
%65 = tail call i32 @perror(i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
%67 = tail call i32 @exit(i32 %66)
unreachable
}