#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 {
  scalar_t__ pw_uid;
};
struct group {
  scalar_t__ gr_gid;
};

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ chown_rec(char *, int, int, int);
int /*<<< orphan*/ endgrent();
int /*<<< orphan*/ endpwent();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int getgid();
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int getuid();
int /*<<< orphan*/ safe_chown(char *, int, int, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc <= 1) {
    fprintf(stderr, "%s: wrong number of arguments\n", argv[0]);
    abort();
  }
  struct passwd *pwd = getpwnam("ejexec");
  struct group *grp = getgrnam("ejexec");
  endpwent();
  endgrent();
  if (!pwd) {
    fprintf(stderr, "%s: user '%s' does not exist\n", argv[0], "ejexec");
    abort();
  }
  if (pwd->pw_uid <= 0) {
    fprintf(stderr, "%s: user '%s' has uid %d\n", argv[0], "ejexec",
            pwd->pw_uid);
    abort();
  }
  if (!grp) {
    fprintf(stderr, "%s: group '%s' does not exist\n", argv[0], "ejexec");
    abort();
  }
  if (grp->gr_gid <= 0) {
    fprintf(stderr, "%s: group '%s' has gid %d\n", argv[0], "ejexec",
            grp->gr_gid);
    abort();
  }
  int my_uid = getuid();
  int my_gid = getgid();
  safe_chown(argv[1], my_uid, my_gid, pwd->pw_uid);
  chown_rec(argv[1], my_uid, my_gid, pwd->pw_uid);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jg     11de <main+0x3e>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1050 <abort@plt>
lea    0xe3a(%rip),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
lea    0xe2a(%rip),%rdi
call   1090 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10c0 <endpwent@plt>
mov    $0x0,%al
call   1030 <endgrent@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1243 <main+0xa3>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf8(%rip),%rsi
lea    0xdea(%rip),%rcx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1050 <abort@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jg     1284 <main+0xe4>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xdd5(%rip),%rsi
lea    0xda9(%rip),%rcx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1050 <abort@plt>
cmpq   $0x0,-0x20(%rbp)
jne    12bb <main+0x11b>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb8(%rip),%rsi
lea    0xd72(%rip),%rcx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1050 <abort@plt>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jg     12fc <main+0x15c>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
lea    0xd96(%rip),%rsi
lea    0xd31(%rip),%rcx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1050 <abort@plt>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   10b0 <getgid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%ecx
call   1040 <safe_chown@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%ecx
call   10a0 <chown_rec@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
cmp    $0x1,%edi
jle    1249 <main+0xa9>
lea    0xe64(%rip),%r15
mov    %r15,%rdi
call   1070 <getpwnam@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1090 <getgrnam@plt>
mov    %rax,%r15
xor    %eax,%eax
call   10c0 <endpwent@plt>
xor    %eax,%eax
call   1030 <endgrent@plt>
test   %r12,%r12
je     126a <main+0xca>
mov    (%r12),%r8
test   %r8,%r8
jle    127f <main+0xdf>
test   %r15,%r15
je     1294 <main+0xf4>
mov    (%r15),%r8
test   %r8,%r8
jle    12bc <main+0x11c>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10b0 <getgid@plt>
mov    %eax,%ebx
mov    0x8(%r14),%rdi
mov    (%r12),%ecx
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <safe_chown@plt>
mov    0x8(%r14),%rdi
mov    (%r12),%ecx
mov    %ebp,%esi
mov    %ebx,%edx
call   10a0 <chown_rec@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xda4(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1050 <abort@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xda9(%rip),%rsi
jmp    12a7 <main+0x107>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdb2(%rip),%rsi
jmp    12cf <main+0x12f>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdb7(%rip),%rsi
lea    0xd71(%rip),%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1050 <abort@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdae(%rip),%rsi
lea    0xd49(%rip),%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1050 <abort@plt>

LLVM IR (-O0):
%struct.passwd = type { i64 }
%struct.group = type { i64 }
@stderr = global i32 0
@.str = [31 x i8] c"%s: wrong number of arguments\0A\00"
@.str.1 = [7 x i8] c"ejexec\00"
@.str.2 = [30 x i8] c"%s: user '%s' does not exist\0A\00"
@.str.3 = [26 x i8] c"%s: user '%s' has uid %d\0A\00"
@.str.4 = [31 x i8] c"%s: group '%s' does not exist\0A\00"
@.str.5 = [27 x i8] c"%s: group '%s' has gid %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.passwd*
%6 = alloca %struct.group*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%9 = load i32, i32* %3
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %2
%19 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store %struct.passwd* %19, %struct.passwd** %5
%20 = call %struct.group* @getgrnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store %struct.group* %20, %struct.group** %6
%21 = call i32 (...) @endpwent()
%22 = call i32 (...) @endgrent()
%23 = load %struct.passwd*, %struct.passwd** %5
%24 = icmp ne %struct.passwd* %23, null
br i1 %24, label %32, label %25
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = load i8**, i8*** %4
%28 = getelementptr inbounds i8*, i8** %27, i64 0
%29 = load i8*, i8** %28
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %18
%33 = load %struct.passwd*, %struct.passwd** %5
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = icmp sle i64 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %4
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
%42 = load %struct.passwd*, %struct.passwd** %5
%43 = getelementptr inbounds %struct.passwd, %struct.passwd* %42, i32 0, i32 0
%44 = load i64, i64* %43
%45 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %41, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %44)
%46 = call i32 (...) @abort()
unreachable
47: ; preds = %32
%48 = load %struct.group*, %struct.group** %6
%49 = icmp ne %struct.group* %48, null
br i1 %49, label %57, label %50
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = load i8**, i8*** %4
%53 = getelementptr inbounds i8*, i8** %52, i64 0
%54 = load i8*, i8** %53
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %54, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%56 = call i32 (...) @abort()
unreachable
57: ; preds = %47
%58 = load %struct.group*, %struct.group** %6
%59 = getelementptr inbounds %struct.group, %struct.group* %58, i32 0, i32 0
%60 = load i64, i64* %59
%61 = icmp sle i64 %60, 0
br i1 %61, label %62, label %72
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = load i8**, i8*** %4
%65 = getelementptr inbounds i8*, i8** %64, i64 0
%66 = load i8*, i8** %65
%67 = load %struct.group*, %struct.group** %6
%68 = getelementptr inbounds %struct.group, %struct.group* %67, i32 0, i32 0
%69 = load i64, i64* %68
%70 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %66, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %69)
%71 = call i32 (...) @abort()
unreachable
72: ; preds = %57
%73 = call i32 (...) @getuid()
store i32 %73, i32* %7
%74 = call i32 (...) @getgid()
store i32 %74, i32* %8
%75 = load i8**, i8*** %4
%76 = getelementptr inbounds i8*, i8** %75, i64 1
%77 = load i8*, i8** %76
%78 = load i32, i32* %7
%79 = load i32, i32* %8
%80 = load %struct.passwd*, %struct.passwd** %5
%81 = getelementptr inbounds %struct.passwd, %struct.passwd* %80, i32 0, i32 0
%82 = load i64, i64* %81
%83 = trunc i64 %82 to i32
%84 = call i32 @safe_chown(i8* %77, i32 %78, i32 %79, i32 %83)
%85 = load i8**, i8*** %4
%86 = getelementptr inbounds i8*, i8** %85, i64 1
%87 = load i8*, i8** %86
%88 = load i32, i32* %7
%89 = load i32, i32* %8
%90 = load %struct.passwd*, %struct.passwd** %5
%91 = getelementptr inbounds %struct.passwd, %struct.passwd* %90, i32 0, i32 0
%92 = load i64, i64* %91
%93 = trunc i64 %92 to i32
%94 = call i32 @chown_rec(i8* %87, i32 %88, i32 %89, i32 %93)
ret i32 0
}

LLVM IR (-O2):
%struct.passwd = type { i64 }
%struct.group = type { i64 }
@stderr = global i32 0
@.str = [31 x i8] c"%s: wrong number of arguments\0A\00"
@.str.1 = [7 x i8] c"ejexec\00"
@.str.2 = [30 x i8] c"%s: user '%s' does not exist\0A\00"
@.str.3 = [26 x i8] c"%s: user '%s' has uid %d\0A\00"
@.str.4 = [31 x i8] c"%s: group '%s' does not exist\0A\00"
@.str.5 = [27 x i8] c"%s: group '%s' has gid %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %2
%10 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call %struct.group* @getgrnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 (...) @endpwent()
%13 = tail call i32 (...) @endgrent()
%14 = icmp eq %struct.passwd* %10, null
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %1
%18 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 (...) @abort()
unreachable
20: ; preds = %9
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 0
%22 = load i64, i64* %21
%23 = icmp slt i64 %22, 1
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %1
%27 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %22)
%28 = tail call i32 (...) @abort()
unreachable
29: ; preds = %20
%30 = icmp eq %struct.group* %11, null
br i1 %30, label %31, label %36
31: ; preds = %29
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %1
%34 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%35 = tail call i32 (...) @abort()
unreachable
36: ; preds = %29
%37 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 0
%38 = load i64, i64* %37
%39 = icmp slt i64 %38, 1
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %1
%43 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %38)
%44 = tail call i32 (...) @abort()
unreachable
45: ; preds = %36
%46 = tail call i32 (...) @getuid()
%47 = tail call i32 (...) @getgid()
%48 = getelementptr inbounds i8*, i8** %1, i64 1
%49 = load i8*, i8** %48
%50 = load i64, i64* %21
%51 = trunc i64 %50 to i32
%52 = tail call i32 @safe_chown(i8* %49, i32 %46, i32 %47, i32 %51)
%53 = load i8*, i8** %48
%54 = load i64, i64* %21
%55 = trunc i64 %54 to i32
%56 = tail call i32 @chown_rec(i8* %53, i32 %46, i32 %47, i32 %55)
ret i32 0
}