#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*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int /*<<< orphan*/ *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
pid_t pidof(char *exec) {
  size_t length = 0, offset = 0;
  char *cmd_str = NULL, *pid_str = NULL;
  FILE *cmd = NULL;
  pid_t pid = 0;
  length = 41 + strlen(exec);
  if (strncmp(exec, "./", 2) == 0) {
    offset = 2;
    length -= 2;
  }
  cmd_str = malloc(sizeof(char) * (length + 1));
  sprintf(cmd_str, "ps axc | awk \"{if (\\$5==\\\"%s\\\") print \\$1}\"",
          exec + offset);
  cmd = popen(cmd_str, "r");
  length = 6;
  pid_str = malloc(sizeof(char) * (length + 1));
  fread(pid_str, 1, length, cmd);
  sscanf(pid_str, "%d", &pid);
  return pid;
}


x86 assembly (clang, -O0):
<pidof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x29,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe38(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11f0 <pidof+0x80>
movq   $0x2,-0x18(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
add    -0x18(%rbp),%rdx
lea    0xde9(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x30(%rbp)
movq   $0x6,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x28(%rbp),%rdi
lea    0xdbe(%rip),%rsi
lea    -0x34(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidof>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
movl   $0x0,0xc(%rsp)
call   1050 <strlen@plt>
mov    %eax,%r14d
lea    0xe6d(%rip),%rsi
mov    %r15,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
xor    %ebx,%ebx
xor    %ecx,%ecx
test   %rax,%rax
sete   %bl
setne  %cl
lea    (%r14,%rcx,2),%edi
add    $0x28,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    (%r15,%rbx,2),%rdx
lea    0xe3c(%rip),%rsi
mov    %rax,%rdi
call   1090 <sprintf@plt>
lea    0xe59(%rip),%rsi
mov    %r14,%rdi
call   1080 <popen@plt>
mov    %rax,%r14
mov    $0x7,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    $0x6,%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
lea    0xe27(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <sscanf@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
@.str.1 = [44 x i8] c"ps axc | awk \22{if (\\$5==\\\22%s\\\22) print \\$1}\22\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @pidof(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %2
store i64 0, i64* %3
store i64 0, i64* %4
store i8* null, i8** %5
store i8* null, i8** %6
store i32* null, i32** %7
store i32 0, i32* %8
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 41, %10
%12 = sext i32 %11 to i64
store i64 %12, i64* %3
%13 = load i8*, i8** %2
%14 = call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
store i64 2, i64* %4
%17 = load i64, i64* %3
%18 = sub i64 %17, 2
store i64 %18, i64* %3
br label %19
19: ; preds = %16, %1
%20 = load i64, i64* %3
%21 = add i64 %20, 1
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = load i8*, i8** %2
%27 = load i64, i64* %4
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %5
%31 = call i32* @popen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %7
store i64 6, i64* %3
%32 = load i64, i64* %3
%33 = add i64 %32, 1
%34 = mul i64 1, %33
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = load i64, i64* %3
%39 = load i32*, i32** %7
%40 = call i32 @fread(i8* %37, i32 1, i64 %38, i32* %39)
%41 = load i8*, i8** %6
%42 = call i32 @sscanf(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %8)
%43 = load i32, i32* %8
ret i32 %43
}

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [44 x i8] c"ps axc | awk \22{if (\\$5==\\\22%s\\\22) print \\$1}\22\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @pidof(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%6 = icmp eq i64 %5, 0
%7 = select i1 %6, i32 39, i32 41
%8 = select i1 %6, i64 2, i64 0
%9 = add i32 %4, 1
%10 = add i32 %9, %7
%11 = tail call i8* @malloc(i32 %10)
%12 = getelementptr inbounds i8, i8* %0, i64 %8
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = tail call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call dereferenceable_or_null(7) i8* @malloc(i32 7)
%16 = tail call i32 @fread(i8* %15, i32 1, i64 6, i32* %14)
%17 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%18 = load i32, i32* %2
ret i32 %18
}