#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 scalar_t__ uid_t;
struct passwd {
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
scalar_t__ atoi(char *);
scalar_t__ chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ err_log(int, int, char *, ...);
struct passwd *getpwnam(char *);
struct passwd *getpwuid(scalar_t__);
scalar_t__ getuid();
scalar_t__ initgroups(char *, int /*<<< orphan*/);
extern int os_chown(char *path, char *user) {
  struct passwd *pw = NULL;
  err_log(9, 0x80000000, "Chowning %s to %s\n", path, user);
  if (getuid() == (uid_t)0) {
    if (atoi(user)) {
      pw = getpwuid((uid_t)atoi(user));
    } else {
      pw = getpwnam(user);
    }
    if (pw) {
      if (initgroups(user, pw->pw_gid) != 0 ||
          chown(path, pw->pw_uid, pw->pw_gid) != 0) {
        err_log(1, 0x80000000, "Couldn't chown %s, gid=%d, uid=%d\n", user,
                pw->pw_gid, pw->pw_uid);
        return 0;
      }
    } else {
      err_log(1, 0x80000000, "Couldn't lookup user %s for chown\n", user);
      return 0;
    }
  }
  err_log(9, 0x80000000, "Success!\n");
  return 1;
}


x86 assembly (clang, -O0):
<os_chown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x9,%edi
mov    $0x80000000,%esi
lea    0xe5f(%rip),%rdx
mov    $0x0,%al
call   1040 <err_log@plt>
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
jne    12a1 <os_chown+0x131>
mov    -0x18(%rbp),%rdi
call   1080 <atoi@plt>
cmp    $0x0,%rax
je     11e6 <os_chown+0x76>
mov    -0x18(%rbp),%rdi
call   1080 <atoi@plt>
mov    %rax,%rdi
call   1030 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
jmp    11f3 <os_chown+0x83>
mov    -0x18(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1274 <os_chown+0x104>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
call   1090 <initgroups@plt>
cmp    $0x0,%rax
jne    1238 <os_chown+0xc8>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <chown@plt>
cmp    $0x0,%rax
je     126f <os_chown+0xff>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x20(%rbp),%rax
mov    (%rax),%r9d
mov    $0x1,%edi
mov    $0x80000000,%esi
lea    0xdb7(%rip),%rdx
mov    $0x0,%al
call   1040 <err_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c0 <os_chown+0x150>
jmp    129c <os_chown+0x12c>
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
mov    $0x80000000,%esi
lea    0xdad(%rip),%rdx
mov    $0x0,%al
call   1040 <err_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c0 <os_chown+0x150>
jmp    12a1 <os_chown+0x131>
mov    $0x9,%edi
mov    $0x80000000,%esi
lea    0xda7(%rip),%rdx
mov    $0x0,%al
call   1040 <err_log@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_chown>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe7e(%rip),%rdx
mov    $0x9,%edi
mov    $0x80000000,%esi
mov    %r14,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   1040 <err_log@plt>
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     11c7 <os_chown+0x57>
lea    0xead(%rip),%rdx
mov    $0x9,%edi
mov    $0x80000000,%esi
xor    %eax,%eax
call   1040 <err_log@plt>
mov    $0x1,%ebx
jmp    1253 <os_chown+0xe3>
mov    %r15,%rdi
call   1080 <atoi@plt>
test   %rax,%rax
je     11de <os_chown+0x6e>
mov    %rax,%rdi
call   1030 <getpwuid@plt>
jmp    11e6 <os_chown+0x76>
mov    %r15,%rdi
call   1060 <getpwnam@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1236 <os_chown+0xc6>
mov    0x4(%rbx),%esi
mov    %r15,%rdi
call   1090 <initgroups@plt>
test   %rax,%rax
jne    1210 <os_chown+0xa0>
mov    (%rbx),%esi
mov    0x4(%rbx),%edx
mov    %r14,%rdi
call   1070 <chown@plt>
test   %rax,%rax
je     11a5 <os_chown+0x35>
mov    (%rbx),%r9d
mov    0x4(%rbx),%r8d
lea    0xdf5(%rip),%rdx
xor    %ebx,%ebx
mov    $0x1,%edi
mov    $0x80000000,%esi
mov    %r15,%rcx
xor    %eax,%eax
call   1040 <err_log@plt>
jmp    1253 <os_chown+0xe3>
lea    0xdf9(%rip),%rdx
xor    %ebx,%ebx
mov    $0x1,%edi
mov    $0x80000000,%esi
mov    %r15,%rcx
xor    %eax,%eax
call   1040 <err_log@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [19 x i8] c"Chowning %s to %s\0A\00"
@.str.1 = [35 x i8] c"Couldn't chown %s, gid=%d, uid=%d\0A\00"
@.str.2 = [35 x i8] c"Couldn't lookup user %s for chown\0A\00"
@.str.3 = [10 x i8] c"Success!\0A\00"
define i32 @os_chown(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.passwd*
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.passwd* null, %struct.passwd** %6
%7 = load i8*, i8** %4
%8 = load i8*, i8** %5
%9 = call i32 (i32, i32, i8*, ...) @err_log(i32 9, i32 -2147483648, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %8)
%10 = call i64 (...) @getuid()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %57
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i64 @atoi(i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = call i64 @atoi(i8* %17)
%19 = call %struct.passwd* @getpwuid(i64 %18)
store %struct.passwd* %19, %struct.passwd** %6
br label %23
20: ; preds = %12
%21 = load i8*, i8** %5
%22 = call %struct.passwd* @getpwnam(i8* %21)
store %struct.passwd* %22, %struct.passwd** %6
br label %23
23: ; preds = %20, %16
%24 = load %struct.passwd*, %struct.passwd** %6
%25 = icmp ne %struct.passwd* %24, null
br i1 %25, label %26, label %53
26: ; preds = %23
%27 = load i8*, i8** %5
%28 = load %struct.passwd*, %struct.passwd** %6
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i64 @initgroups(i8* %27, i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %43, label %33
33: ; preds = %26
%34 = load i8*, i8** %4
%35 = load %struct.passwd*, %struct.passwd** %6
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load %struct.passwd*, %struct.passwd** %6
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = call i64 @chown(i8* %34, i32 %37, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %52
43: ; preds = %33, %26
%44 = load i8*, i8** %5
%45 = load %struct.passwd*, %struct.passwd** %6
%46 = getelementptr inbounds %struct.passwd, %struct.passwd* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = load %struct.passwd*, %struct.passwd** %6
%49 = getelementptr inbounds %struct.passwd, %struct.passwd* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 -2147483648, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %44, i32 %47, i32 %50)
store i32 0, i32* %3
br label %59
52: ; preds = %33
br label %56
53: ; preds = %23
%54 = load i8*, i8** %5
%55 = call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 -2147483648, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %54)
store i32 0, i32* %3
br label %59
56: ; preds = %52
br label %57
57: ; preds = %56, %2
%58 = call i32 (i32, i32, i8*, ...) @err_log(i32 9, i32 -2147483648, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %59
59: ; preds = %57, %53, %43
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [19 x i8] c"Chowning %s to %s\0A\00"
@.str.1 = [35 x i8] c"Couldn't chown %s, gid=%d, uid=%d\0A\00"
@.str.2 = [35 x i8] c"Couldn't lookup user %s for chown\0A\00"
@.str.3 = [10 x i8] c"Success!\0A\00"
define i32 @os_chown(i8* %0, i8* %1) {
%3 = tail call i32 (i32, i32, i8*, ...) @err_log(i32 9, i32 -2147483648, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%4 = tail call i64 (...) @getuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %34
6: ; preds = %2
%7 = tail call i64 @atoi(i8* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call %struct.passwd* @getpwuid(i64 %7)
br label %13
11: ; preds = %6
%12 = tail call %struct.passwd* @getpwnam(i8* %1)
br label %13
13: ; preds = %11, %9
%14 = phi %struct.passwd* [ %10, %9 ], [ %12, %11 ]
%15 = icmp eq %struct.passwd* %14, null
br i1 %15, label %32, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i64 @initgroups(i8* %1, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* %17
%25 = tail call i64 @chown(i8* %0, i32 %23, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %21, %16
%28 = load i32, i32* %17
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 0
%30 = load i32, i32* %29
%31 = tail call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 -2147483648, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %28, i32 %30)
br label %36
32: ; preds = %13
%33 = tail call i32 (i32, i32, i8*, ...) @err_log(i32 1, i32 -2147483648, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %36
34: ; preds = %21, %2
%35 = tail call i32 (i32, i32, i8*, ...) @err_log(i32 9, i32 -2147483648, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %32, %27
%37 = phi i32 [ 0, %27 ], [ 1, %34 ], [ 0, %32 ]
ret i32 %37
}