#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 */

/* Variables and functions */
scalar_t__ chown(char *, int, int);
int /*<<< orphan*/ exit(int);
int getgid();
int getgroupid(char *);
int getuid();
int getuserid(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ strcmp(char *, char *);
int file_chown(int debug, char filename[200], char owner[200],
               char group[200]) {
  int uid, gid;
  if (strcmp(owner, "0") == 0)
    uid = getuid();
  else
    uid = getuserid(owner);
  if (strcmp(group, "0") == 0)
    gid = getgid();
  else
    gid = getgroupid(group);
  if (debug == 0) {
    printf("changing file owner to: %d\n", uid);
    printf("changing file group to: %d\n", gid);
  }
  if (chown(filename, uid, gid)) {
    perror("Error: chown failed");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<file_chown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11d0 <file_chown+0x40>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x24(%rbp)
jmp    11dc <file_chown+0x4c>
mov    -0x18(%rbp),%rdi
call   1090 <getuserid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1205 <file_chown+0x75>
mov    $0x0,%al
call   1070 <getgid@plt>
mov    %eax,-0x28(%rbp)
jmp    1211 <file_chown+0x81>
mov    -0x20(%rbp),%rdi
call   1060 <getgroupid@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1239 <file_chown+0xa9>
mov    -0x24(%rbp),%esi
lea    0xddd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%esi
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1080 <chown@plt>
cmp    $0x0,%rax
je     1268 <file_chown+0xd8>
lea    0xde1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_chown>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe56(%rip),%rsi
mov    %rdx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11c1 <file_chown+0x31>
mov    %rbx,%rdi
call   1090 <getuserid@plt>
jmp    11c8 <file_chown+0x38>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ebx
lea    0xe2f(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11e8 <file_chown+0x58>
mov    %rbp,%rdi
call   1060 <getgroupid@plt>
jmp    11ef <file_chown+0x5f>
xor    %eax,%eax
call   1070 <getgid@plt>
mov    %eax,%ebp
test   %r15d,%r15d
jne    1212 <file_chown+0x82>
lea    0xe05(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0xe13(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1080 <chown@plt>
test   %rax,%rax
jne    122e <file_chown+0x9e>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@.str.1 = [28 x i8] c"changing file owner to: %d\0A\00"
@.str.2 = [28 x i8] c"changing file group to: %d\0A\00"
@.str.3 = [20 x i8] c"Error: chown failed\00"
define i32 @file_chown(i32 %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%12 = load i8*, i8** %8
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = call i32 (...) @getuid()
store i32 %16, i32* %10
br label %20
17: ; preds = %4
%18 = load i8*, i8** %8
%19 = call i32 @getuserid(i8* %18)
store i32 %19, i32* %10
br label %20
20: ; preds = %17, %15
%21 = load i8*, i8** %9
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (...) @getgid()
store i32 %25, i32* %11
br label %29
26: ; preds = %20
%27 = load i8*, i8** %9
%28 = call i32 @getgroupid(i8* %27)
store i32 %28, i32* %11
br label %29
29: ; preds = %26, %24
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* %10
%34 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %11
%36 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %32, %29
%38 = load i8*, i8** %7
%39 = load i32, i32* %10
%40 = load i32, i32* %11
%41 = call i64 @chown(i8* %38, i32 %39, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %37
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
@.str.1 = [28 x i8] c"changing file owner to: %d\0A\00"
@.str.2 = [28 x i8] c"changing file group to: %d\0A\00"
@.str.3 = [20 x i8] c"Error: chown failed\00"
define i32 @file_chown(i32 %0, i8* nocapture %1, i8* %2, i8* %3) {
%5 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 (...) @getuid()
br label %11
9: ; preds = %4
%10 = tail call i32 @getuserid(i8* %2)
br label %11
11: ; preds = %9, %7
%12 = phi i32 [ %8, %7 ], [ %10, %9 ]
%13 = tail call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 (...) @getgid()
br label %19
17: ; preds = %11
%18 = tail call i32 @getgroupid(i8* %3)
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %16, %15 ], [ %18, %17 ]
%21 = icmp eq i32 %0, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %25
25: ; preds = %22, %19
%26 = tail call i64 @chown(i8* %1, i32 %12, i32 %20)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %25
ret i32 undef
}