#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*/ 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*/ sprintf(char *, char *, char *);
int get_process_id(char *arg) {
  FILE *fp;
  char buf[80] = {0};
  int pid;
  sprintf(buf, "ps aux | grep %s | awk '{print $2}'", arg);
  fp = popen(buf, "r");
  if (fp) {
    fgets(buf, 10, fp);
    pid = atoi(buf);
  } else {
    pid = -1;
  }
  pclose(fp);
  return pid;
}


x86 assembly (clang, -O0):
<get_process_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1040 <memset@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe75(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d2 <get_process_id+0x72>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1050 <fgets@plt>
lea    -0x60(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x64(%rbp)
jmp    11d9 <get_process_id+0x79>
movl   $0xffffffff,-0x64(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <pclose@plt>
mov    -0x64(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_process_id>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%rdx
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe85(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
call   1050 <popen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11bc <get_process_id+0x6c>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
mov    %rbp,%rdi
call   1060 <atoi@plt>
mov    %eax,%ebp
jmp    11c1 <get_process_id+0x71>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <pclose@plt>
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"ps aux | grep %s | awk '{print $2}'\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_process_id(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [80 x i8]
%5 = alloca i32
store i8* %0, i8** %2
%6 = bitcast [80 x i8]* %4 to i8*
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %3
%12 = load i32*, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%16 = load i32*, i32** %3
%17 = call i32 @fgets(i8* %15, i32 10, i32* %16)
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %5
br label %21
20: ; preds = %1
store i32 -1, i32* %5
br label %21
21: ; preds = %20, %14
%22 = load i32*, i32** %3
%23 = call i32 @pclose(i32* %22)
%24 = load i32, i32* %5
ret i32 %24
}

LLVM IR (-O2):
@.str = [36 x i8] c"ps aux | grep %s | awk '{print $2}'\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_process_id(i8* %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i32* @popen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = call i32 @fgets(i8* %3, i32 10, i32* %5)
%9 = call i32 @atoi(i8* %3)
br label %10
10: ; preds = %1, %7
%11 = phi i32 [ %9, %7 ], [ -1, %1 ]
%12 = call i32 @pclose(i32* %5)
ret i32 %11
}