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


x86 assembly (clang, -O0):
<_set_init_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x124(%rbp)
lea    0xe3d(%rip),%rdi
lea    0xe43(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1207 <_set_init_program+0x67>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
lea    0xe0c(%rip),%rdx
mov    $0x0,%al
call   1080 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <_set_init_program+0x194>
jmp    120c <_set_init_program+0x6c>
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x125(%rbp)
jne    123a <_set_init_program+0x9a>
cmpl   $0x0,-0x124(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x125(%rbp)
mov    -0x125(%rbp),%al
test   $0x1,%al
jne    124d <_set_init_program+0xad>
jmp    12de <_set_init_program+0x13e>
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     12d9 <_set_init_program+0x139>
mov    -0x18(%rbp),%rax
mov    %rax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x130(%rbp),%rdi
xor    %esi,%esi
sub    %eax,%esi
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
mov    -0x8(%rbp),%edx
mov    $0x23,%eax
mov    $0x75,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    -0x18(%rbp),%rsi
movzbl %al,%edi
call   1050 <fputc@plt>
movl   $0x1,-0x124(%rbp)
jmp    120c <_set_init_program+0x6c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x124(%rbp)
jne    1322 <_set_init_program+0x182>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd1a(%rip),%rsi
lea    0xcf1(%rip),%rcx
mov    $0x0,%al
call   1080 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <_set_init_program+0x194>
mov    $0x71,%edi
mov    $0x5,%esi
call   10c0 <_telinit@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_set_init_program>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe43(%rip),%rdi
lea    0xe49(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1210 <_set_init_program+0x70>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     1238 <_set_init_program+0x98>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
lea    0xe04(%rip),%rcx
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    $0xffffffff,%eax
jmp    12db <_set_init_program+0x13b>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
lea    0xdd9(%rip),%rdx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    $0xffffffff,%eax
jmp    12db <_set_init_program+0x13b>
test   %ebp,%ebp
mov    $0x23,%eax
mov    $0x75,%ecx
cmove  %eax,%ecx
mov    %rsp,%r15
movzbl %cl,%r12d
jmp    128b <_set_init_program+0xeb>
mov    %r15,%rdi
call   1040 <strlen@plt>
neg    %eax
mov    0x2d87(%rip),%rcx
mov    (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <fseek@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x1,%r13d
mov    %rbx,%rdi
call   1070 <feof@plt>
or     %r13d,%eax
jne    12bb <_set_init_program+0x11b>
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
test   %rax,%rax
jne    1250 <_set_init_program+0xb0>
xor    %r13d,%r13d
jmp    127e <_set_init_program+0xde>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rbp,%rbp
je     11e5 <_set_init_program+0x45>
mov    $0x71,%edi
mov    $0x5,%esi
call   10c0 <_telinit@plt>
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 @_set_init_program(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [256 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %8
%9 = 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* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%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 -1, i32* %3
br label %60
14: ; preds = %2
br label %15
15: ; preds = %48, %14
%16 = load i32*, i32** %6
%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* %8
%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 %49
25: ; preds = %23
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%27 = load i32*, i32** %6
%28 = call i32 @fgets(i8* %26, i32 255, i32* %27)
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 255
store i8 0, i8* %29
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%31 = load i8*, i8** %5
%32 = call i64 @strstr(i8* %30, i8* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %48
34: ; preds = %25
%35 = load i32*, i32** %6
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %7, 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 = icmp ne i32 %41, 0
%43 = zext i1 %42 to i64
%44 = select i1 %42, i32 117, i32 35
%45 = trunc i32 %44 to i8
%46 = load i32*, i32** %6
%47 = call i32 @fputc(i8 zeroext %45, i32* %46)
store i32 1, i32* %8
br label %48
48: ; preds = %34, %25
br label %15
49: ; preds = %23
%50 = load i32*, i32** %6
%51 = call i32 @fclose(i32* %50)
%52 = load i32, i32* %8
%53 = icmp ne i32 %52, 0
br i1 %53, label %58, label %54
54: ; preds = %49
%55 = load i32, i32* @LOG_ERR
%56 = load i8*, i8** %5
%57 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %55, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %56, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
58: ; preds = %49
%59 = call i32 @_telinit(i8 signext 113, i32 5)
store i32 %59, i32* %3
br label %60
60: ; preds = %58, %54, %11
%61 = load i32, i32* %3
ret i32 %61
}

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 @_set_init_program(i32 %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %16, label %7
7: ; preds = %2
%8 = call i32 @feof(i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = call i32 @fclose(i32* %5)
br label %36
12: ; preds = %7
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 255
%14 = icmp eq i32 %0, 0
%15 = select i1 %14, i8 35, i8 117
br label %19
16: ; preds = %2
%17 = load i32, i32* @LOG_ERR
%18 = tail call i32 (i32, i8*, i8*, ...) @syslog(i32 %17, 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
19: ; preds = %12, %29
%20 = call i32 @fgets(i8* %4, i32 255, i32* %5)
store i8 0, i8* %13
%21 = call i64 @strstr(i8* %4, i8* %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %19
%24 = call i32 @strlen(i8* %4)
%25 = sub nsw i32 0, %24
%26 = load i32, i32* @SEEK_CUR
%27 = call i32 @fseek(i32* %5, i32 %25, i32 %26)
%28 = call i32 @fputc(i8 zeroext %15, i32* %5)
br label %29
29: ; preds = %23, %19
%30 = phi i32 [ 1, %23 ], [ 0, %19 ]
%31 = call i32 @feof(i32* %5)
%32 = or i32 %31, %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %19, label %34
34: ; preds = %29
%35 = call i32 @fclose(i32* %5)
br i1 %22, label %36, label %39
36: ; preds = %10, %34
%37 = load i32, i32* @LOG_ERR
%38 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %41
39: ; preds = %34
%40 = call i32 @_telinit(i8 signext 113, i32 5)
br label %41
41: ; preds = %39, %36, %16
%42 = phi i32 [ -1, %16 ], [ %40, %39 ], [ -1, %36 ]
ret i32 %42
}