#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 fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int getzpid(char proc[]) {
  char buffer[16], command[64];
  FILE *finput;
  int fd_finput;
  int i, j, k = strlen(proc), n;
  for (i = k; i > 0; i--) {
    if (proc[i] == '/') {
      for (j = 0; i < k; i++, j++)
        buffer[j] = proc[i + 1];
      break;
    }
  }
  sprintf(command, "pgrep %s | head -1", buffer);
  buffer[0] = '\0';
  if ((finput = popen(command, "r")) == NULL) {
  }
  fd_finput = fileno(finput);
  if ((n = read(fd_finput, buffer, 15)) > 0)
    buffer[n] = '\0';
  pclose(finput);
  return atoi(buffer);
}


x86 assembly (clang, -O0):
<getzpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%eax
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jle    120a <getzpid+0x9a>
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11f7 <getzpid+0x87>
movl   $0x0,-0x74(%rbp)
mov    -0x70(%rbp),%eax
cmp    -0x78(%rbp),%eax
jge    11f2 <getzpid+0x82>
mov    -0x8(%rbp),%rax
mov    -0x70(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
movslq -0x74(%rbp),%rax
mov    %cl,-0x20(%rbp,%rax,1)
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    11b7 <getzpid+0x47>
jmp    120a <getzpid+0x9a>
jmp    11fc <getzpid+0x8c>
mov    -0x70(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x70(%rbp)
jmp    1191 <getzpid+0x21>
lea    -0x60(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    0xde7(%rip),%rsi
call   1090 <sprintf@plt>
movb   $0x0,-0x20(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
jne    1245 <getzpid+0xd5>
jmp    1245 <getzpid+0xd5>
mov    -0x68(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x6c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xf,%edx
call   1050 <read@plt>
mov    %eax,-0x7c(%rbp)
cmp    $0x0,%eax
jle    1277 <getzpid+0x107>
movslq -0x7c(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
mov    -0x68(%rbp),%rdi
call   1040 <pclose@plt>
lea    -0x20(%rbp),%rdi
call   1080 <atoi@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getzpid>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    11d5 <getzpid+0x55>
mov    $0x1,%edx
mov    %eax,%ecx
nopw   0x0(%rax,%rax,1)
mov    %ecx,%esi
cmpb   $0x2f,(%rbx,%rsi,1)
je     11b8 <getzpid+0x38>
lea    -0x1(%rcx),%esi
add    $0xffffffffffffffff,%rdx
cmp    $0x1,%ecx
mov    %esi,%ecx
jg     11a0 <getzpid+0x20>
jmp    11d5 <getzpid+0x55>
cmp    %eax,%ecx
jge    11d5 <getzpid+0x55>
mov    %eax,%eax
add    %rax,%rbx
add    %rdx,%rbx
neg    %edx
add    $0x1,%rdx
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
lea    0xe24(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rsp,%r14
mov    %rbx,%rdi
mov    %r14,%rdx
call   10a0 <sprintf@plt>
movb   $0x0,(%rsp)
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   1080 <popen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xf,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1226 <getzpid+0xa6>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rbx,%rdi
call   1040 <pclose@plt>
mov    %rsp,%rdi
call   1090 <atoi@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [19 x i8] c"pgrep %s | head -1\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getzpid(i8* %0) {
%2 = alloca i8*
%3 = alloca [16 x i8]
%4 = alloca [64 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %2
%11 = load i8*, i8** %2
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
store i32 %13, i32* %7
br label %14
14: ; preds = %47, %1
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %50
17: ; preds = %14
%18 = load i8*, i8** %2
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 47
br i1 %24, label %25, label %46
25: ; preds = %17
store i32 0, i32* %8
br label %26
26: ; preds = %40, %25
%27 = load i32, i32* %7
%28 = load i32, i32* %9
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = load i8*, i8** %2
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i8, i8* %35
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %38
store i8 %36, i8* %39
br label %40
40: ; preds = %30
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %26
45: ; preds = %26
br label %50
46: ; preds = %17
br label %47
47: ; preds = %46
%48 = load i32, i32* %7
%49 = add nsw i32 %48, -1
store i32 %49, i32* %7
br label %14
50: ; preds = %45, %14
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%52 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%53 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %52)
%54 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
store i8 0, i8* %54
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%56 = call i32* @popen(i8* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %56, i32** %5
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %59
58: ; preds = %50
br label %59
59: ; preds = %58, %50
%60 = load i32*, i32** %5
%61 = call i32 @fileno(i32* %60)
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%64 = call i32 @read(i32 %62, i8* %63, i32 15)
store i32 %64, i32* %10
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %59
%67 = load i32, i32* %10
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %68
store i8 0, i8* %69
br label %70
70: ; preds = %66, %59
%71 = load i32*, i32** %5
%72 = call i32 @pclose(i32* %71)
%73 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%74 = call i32 @atoi(i8* %73)
ret i32 %74
}

LLVM IR (-O2):
@.str = [19 x i8] c"pgrep %s | head -1\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getzpid(i8* %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %1, %25
%10 = phi i32 [ %26, %25 ], [ %7, %1 ]
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 47
br i1 %14, label %15, label %25
15: ; preds = %9
%16 = icmp slt i32 %10, %7
br i1 %16, label %17, label %28
17: ; preds = %15
%18 = zext i32 %10 to i64
%19 = add nuw nsw i64 %18, 1
%20 = getelementptr i8, i8* %0, i64 %19
%21 = xor i32 %10, -1
%22 = add i32 %7, %21
%23 = zext i32 %22 to i64
%24 = add nuw nsw i64 %23, 1
br label %28
25: ; preds = %9
%26 = add nsw i32 %10, -1
%27 = icmp sgt i32 %10, 1
br i1 %27, label %9, label %28
28: ; preds = %25, %17, %1, %15
%29 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %5)
store i8 0, i8* %5
%30 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @fileno(i32* %30)
%32 = call i32 @read(i32 %31, i8* %5, i32 15)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = zext i32 %32 to i64
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %34, %28
%38 = call i32 @pclose(i32* %30)
%39 = call i32 @atoi(i8* %5)
ret i32 %39
}