#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;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ endpwent();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, ...);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ *open_memstream(char **, size_t *);
int /*<<< orphan*/ printf(char *, ...);
int scan_msg(int, int *, int /*<<< orphan*/ *);
int scan_posix_mqueue(int, int *, int /*<<< orphan*/ *);
int scan_sem(int, int *, int /*<<< orphan*/ *);
int scan_shm(int, int *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  struct passwd *pwd = getpwnam("ejexec");
  endpwent();
  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();
  }
  char *rep_s = NULL;
  size_t rep_z = 0;
  FILE *rep_f = open_memstream(&rep_s, &rep_z);
  int count = 0;
  int retval = scan_msg(pwd->pw_uid, &count, rep_f) |
               scan_sem(pwd->pw_uid, &count, rep_f) |
               scan_shm(pwd->pw_uid, &count, rep_f) |
               scan_posix_mqueue(pwd->pw_uid, &count, rep_f);
  fclose(rep_f);
  rep_f = NULL;
  if (count > 0) {
    printf("System V IPC scan found the following objects:\n");
    printf("%s", rep_s);
    printf("Total %d objects found\n", count);
  }
  return retval;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe33(%rip),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <endpwent@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1214 <main+0x64>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe08(%rip),%rsi
lea    0xdfa(%rip),%rcx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jg     1255 <main+0xa5>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8
lea    0xde5(%rip),%rsi
lea    0xdb9(%rip),%rcx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edi
mov    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rsi
call   10c0 <scan_msg@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edi
mov    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rsi
call   10d0 <scan_sem@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edi
mov    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rsi
call   1090 <scan_shm@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %eax,%edi
mov    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rsi
call   10a0 <scan_posix_mqueue@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    1342 <main+0x192>
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x34(%rbp),%esi
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
lea    0xe28(%rip),%rdi
call   1080 <getpwnam@plt>
mov    %rax,%r12
xor    %eax,%eax
call   10c0 <endpwent@plt>
test   %r12,%r12
je     12cf <main+0x10f>
mov    (%r12),%r8
test   %r8,%r8
jle    12f7 <main+0x137>
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <open_memstream@plt>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
mov    (%r12),%edi
lea    0x4(%rsp),%r13
mov    %r13,%rsi
mov    %rax,%rdx
call   10d0 <scan_msg@plt>
mov    %eax,%r14d
mov    (%r12),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10e0 <scan_sem@plt>
mov    %eax,%ebp
mov    (%r12),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10a0 <scan_shm@plt>
mov    %eax,%r15d
mov    (%r12),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10b0 <scan_posix_mqueue@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1060 <fclose@plt>
cmpl   $0x0,0x4(%rsp)
jle    12b5 <main+0xf5>
lea    0xdcf(%rip),%rdi
call   1050 <puts@plt>
mov    0x8(%rsp),%rsi
lea    0xda3(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x4(%rsp),%esi
lea    0xd94(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
or     %r14d,%ebp
or     %r15d,%ebp
or     %r12d,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd25(%rip),%rsi
lea    0xd17(%rip),%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd1b(%rip),%rsi
lea    0xcef(%rip),%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.passwd = type { i64 }
@.str = [7 x i8] c"ejexec\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"%s: user '%s' does not exist\0A\00"
@.str.2 = [26 x i8] c"%s: user '%s' has uid %d\0A\00"
@.str.3 = [48 x i8] c"System V IPC scan found the following objects:\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [24 x i8] c"Total %d objects found\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store %struct.passwd* %12, %struct.passwd** %6
%13 = call i32 (...) @endpwent()
%14 = load %struct.passwd*, %struct.passwd** %6
%15 = icmp ne %struct.passwd* %14, null
br i1 %15, label %23, label %16
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%22 = call i32 (...) @abort()
unreachable
23: ; preds = %2
%24 = load %struct.passwd*, %struct.passwd** %6
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = load i8*, i8** %31
%33 = load %struct.passwd*, %struct.passwd** %6
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %35)
%37 = call i32 (...) @abort()
unreachable
38: ; preds = %23
store i8* null, i8** %7
store i64 0, i64* %8
%39 = call i32* @open_memstream(i8** %7, i64* %8)
store i32* %39, i32** %9
store i32 0, i32* %10
%40 = load %struct.passwd*, %struct.passwd** %6
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = trunc i64 %42 to i32
%44 = load i32*, i32** %9
%45 = call i32 @scan_msg(i32 %43, i32* %10, i32* %44)
%46 = load %struct.passwd*, %struct.passwd** %6
%47 = getelementptr inbounds %struct.passwd, %struct.passwd* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = trunc i64 %48 to i32
%50 = load i32*, i32** %9
%51 = call i32 @scan_sem(i32 %49, i32* %10, i32* %50)
%52 = or i32 %45, %51
%53 = load %struct.passwd*, %struct.passwd** %6
%54 = getelementptr inbounds %struct.passwd, %struct.passwd* %53, i32 0, i32 0
%55 = load i64, i64* %54
%56 = trunc i64 %55 to i32
%57 = load i32*, i32** %9
%58 = call i32 @scan_shm(i32 %56, i32* %10, i32* %57)
%59 = or i32 %52, %58
%60 = load %struct.passwd*, %struct.passwd** %6
%61 = getelementptr inbounds %struct.passwd, %struct.passwd* %60, i32 0, i32 0
%62 = load i64, i64* %61
%63 = trunc i64 %62 to i32
%64 = load i32*, i32** %9
%65 = call i32 @scan_posix_mqueue(i32 %63, i32* %10, i32* %64)
%66 = or i32 %59, %65
store i32 %66, i32* %11
%67 = load i32*, i32** %9
%68 = call i32 @fclose(i32* %67)
store i32* null, i32** %9
%69 = load i32, i32* %10
%70 = icmp sgt i32 %69, 0
br i1 %70, label %71, label %77
71: ; preds = %38
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
%73 = load i8*, i8** %7
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %73)
%75 = load i32, i32* %10
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %75)
br label %77
77: ; preds = %71, %38
%78 = load i32, i32* %11
ret i32 %78
}

LLVM IR (-O2):
%struct.passwd = type { i64 }
@.str = [7 x i8] c"ejexec\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"%s: user '%s' does not exist\0A\00"
@.str.2 = [26 x i8] c"%s: user '%s' has uid %d\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [24 x i8] c"Total %d objects found\0A\00"
@str = [47 x i8] c"System V IPC scan found the following objects:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 (...) @endpwent()
%8 = icmp eq %struct.passwd* %6, null
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %1
%12 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 (...) @abort()
unreachable
14: ; preds = %2
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 0
%16 = load i64, i64* %15
%17 = icmp slt i64 %16, 1
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %1
%21 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %16)
%22 = tail call i32 (...) @abort()
unreachable
23: ; preds = %14
%24 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%25 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%26 = call i32* @open_memstream(i8** %3, i64* %4)
%27 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%28 = load i64, i64* %15
%29 = trunc i64 %28 to i32
%30 = call i32 @scan_msg(i32 %29, i32* %5, i32* %26)
%31 = load i64, i64* %15
%32 = trunc i64 %31 to i32
%33 = call i32 @scan_sem(i32 %32, i32* %5, i32* %26)
%34 = load i64, i64* %15
%35 = trunc i64 %34 to i32
%36 = call i32 @scan_shm(i32 %35, i32* %5, i32* %26)
%37 = load i64, i64* %15
%38 = trunc i64 %37 to i32
%39 = call i32 @scan_posix_mqueue(i32 %38, i32* %5, i32* %26)
%40 = call i32 @fclose(i32* %26)
%41 = load i32, i32* %5
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %23
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%45 = load i8*, i8** %3
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%47 = load i32, i32* %5
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %43, %23
%50 = or i32 %33, %30
%51 = or i32 %50, %36
%52 = or i32 %51, %39
ret i32 %52
}