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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ sort_pids(int /*<<< orphan*/ *, size_t);
int sscanf(char *, char *, int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strstr(char *, char *);
size_t get_userpids_by_command(char *user, char *command, pid_t *pids,
                               size_t max_num) {
  FILE *ph;
  size_t count;
  char buf[500];
  char commandbuf[100];
  pid_t pid;
  snprintf(buf, 500, "ps -u %s -o \"%%p %%a\"", user);
  ph = popen(buf, "r");
  if (ph == NULL) {
    fprintf(stderr, "Function popen(%s) failed: %s\n", buf, strerror(errno));
    return 0;
  }
  count = 0;
  while (fgets(buf, 500, ph) != NULL) {
    if (count >= max_num) {
      break;
    }
    memset(commandbuf, 0, 100);
    if (sscanf(buf, "%d %99c", &pid, commandbuf) != 2) {
      continue;
    }
    if (strstr(commandbuf, command)) {
      pids[count] = pid;
      count++;
    }
  }
  pclose(ph);
  sort_pids(pids, count);
  return count;
}


x86 assembly (clang, -O0):
<get_userpids_by_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1f4,%esi
lea    0xe2e(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    124d <get_userpids_by_command+0xad>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2b4(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x2b0(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x2b4(%rbp),%edi
mov    -0x2b0(%rbp),%rdx
mov    %rax,%rcx
lea    0xddd(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1323 <get_userpids_by_command+0x183>
movq   $0x0,-0x38(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x1f4,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1305 <get_userpids_by_command+0x165>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     1287 <get_userpids_by_command+0xe7>
jmp    1305 <get_userpids_by_command+0x165>
lea    -0x2a0(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
lea    -0x230(%rbp),%rdi
lea    -0x2a0(%rbp),%rcx
lea    0xd88(%rip),%rsi
lea    -0x2a4(%rbp),%rdx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
je     12c9 <get_userpids_by_command+0x129>
jmp    1255 <get_userpids_by_command+0xb5>
lea    -0x2a0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     1300 <get_userpids_by_command+0x160>
mov    -0x2a4(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1255 <get_userpids_by_command+0xb5>
mov    -0x30(%rbp),%rdi
call   1030 <pclose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1080 <sort_pids@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_userpids_by_command>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x288,%rsp
mov    %rcx,%r12
mov    %rdx,%rbp
mov    %rsi,0x18(%rsp)
mov    %rdi,%rcx
lea    0xe3a(%rip),%rdx
lea    0x90(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1f4,%esi
call   1040 <snprintf@plt>
lea    0xe34(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <popen@plt>
test   %rax,%rax
je     128f <get_userpids_by_command+0xef>
mov    %rax,%rbx
mov    %rbp,0x10(%rsp)
xor    %r13d,%r13d
lea    0x90(%rsp),%rbp
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
mov    %rbp,%rdi
mov    $0x1f4,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1278 <get_userpids_by_command+0xd8>
cmp    %r12,%r13
jae    1278 <get_userpids_by_command+0xd8>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
lea    0xdf4(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    1210 <get_userpids_by_command+0x70>
mov    %r14,%rdi
mov    0x18(%rsp),%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     1210 <get_userpids_by_command+0x70>
mov    0xc(%rsp),%eax
mov    0x10(%rsp),%rcx
mov    %eax,(%rcx,%r13,4)
add    $0x1,%r13
jmp    1210 <get_userpids_by_command+0x70>
mov    %rbx,%rdi
call   1030 <pclose@plt>
mov    0x10(%rsp),%rdi
mov    %r13,%rsi
call   1080 <sort_pids@plt>
jmp    12c2 <get_userpids_by_command+0x122>
mov    0x2d4a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd6b(%rip),%rsi
lea    0x90(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x288,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"ps -u %s -o \22%%p %%a\22\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"Function popen(%s) failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [8 x i8] c"%d %99c\00"
define i64 @get_userpids_by_command(i8* %0, i8* %1, i32* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i64
%12 = alloca [500 x i8]
%13 = alloca [100 x i8]
%14 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i64 %3, i64* %9
%15 = getelementptr inbounds [500 x i8], [500 x i8]* %12, i64 0, i64 0
%16 = load i8*, i8** %6
%17 = call i32 @snprintf(i8* %15, i32 500, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [500 x i8], [500 x i8]* %12, i64 0, i64 0
%19 = call i32* @popen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %10
%20 = load i32*, i32** %10
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %28
22: ; preds = %4
%23 = load i32, i32* @stderr
%24 = getelementptr inbounds [500 x i8], [500 x i8]* %12, i64 0, i64 0
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %24, i8* %26)
store i64 0, i64* %5
br label %67
28: ; preds = %4
store i64 0, i64* %11
br label %29
29: ; preds = %59, %46, %28
%30 = getelementptr inbounds [500 x i8], [500 x i8]* %12, i64 0, i64 0
%31 = load i32*, i32** %10
%32 = call i32* @fgets(i8* %30, i32 500, i32* %31)
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %60
34: ; preds = %29
%35 = load i64, i64* %11
%36 = load i64, i64* %9
%37 = icmp uge i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34
br label %60
39: ; preds = %34
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%41 = call i32 @memset(i8* %40, i32 0, i32 100)
%42 = getelementptr inbounds [500 x i8], [500 x i8]* %12, i64 0, i64 0
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%44 = call i32 @sscanf(i8* %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %14, i8* %43)
%45 = icmp ne i32 %44, 2
br i1 %45, label %46, label %47
46: ; preds = %39
br label %29
47: ; preds = %39
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%49 = load i8*, i8** %7
%50 = call i64 @strstr(i8* %48, i8* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %47
%53 = load i32, i32* %14
%54 = load i32*, i32** %8
%55 = load i64, i64* %11
%56 = getelementptr inbounds i32, i32* %54, i64 %55
store i32 %53, i32* %56
%57 = load i64, i64* %11
%58 = add i64 %57, 1
store i64 %58, i64* %11
br label %59
59: ; preds = %52, %47
br label %29
60: ; preds = %38, %29
%61 = load i32*, i32** %10
%62 = call i32 @pclose(i32* %61)
%63 = load i32*, i32** %8
%64 = load i64, i64* %11
%65 = call i32 @sort_pids(i32* %63, i64 %64)
%66 = load i64, i64* %11
store i64 %66, i64* %5
br label %67
67: ; preds = %60, %22
%68 = load i64, i64* %5
ret i64 %68
}

LLVM IR (-O2):
@.str = [22 x i8] c"ps -u %s -o \22%%p %%a\22\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"Function popen(%s) failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [8 x i8] c"%d %99c\00"
define i64 @get_userpids_by_command(i8* %0, i8* %1, i32* %2, i64 %3) {
%5 = alloca [500 x i8]
%6 = alloca [100 x i8]
%7 = alloca i32
%8 = getelementptr inbounds [500 x i8], [500 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%10 = bitcast i32* %7 to i8*
%11 = call i32 @snprintf(i8* %8, i32 500, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = call i32* @popen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %34
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %8, i8* %17)
br label %41
19: ; preds = %37, %23
%20 = call i32* @fgets(i8* %8, i32 500, i32* %12)
%21 = icmp ne i32* %20, null
%22 = select i1 %21, i1 %36, i1 false
br i1 %22, label %23, label %38
23: ; preds = %19
%24 = call i32 @memset(i8* %9, i32 0, i32 100)
%25 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %7, i8* %9)
%26 = icmp eq i32 %25, 2
br i1 %26, label %27, label %19
27: ; preds = %23
%28 = call i64 @strstr(i8* %9, i8* %1)
%29 = icmp eq i64 %28, 0
br i1 %29, label %37, label %30
30: ; preds = %27
%31 = load i32, i32* %7
%32 = getelementptr inbounds i32, i32* %2, i64 %35
store i32 %31, i32* %32
%33 = add nuw i64 %35, 1
br label %34
34: ; preds = %4, %30
%35 = phi i64 [ %33, %30 ], [ 0, %4 ]
%36 = icmp ult i64 %35, %3
br label %37
37: ; preds = %34, %27
br label %19
38: ; preds = %19
%39 = call i32 @pclose(i32* %12)
%40 = call i32 @sort_pids(i32* %2, i64 %35)
br label %41
41: ; preds = %38, %14
%42 = phi i64 [ 0, %14 ], [ %35, %38 ]
ret i64 %42
}