#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int strlen(char const *);
int str_search(const char *s) {
  FILE *f = fopen("/proc/cmdline", "r");
  char *buf = malloc((4096 + 1) * sizeof(char));
  int i, j, found;
  fgets(buf, 4096, f);
  for (i = 0; i <= strlen(buf) - strlen(s); i++) {
    found = -1;
    for (j = 0; j < strlen(s); j++)
      if (s[j] != buf[i + j])
        break;
    if (j == strlen(s)) {
      found = i;
      break;
    }
  }
  free(buf);
  fclose(f);
  return found;
}


x86 assembly (clang, -O0):
<str_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    $0x1001,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1060 <fgets@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2c(%rbp),%ecx
mov    %eax,%edx
mov    -0x28(%rbp),%eax
sub    %edx,%ecx
cmp    %ecx,%eax
jg     1278 <str_search+0x118>
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jge    123e <str_search+0xde>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%edx
add    -0x20(%rbp),%edx
movslq %edx,%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
je     122b <str_search+0xcb>
jmp    123e <str_search+0xde>
jmp    1230 <str_search+0xd0>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11e5 <str_search+0x85>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jne    1265 <str_search+0x105>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1278 <str_search+0x118>
jmp    126a <str_search+0x10a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11aa <str_search+0x4a>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_search>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe8b(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
mov    $0x1001,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0xffffffff,%r15d
cmp    %eax,%ebp
jl     1253 <str_search+0xf3>
mov    %r14,(%rsp)
xor    %r13d,%r13d
mov    %r12,%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0x0,%ebp
test   %eax,%eax
jle    120d <str_search+0xad>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rbx,%rbp,1),%eax
cmp    (%r15,%rbp,1),%al
jne    120d <str_search+0xad>
add    $0x1,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     11f0 <str_search+0x90>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebp
je     124c <str_search+0xec>
lea    0x1(%r13),%r14
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
sub    %eax,%ebp
movslq %ebp,%rax
add    $0x1,%r15
cmp    %rax,%r13
mov    %r14,%r13
jl     11d0 <str_search+0x70>
mov    (%rsp),%r14
mov    $0xffffffff,%r15d
jmp    1253 <str_search+0xf3>
mov    %r13d,%r15d
mov    (%rsp),%r14
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @str_search(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %3
%9 = call i8* @malloc(i32 4097)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = load i32*, i32** %3
%12 = call i32 @fgets(i8* %10, i32 4096, i32* %11)
store i32 0, i32* %5
br label %13
13: ; preds = %56, %1
%14 = load i32, i32* %5
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = load i8*, i8** %2
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 %16, %18
%20 = icmp sle i32 %14, %19
br i1 %20, label %21, label %59
21: ; preds = %13
store i32 -1, i32* %7
store i32 0, i32* %6
br label %22
22: ; preds = %45, %21
%23 = load i32, i32* %6
%24 = load i8*, i8** %2
%25 = call i32 @strlen(i8* %24)
%26 = icmp slt i32 %23, %25
br i1 %26, label %27, label %48
27: ; preds = %22
%28 = load i8*, i8** %2
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = load i8*, i8** %4
%35 = load i32, i32* %5
%36 = load i32, i32* %6
%37 = add nsw i32 %35, %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %34, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %33, %41
br i1 %42, label %43, label %44
43: ; preds = %27
br label %48
44: ; preds = %27
br label %45
45: ; preds = %44
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
br label %22
48: ; preds = %43, %22
%49 = load i32, i32* %6
%50 = load i8*, i8** %2
%51 = call i32 @strlen(i8* %50)
%52 = icmp eq i32 %49, %51
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = load i32, i32* %5
store i32 %54, i32* %7
br label %59
55: ; preds = %48
br label %56
56: ; preds = %55
%57 = load i32, i32* %5
%58 = add nsw i32 %57, 1
store i32 %58, i32* %5
br label %13
59: ; preds = %53, %13
%60 = load i8*, i8** %4
%61 = call i32 @free(i8* %60)
%62 = load i32*, i32** %3
%63 = call i32 @fclose(i32* %62)
%64 = load i32, i32* %7
ret i32 %64
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @str_search(i8* %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call dereferenceable_or_null(4097) i8* @malloc(i32 4097)
%4 = tail call i32 @fgets(i8* %3, i32 4096, i32* %2)
%5 = tail call i32 @strlen(i8* %3)
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp slt i32 %5, %6
br i1 %7, label %41, label %8
8: ; preds = %1, %32
%9 = phi i64 [ %33, %32 ], [ 0, %1 ]
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %8, %20
%13 = phi i64 [ %21, %20 ], [ 0, %8 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = add nuw nsw i64 %13, %9
%17 = getelementptr inbounds i8, i8* %3, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %15, %18
br i1 %19, label %20, label %25
20: ; preds = %12
%21 = add nuw nsw i64 %13, 1
%22 = tail call i32 @strlen(i8* %0)
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %21, %23
br i1 %24, label %12, label %25
25: ; preds = %12, %20
%26 = phi i64 [ %21, %20 ], [ %13, %12 ]
%27 = trunc i64 %26 to i32
br label %28
28: ; preds = %25, %8
%29 = phi i32 [ 0, %8 ], [ %27, %25 ]
%30 = tail call i32 @strlen(i8* %0)
%31 = icmp eq i32 %29, %30
br i1 %31, label %39, label %32
32: ; preds = %28
%33 = add nuw nsw i64 %9, 1
%34 = tail call i32 @strlen(i8* %3)
%35 = tail call i32 @strlen(i8* %0)
%36 = sub nsw i32 %34, %35
%37 = sext i32 %36 to i64
%38 = icmp slt i64 %9, %37
br i1 %38, label %8, label %41
39: ; preds = %28
%40 = trunc i64 %9 to i32
br label %41
41: ; preds = %32, %39, %1
%42 = phi i32 [ -1, %1 ], [ %40, %39 ], [ -1, %32 ]
%43 = tail call i32 @free(i8* %3)
%44 = tail call i32 @fclose(i32* %2)
ret i32 %42
}