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

/* Variables and functions */
int chmod(char const *, int /*<<< orphan*/);
int chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, ...);
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int chperm(const char *path, const char *user, mode_t mode) {
  struct passwd *p;
  if (chmod(path, mode) == -1) {
    fprintf(
        stderr,
        "%s: "
        "chperm: %s"
        " (%s:%d)\n",
        "warning", strerror(errno),
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c",
        504);
    ;
    ;
    return -1;
  }
  if (user) {
    errno = 0;
    p = getpwnam(user);
    if (!p) {
      if (errno) {
        fprintf(stderr,
                "%s: "
                "getpwnam: %s"
                " (%s:%d)\n",
                "warning", strerror(errno),
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/"
                "src/common.c",
                512);
        ;
        ;
      } else {
        fprintf(stderr,
                "%s: "
                "getpwnam: not found"
                " (%s:%d)\n",
                "warning",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/"
                "src/common.c",
                514);
        ;
        ;
      }
      return -1;
    }
    if (chown(path, p->pw_uid, p->pw_gid) == -1) {
      fprintf(
          stderr,
          "%s: "
          "chown: %s"
          " (%s:%d)\n",
          "warning", strerror(errno),
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c",
          519);
      ;
      ;
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<chperm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <chmod@plt>
cmp    $0xffffffff,%eax
jne    11c7 <chperm+0x77>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rcx
lea    0xe60(%rip),%rsi
lea    0xe71(%rip),%rdx
lea    0xe72(%rip),%r8
mov    $0x1f8,%r9d
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <chperm+0x1b2>
cmpq   $0x0,-0x18(%rbp)
je     12fb <chperm+0x1ab>
mov    0x2dff(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1288 <chperm+0x138>
mov    0x2dd9(%rip),%rax
cmpq   $0x0,(%rax)
je     1251 <chperm+0x101>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rcx
lea    0xe72(%rip),%rsi
lea    0xde0(%rip),%rdx
lea    0xde1(%rip),%r8
mov    $0x200,%r9d
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    127c <chperm+0x12c>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
lea    0xdb0(%rip),%rdx
lea    0xdb1(%rip),%rcx
mov    $0x202,%r8d
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <chperm+0x1b2>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
jne    12f6 <chperm+0x1a6>
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d1e(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rcx
lea    0xe0f(%rip),%rsi
lea    0xd42(%rip),%rdx
lea    0xd43(%rip),%r8
mov    $0x207,%r9d
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <chperm+0x1b2>
jmp    12fb <chperm+0x1ab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chperm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %edx,%esi
call   1060 <chmod@plt>
cmp    $0xffffffff,%eax
je     11dd <chperm+0x8d>
xor    %r14d,%r14d
test   %rbp,%rbp
je     125a <chperm+0x10a>
mov    0x2e5c(%rip),%r15
movq   $0x0,(%r15)
mov    %rbp,%rdi
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1217 <chperm+0xc7>
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %rbx,%rdi
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
jne    125a <chperm+0x10a>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ebx
mov    (%r15),%rdi
call   1070 <strerror@plt>
lea    0xf1c(%rip),%rsi
lea    0xe4f(%rip),%rdx
lea    0xe50(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x207,%r9d
jmp    124d <chperm+0xfd>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ebx
mov    0x2deb(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xe04(%rip),%rsi
lea    0xe15(%rip),%rdx
lea    0xe16(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x1f8,%r9d
jmp    124d <chperm+0xfd>
mov    (%r15),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%ebx
test   %rdi,%rdi
je     1268 <chperm+0x118>
call   1070 <strerror@plt>
lea    0xe6f(%rip),%rsi
lea    0xddd(%rip),%rdx
lea    0xdde(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x200,%r9d
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4e(%rip),%rsi
lea    0xda2(%rip),%rdx
lea    0xda3(%rip),%rcx
mov    %ebx,%edi
mov    $0x202,%r8d
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1254 <chperm+0x104>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [24 x i8] c"%s: chperm: %s (%s:%d)\0A\00"
@.str.1 = [8 x i8] c"warning\00"
@errno = global i64 0
@.str.2 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c\00"
@.str.3 = [26 x i8] c"%s: getpwnam: %s (%s:%d)\0A\00"
@.str.4 = [33 x i8] c"%s: getpwnam: not found (%s:%d)\0A\00"
@.str.5 = [23 x i8] c"%s: chown: %s (%s:%d)\0A\00"
define i32 @chperm(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.passwd*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* %7
%11 = call i32 @chmod(i8* %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i64, i64* @errno
%16 = call i8* @strerror(i64 %15)
%17 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 504)
store i32 -1, i32* %4
br label %55
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %54
21: ; preds = %18
store i64 0, i64* @errno
%22 = load i8*, i8** %6
%23 = call %struct.passwd* @getpwnam(i8* %22)
store %struct.passwd* %23, %struct.passwd** %8
%24 = load %struct.passwd*, %struct.passwd** %8
%25 = icmp ne %struct.passwd* %24, null
br i1 %25, label %38, label %26
26: ; preds = %21
%27 = load i64, i64* @errno
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i64, i64* @errno
%32 = call i8* @strerror(i64 %31)
%33 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 512)
br label %37
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 514)
br label %37
37: ; preds = %34, %29
store i32 -1, i32* %4
br label %55
38: ; preds = %21
%39 = load i8*, i8** %5
%40 = load %struct.passwd*, %struct.passwd** %8
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = load %struct.passwd*, %struct.passwd** %8
%44 = getelementptr inbounds %struct.passwd, %struct.passwd* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @chown(i8* %39, i32 %42, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %53
48: ; preds = %38
%49 = load i32, i32* @stderr
%50 = load i64, i64* @errno
%51 = call i8* @strerror(i64 %50)
%52 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %51, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 519)
store i32 -1, i32* %4
br label %55
53: ; preds = %38
br label %54
54: ; preds = %53, %18
store i32 0, i32* %4
br label %55
55: ; preds = %54, %48, %37, %13
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [24 x i8] c"%s: chperm: %s (%s:%d)\0A\00"
@.str.1 = [8 x i8] c"warning\00"
@errno = global i64 0
@.str.2 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c\00"
@.str.3 = [26 x i8] c"%s: getpwnam: %s (%s:%d)\0A\00"
@.str.4 = [33 x i8] c"%s: getpwnam: not found (%s:%d)\0A\00"
@.str.5 = [23 x i8] c"%s: chown: %s (%s:%d)\0A\00"
define i32 @chperm(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32 @chmod(i8* %0, i32 %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = load i64, i64* @errno
%9 = tail call i8* @strerror(i64 %8)
%10 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 504)
br label %37
11: ; preds = %3
%12 = icmp eq i8* %1, null
br i1 %12, label %37, label %13
13: ; preds = %11
store i64 0, i64* @errno
%14 = tail call %struct.passwd* @getpwnam(i8* %1)
%15 = icmp eq %struct.passwd* %14, null
br i1 %15, label %16, label %25
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = icmp eq i64 %17, 0
%19 = load i32, i32* @stderr
br i1 %18, label %23, label %20
20: ; preds = %16
%21 = tail call i8* @strerror(i64 %17)
%22 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 512)
br label %37
23: ; preds = %16
%24 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 514)
br label %37
25: ; preds = %13
%26 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 0
%29 = load i32, i32* %28
%30 = tail call i32 @chown(i8* %0, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = load i64, i64* @errno
%35 = tail call i8* @strerror(i64 %34)
%36 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %35, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 519)
br label %37
37: ; preds = %11, %25, %20, %23, %32, %6
%38 = phi i32 [ -1, %6 ], [ -1, %32 ], [ -1, %23 ], [ -1, %20 ], [ 0, %25 ], [ 0, %11 ]
ret i32 %38
}