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

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int, int);
int pidof(char *);
char *strchr(char const *, char);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, long);
pid_t get_pidof(const char *process) {
  char name[2048];
  const char *p;
  long len;
  if (!process || (strlen(process) == 0)) {
    return -1;
  }
  memset(name, 0x00, sizeof(name));
  p = strchr(process, ' ');
  len = (p == NULL) ? (long)sizeof(name) : (p - process);
  strncpy(name, process, len);
  return pidof(name);
}


x86 assembly (clang, -O0):
<get_pidof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117d <get_pidof+0x2d>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1189 <get_pidof+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <get_pidof+0xc9>
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x818(%rbp)
cmpq   $0x0,-0x818(%rbp)
jne    11d0 <get_pidof+0x80>
mov    $0x800,%eax
mov    %rax,-0x828(%rbp)
jmp    11e5 <get_pidof+0x95>
mov    -0x818(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x828(%rbp)
mov    -0x828(%rbp),%rax
mov    %rax,-0x820(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x820(%rbp),%rdx
call   1030 <strncpy@plt>
lea    -0x810(%rbp),%rdi
call   1070 <pidof@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pidof>:
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     11a0 <get_pidof+0x60>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     11a0 <get_pidof+0x60>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
sub    %rbx,%rcx
test   %rax,%rax
mov    $0x800,%edx
cmovne %rcx,%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    %r14,%rdi
call   1060 <pidof@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @get_pidof(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [2048 x i8]
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @strlen(i8* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9, %1
store i32 -1, i32* %2
br label %36
14: ; preds = %9
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 2048)
%17 = load i8*, i8** %3
%18 = call i8* @strchr(i8* %17, i8 signext 32)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %14
br label %28
22: ; preds = %14
%23 = load i8*, i8** %5
%24 = load i8*, i8** %3
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
br label %28
28: ; preds = %22, %21
%29 = phi i64 [ 2048, %21 ], [ %27, %22 ]
store i64 %29, i64* %6
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%31 = load i8*, i8** %3
%32 = load i64, i64* %6
%33 = call i32 @strncpy(i8* %30, i8* %31, i64 %32)
%34 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%35 = call i32 @pidof(i8* %34)
store i32 %35, i32* %2
br label %36
36: ; preds = %28, %13
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
define i32 @get_pidof(i8* %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %18, label %5
5: ; preds = %1
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %5
%9 = call i32 @memset(i8* %3, i32 0, i32 2048)
%10 = call i8* @strchr(i8* %0, i8 signext 32)
%11 = icmp eq i8* %10, null
%12 = ptrtoint i8* %10 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = select i1 %11, i64 2048, i64 %14
%16 = call i32 @strncpy(i8* %3, i8* %0, i64 %15)
%17 = call i32 @pidof(i8* %3)
br label %18
18: ; preds = %1, %5, %8
%19 = phi i32 [ %17, %8 ], [ -1, %5 ], [ -1, %1 ]
ret i32 %19
}