#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*/ LOG_ERR;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, ...);
int libamg_process_check_daemon(char *prog) {
  FILE *f;
  char buf[256];
  int found = 0;
  int c = 0;
  f = fopen("/etc/inittab", "r+");
  if (!f) {
    syslog(LOG_ERR, "could not open %s", "/etc/inittab");
    return (0);
  }
  while ((!feof(f)) && (!found)) {
    fgets(buf, 255, f);
    buf[255] = 0;
    if (strstr(buf, prog)) {
      fseek(f, -strlen(buf), SEEK_CUR);
      c = fgetc(f);
      found = 1;
    }
  }
  fclose(f);
  if (!found) {
    syslog(LOG_ERR, "%s not found in %s", prog, "/etc/inittab");
    return (0);
  }
  return (c == 'u');
}


x86 assembly (clang, -O0):
<libamg_process_check_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x124(%rbp)
movl   $0x0,-0x128(%rbp)
lea    0xe46(%rip),%rdi
lea    0xe4c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ff <libamg_process_check_daemon+0x6f>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
lea    0xe14(%rip),%rdx
mov    $0x0,%al
call   1080 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    131c <libamg_process_check_daemon+0x18c>
jmp    1204 <libamg_process_check_daemon+0x74>
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x129(%rbp)
jne    1232 <libamg_process_check_daemon+0xa2>
cmpl   $0x0,-0x124(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x129(%rbp)
mov    -0x129(%rbp),%al
test   $0x1,%al
jne    1245 <libamg_process_check_daemon+0xb5>
jmp    12c6 <libamg_process_check_daemon+0x136>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xff,%esi
call   1060 <fgets@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     12c1 <libamg_process_check_daemon+0x131>
mov    -0x18(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x138(%rbp),%rdi
xor    %esi,%esi
sub    %eax,%esi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x128(%rbp)
movl   $0x1,-0x124(%rbp)
jmp    1204 <libamg_process_check_daemon+0x74>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x124(%rbp)
jne    130a <libamg_process_check_daemon+0x17a>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd32(%rip),%rsi
lea    0xd09(%rip),%rcx
mov    $0x0,%al
call   1080 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    131c <libamg_process_check_daemon+0x18c>
cmpl   $0x75,-0x128(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libamg_process_check_daemon>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r14
lea    0xe55(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11fb <libamg_process_check_daemon+0x6b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     1220 <libamg_process_check_daemon+0x90>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
lea    0xe16(%rip),%rcx
xor    %ebx,%ebx
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <syslog@plt>
jmp    12b8 <libamg_process_check_daemon+0x128>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
lea    0xdee(%rip),%rdx
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <syslog@plt>
jmp    12b8 <libamg_process_check_daemon+0x128>
xor    %r12d,%r12d
mov    %rsp,%r15
jmp    123d <libamg_process_check_daemon+0xad>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <feof@plt>
or     %r13d,%eax
jne    129e <libamg_process_check_daemon+0x10e>
mov    %r15,%rdi
mov    $0xff,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
movb   $0x0,0xff(%rsp)
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <strstr@plt>
mov    %rax,%rbp
mov    $0x0,%r13d
test   %rax,%rax
je     1230 <libamg_process_check_daemon+0xa0>
mov    %r15,%rdi
call   1040 <strlen@plt>
neg    %eax
mov    0x2d69(%rip),%rcx
mov    (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    %eax,%r12d
mov    $0x1,%r13d
jmp    1230 <libamg_process_check_daemon+0xa0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rbp,%rbp
je     11d3 <libamg_process_check_daemon+0x43>
xor    %ebx,%ebx
cmp    $0x75,%r12d
sete   %bl
mov    %ebx,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/etc/inittab\00"
@.str.1 = [3 x i8] c"r+\00"
@LOG_ERR = global i32 0
@.str.2 = [18 x i8] c"could not open %s\00"
@SEEK_CUR = global i32 0
@.str.3 = [19 x i8] c"%s not found in %s\00"
define i32 @libamg_process_check_daemon(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
store i32 0, i32* %7
%8 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i32, i32* @LOG_ERR
%13 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %57
14: ; preds = %1
br label %15
15: ; preds = %43, %14
%16 = load i32*, i32** %4
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
%22 = xor i1 %21, true
br label %23
23: ; preds = %19, %15
%24 = phi i1 [ false, %15 ], [ %22, %19 ]
br i1 %24, label %25, label %44
25: ; preds = %23
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%27 = load i32*, i32** %4
%28 = call i32 @fgets(i8* %26, i32 255, i32* %27)
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 255
store i8 0, i8* %29
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%31 = load i8*, i8** %3
%32 = call i64 @strstr(i8* %30, i8* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %43
34: ; preds = %25
%35 = load i32*, i32** %4
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = sub nsw i32 0, %37
%39 = load i32, i32* @SEEK_CUR
%40 = call i32 @fseek(i32* %35, i32 %38, i32 %39)
%41 = load i32*, i32** %4
%42 = call i32 @fgetc(i32* %41)
store i32 %42, i32* %7
store i32 1, i32* %6
br label %43
43: ; preds = %34, %25
br label %15
44: ; preds = %23
%45 = load i32*, i32** %4
%46 = call i32 @fclose(i32* %45)
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %44
%50 = load i32, i32* @LOG_ERR
%51 = load i8*, i8** %3
%52 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %50, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %51, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %57
53: ; preds = %44
%54 = load i32, i32* %7
%55 = icmp eq i32 %54, 117
%56 = zext i1 %55 to i32
store i32 %56, i32* %2
br label %57
57: ; preds = %53, %49, %11
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@.str = [13 x i8] c"/etc/inittab\00"
@.str.1 = [3 x i8] c"r+\00"
@LOG_ERR = global i32 0
@.str.2 = [18 x i8] c"could not open %s\00"
@SEEK_CUR = global i32 0
@.str.3 = [19 x i8] c"%s not found in %s\00"
define i32 @libamg_process_check_daemon(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %13, label %6
6: ; preds = %1
%7 = call i32 @feof(i32* %4)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = call i32 @fclose(i32* %4)
br label %35
11: ; preds = %6
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 255
br label %16
13: ; preds = %1
%14 = load i32, i32* @LOG_ERR
%15 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %41
16: ; preds = %11, %27
%17 = phi i32 [ 0, %11 ], [ %29, %27 ]
%18 = call i32 @fgets(i8* %3, i32 255, i32* %4)
store i8 0, i8* %12
%19 = call i64 @strstr(i8* %3, i8* %0)
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %16
%22 = call i32 @strlen(i8* %3)
%23 = sub nsw i32 0, %22
%24 = load i32, i32* @SEEK_CUR
%25 = call i32 @fseek(i32* %4, i32 %23, i32 %24)
%26 = call i32 @fgetc(i32* %4)
br label %27
27: ; preds = %21, %16
%28 = phi i32 [ 1, %21 ], [ 0, %16 ]
%29 = phi i32 [ %26, %21 ], [ %17, %16 ]
%30 = call i32 @feof(i32* %4)
%31 = or i32 %30, %28
%32 = icmp eq i32 %31, 0
br i1 %32, label %16, label %33
33: ; preds = %27
%34 = call i32 @fclose(i32* %4)
br i1 %20, label %35, label %38
35: ; preds = %9, %33
%36 = load i32, i32* @LOG_ERR
%37 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %36, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %41
38: ; preds = %33
%39 = icmp eq i32 %29, 117
%40 = zext i1 %39 to i32
br label %41
41: ; preds = %38, %35, %13
%42 = phi i32 [ %40, %38 ], [ 0, %35 ], [ 0, %13 ]
ret i32 %42
}