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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int procnum(char *proc_name) {
  char cmd[256], buff[64] = "";
  FILE *fd;
  snprintf(cmd, sizeof(cmd), "ps -eo comm | grep '%s' | wc -l", proc_name);
  if ((fd = popen(cmd, "r")) == NULL) {
    pclose(fd);
    return -1;
  }
  if (fgets(buff, sizeof(buff), fd) == NULL) {
    pclose(fd);
    return -1;
  }
  pclose(fd);
  return atoi(buff);
}


x86 assembly (clang, -O0):
<procnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe67(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x158(%rbp)
cmp    $0x0,%rax
jne    11da <procnum+0x7a>
mov    -0x158(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <procnum+0xcf>
lea    -0x150(%rbp),%rdi
mov    -0x158(%rbp),%rdx
mov    $0x40,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    1214 <procnum+0xb4>
mov    -0x158(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <procnum+0xcf>
mov    -0x158(%rbp),%rdi
call   1030 <pclose@plt>
lea    -0x150(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procnum>:
push   %r14
push   %rbx
sub    $0x148,%rsp
mov    %rdi,%rcx
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe86(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
lea    0xe8d(%rip),%rsi
mov    %rbx,%rdi
call   1060 <popen@plt>
test   %rax,%rax
je     11cd <procnum+0x7d>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <pclose@plt>
test   %r14,%r14
je     11d4 <procnum+0x84>
mov    %rsp,%rdi
call   1070 <atoi@plt>
jmp    11d9 <procnum+0x89>
xor    %edi,%edi
call   1030 <pclose@plt>
mov    $0xffffffff,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [32 x i8] c"ps -eo comm | grep '%s' | wc -l\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @procnum(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca [64 x i8]
%6 = alloca i32*
store i8* %0, i8** %3
%7 = bitcast [64 x i8]* %5 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%12 = call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32*, i32** %6
%16 = call i32 @pclose(i32* %15)
store i32 -1, i32* %2
br label %30
17: ; preds = %1
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32* @fgets(i8* %18, i32 64, i32* %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32*, i32** %6
%24 = call i32 @pclose(i32* %23)
store i32 -1, i32* %2
br label %30
25: ; preds = %17
%26 = load i32*, i32** %6
%27 = call i32 @pclose(i32* %26)
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %2
br label %30
30: ; preds = %25, %22, %14
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [32 x i8] c"ps -eo comm | grep '%s' | wc -l\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @procnum(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @pclose(i32* null)
br label %17
11: ; preds = %1
%12 = call i32* @fgets(i8* %5, i32 64, i32* %7)
%13 = icmp eq i32* %12, null
%14 = call i32 @pclose(i32* %7)
br i1 %13, label %17, label %15
15: ; preds = %11
%16 = call i32 @atoi(i8* %5)
br label %17
17: ; preds = %11, %15, %9
%18 = phi i32 [ -1, %9 ], [ %16, %15 ], [ -1, %11 ]
ret i32 %18
}