#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 */
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ processes;
struct TYPE_5__ {
  size_t gl_pathc;
  int /*<<< orphan*/ *gl_pathv;
};
typedef TYPE_1__ glob_t;
typedef int /*<<< orphan*/ exe;

/* Variables and functions */
int /*<<< orphan*/ GLOB_NOSORT;
int /*<<< orphan*/ glob(char *, int /*<<< orphan*/, int /*<<< orphan*/ *,
                        TYPE_1__ *);
int /*<<< orphan*/ globfree(TYPE_1__ *);
int /*<<< orphan*/ memset(TYPE_1__ *, int /*<<< orphan*/, int);
int readlink(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int is_alive(char *service_name) {
  int result = 0;
  glob_t processes;
  size_t i;
  char exe[256], path[80];
  sprintf(path, "/usr/sbin/%s", service_name);
  memset(&processes, 0, sizeof(processes));
  glob("/proc/*/exe", GLOB_NOSORT, NULL, &processes);
  for (i = 0; i < processes.gl_pathc; i++) {
    int count = readlink(processes.gl_pathv[i], exe, sizeof(exe));
    if (count == -1)
      continue;
    exe[count] = '\0';
    if (strcmp(exe, path) == 0) {
      result = 1;
      break;
    }
  }
  globfree(&processes);
  return result;
}


x86 assembly (clang, -O0):
<is_alive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x180(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe78(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe60(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
call   1070 <glob@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1252 <is_alive+0xf2>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
call   1040 <readlink@plt>
mov    %eax,-0x184(%rbp)
cmpl   $0xffffffff,-0x184(%rbp)
jne    1204 <is_alive+0xa4>
jmp    1241 <is_alive+0xe1>
movslq -0x184(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
lea    -0x130(%rbp),%rdi
lea    -0x180(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    123c <is_alive+0xdc>
movl   $0x1,-0xc(%rbp)
jmp    1252 <is_alive+0xf2>
jmp    1241 <is_alive+0xe1>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11c2 <is_alive+0x62>
lea    -0x20(%rbp),%rdi
call   1030 <globfree@plt>
mov    -0xc(%rbp),%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_alive>:
push   %r15
push   %r14
push   %rbx
sub    $0x160,%rsp
mov    %rdi,%rdx
lea    0xe8a(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <sprintf@plt>
mov    %rsp,%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
xor    %edx,%edx
mov    %r14,%rcx
call   1070 <glob@plt>
cmpq   $0x0,(%rsp)
je     1214 <is_alive+0xb4>
xor    %ebx,%ebx
lea    0x60(%rsp),%r15
lea    0x10(%rsp),%r14
jmp    11da <is_alive+0x7a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
cmp    (%rsp),%rbx
jae    1212 <is_alive+0xb2>
mov    0x8(%rsp),%rax
mov    (%rax,%rbx,4),%edi
mov    %r15,%rsi
mov    $0x100,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11d0 <is_alive+0x70>
cltq
movb   $0x0,0x60(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11d0 <is_alive+0x70>
mov    $0x1,%ebx
jmp    1214 <is_alive+0xb4>
xor    %ebx,%ebx
mov    %rsp,%rdi
call   1030 <globfree@plt>
mov    %ebx,%eax
add    $0x160,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i64, i32* }
@.str = [13 x i8] c"/usr/sbin/%s\00"
@.str.1 = [12 x i8] c"/proc/*/exe\00"
@GLOB_NOSORT = global i32 0
define i32 @is_alive(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.TYPE_5__
%5 = alloca i64
%6 = alloca [256 x i8]
%7 = alloca [80 x i8]
%8 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%10 = load i8*, i8** %2
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @memset(%struct.TYPE_5__* %4, i32 0, i32 16)
%13 = load i32, i32* @GLOB_NOSORT
%14 = call i32 @glob(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32* null, %struct.TYPE_5__* %4)
store i64 0, i64* %5
br label %15
15: ; preds = %41, %1
%16 = load i64, i64* %5
%17 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %4, i32 0, i32 0
%18 = load i64, i64* %17
%19 = icmp ult i64 %16, %18
br i1 %19, label %20, label %44
20: ; preds = %15
%21 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %4, i32 0, i32 1
%22 = load i32*, i32** %21
%23 = load i64, i64* %5
%24 = getelementptr inbounds i32, i32* %22, i64 %23
%25 = load i32, i32* %24
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = call i32 @readlink(i32 %25, i8* %26, i32 256)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %20
br label %41
31: ; preds = %20
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%37 = call i64 @strcmp(i8* %35, i8* %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %31
store i32 1, i32* %3
br label %44
40: ; preds = %31
br label %41
41: ; preds = %40, %30
%42 = load i64, i64* %5
%43 = add i64 %42, 1
store i64 %43, i64* %5
br label %15
44: ; preds = %39, %15
%45 = call i32 @globfree(%struct.TYPE_5__* %4)
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i64, i32* }
@.str = [13 x i8] c"/usr/sbin/%s\00"
@.str.1 = [12 x i8] c"/proc/*/exe\00"
@GLOB_NOSORT = global i32 0
define i32 @is_alive(i8* %0) {
%2 = alloca %struct.TYPE_5__
%3 = alloca [256 x i8]
%4 = alloca [80 x i8]
%5 = bitcast %struct.TYPE_5__* %2 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%8 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = call i32 @memset(%struct.TYPE_5__* %2, i32 0, i32 16)
%10 = load i32, i32* @GLOB_NOSORT
%11 = call i32 @glob(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32* null, %struct.TYPE_5__* %2)
%12 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i64 0, i32 0
%13 = load i64, i64* %12
%14 = icmp eq i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %1
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i64 0, i32 1
br label %17
17: ; preds = %15, %29
%18 = phi i64 [ 0, %15 ], [ %30, %29 ]
%19 = load i32*, i32** %16
%20 = getelementptr inbounds i32, i32* %19, i64 %18
%21 = load i32, i32* %20
%22 = call i32 @readlink(i32 %21, i8* %6, i32 256)
%23 = icmp eq i32 %22, -1
br i1 %23, label %29, label %24
24: ; preds = %17
%25 = sext i32 %22 to i64
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i64 @strcmp(i8* %6, i8* %7)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %17, %24
%30 = add nuw i64 %18, 1
%31 = load i64, i64* %12
%32 = icmp ult i64 %30, %31
br i1 %32, label %17, label %33
33: ; preds = %29, %24, %1
%34 = phi i32 [ 0, %1 ], [ 1, %24 ], [ 0, %29 ]
%35 = call i32 @globfree(%struct.TYPE_5__* %2)
ret i32 %34
}