#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 /*<<< orphan*/ error(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *, char *);
scalar_t__ strcmp(char *, char *);
int get_process_id() {
  int process_id = 0;
  int pid = 0;
  int bool_found_pid = 0;
  FILE *process_list;
  char line[100];
  char str_extension[100];
  char str_match_extension[] = "bof";
  process_list = popen("ps -eo pid,args", "r");
  if (process_list < 0 || process_list == NULL)
    error("ERROR calling ps");
  while (fgets(line, 100 - 1, process_list) != NULL && bool_found_pid == 0) {
    sscanf(line, "%d%*[^.]%*[.]%[bof]", &pid, str_extension);
    if (strcmp(str_extension, str_match_extension) == 0) {
      process_id = pid;
      bool_found_pid = 1;
    }
  }
  printf("Firmware process: %d\n", process_id);
  if (bool_found_pid == 0) {
    error("ERROR finding firmware process ID\n");
  }
  return process_id;
}


x86 assembly (clang, -O0):
<get_process_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0xe7a(%rip),%eax
mov    %eax,-0xf4(%rbp)
lea    0xe71(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jb     11b9 <get_process_id+0x59>
cmpq   $0x0,-0x18(%rbp)
jne    11c5 <get_process_id+0x65>
lea    0xe56(%rip),%rdi
call   1070 <error@plt>
jmp    11ca <get_process_id+0x6a>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x63,%esi
call   1040 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0xf5(%rbp)
je     11fe <get_process_id+0x9e>
cmpl   $0x0,-0xc(%rbp)
sete   %al
mov    %al,-0xf5(%rbp)
mov    -0xf5(%rbp),%al
test   $0x1,%al
jne    1211 <get_process_id+0xb1>
jmp    125b <get_process_id+0xfb>
lea    -0x80(%rbp),%rdi
lea    -0xf0(%rbp),%rcx
lea    0xe04(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1060 <sscanf@plt>
lea    -0xf0(%rbp),%rdi
lea    -0xf4(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1256 <get_process_id+0xf6>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
jmp    11ca <get_process_id+0x6a>
mov    -0x4(%rbp),%esi
lea    0xdd6(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1280 <get_process_id+0x120>
lea    0xdd6(%rip),%rdi
call   1070 <error@plt>
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_process_id>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
movl   $0x0,0x8(%rsp)
movl   $0x666f62,0xc(%rsp)
lea    0xe78(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11a8 <get_process_id+0x48>
lea    0xe6f(%rip),%rdi
call   1070 <error@plt>
lea    0x10(%rsp),%rdi
mov    $0x63,%esi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     123f <get_process_id+0xdf>
xor    %r12d,%r12d
lea    0x10(%rsp),%rbp
lea    0x80(%rsp),%rbx
lea    0xc(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
lea    0xe39(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rcx
call   1060 <sscanf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
mov    %rax,%r15
test   %rax,%rax
jne    120f <get_process_id+0xaf>
mov    0x8(%rsp),%r12d
mov    %rbp,%rdi
mov    $0x63,%esi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %r15,%r15
je     1229 <get_process_id+0xc9>
test   %rax,%rax
jne    11e0 <get_process_id+0x80>
lea    0xe07(%rip),%rdi
mov    %r12d,%esi
call   1030 <printf@plt>
test   %r15,%r15
jne    1250 <get_process_id+0xf0>
jmp    125c <get_process_id+0xfc>
lea    0xdf1(%rip),%rdi
xor    %r12d,%r12d
xor    %esi,%esi
call   1030 <printf@plt>
lea    0xdf6(%rip),%rdi
call   1070 <error@plt>
mov    %r12d,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.get_process_id.str_match_extension = [4 x i8] c"bof\00"
@.str = [16 x i8] c"ps -eo pid,args\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"ERROR calling ps\00"
@.str.3 = [20 x i8] c"%d%*[^.]%*[.]%[bof]\00"
@.str.4 = [22 x i8] c"Firmware process: %d\0A\00"
@.str.5 = [35 x i8] c"ERROR finding firmware process ID\0A\00"
define i32 @get_process_id() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [100 x i8]
%6 = alloca [100 x i8]
%7 = alloca [4 x i8]
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
%8 = bitcast [4 x i8]* %7 to i8*
%9 = call i32* @popen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ult i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %0
%13 = load i32*, i32** %4
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12, %0
%16 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
br label %18
18: ; preds = %38, %17
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** %4
%21 = call i32* @fgets(i8* %19, i32 99, i32* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, 0
br label %26
26: ; preds = %23, %18
%27 = phi i1 [ false, %18 ], [ %25, %23 ]
br i1 %27, label %28, label %39
28: ; preds = %26
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%31 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32* %2, i8* %30)
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%34 = call i64 @strcmp(i8* %32, i8* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = load i32, i32* %2
store i32 %37, i32* %1
store i32 1, i32* %3
br label %38
38: ; preds = %36, %28
br label %18
39: ; preds = %26
%40 = load i32, i32* %1
%41 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %3
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [16 x i8] c"ps -eo pid,args\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"ERROR calling ps\00"
@.str.3 = [20 x i8] c"%d%*[^.]%*[.]%[bof]\00"
@.str.4 = [22 x i8] c"Firmware process: %d\0A\00"
@.str.5 = [35 x i8] c"ERROR finding firmware process ID\0A\00"
define i32 @get_process_id() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %4 to i8*
store i32 6713186, i32* %4
%9 = tail call i32* @popen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %0, %11
%14 = call i32* @fgets(i8* %6, i32 99, i32* %9)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 0)
br label %30
18: ; preds = %13, %18
%19 = phi i32 [ %24, %18 ], [ 0, %13 ]
%20 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32* %1, i8* %7)
%21 = call i64 @strcmp(i8* %7, i8* %8)
%22 = icmp eq i64 %21, 0
%23 = load i32, i32* %1
%24 = select i1 %22, i32 %23, i32 %19
%25 = call i32* @fgets(i8* %6, i32 99, i32* %9)
%26 = icmp eq i32* %25, null
%27 = or i1 %22, %26
br i1 %27, label %28, label %18
28: ; preds = %18
%29 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %24)
br i1 %22, label %33, label %30
30: ; preds = %16, %28
%31 = phi i32 [ 0, %16 ], [ %24, %28 ]
%32 = call i32 @error(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %30, %28
%34 = phi i32 [ %31, %30 ], [ %24, %28 ]
ret i32 %34
}