#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 pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strstr(char *, char *);
pid_t find_adb() {
  char buf[256];
  int i = 0, fd = 0;
  pid_t found = 0;
  for (i = 0; i < 32000; ++i) {
    sprintf(buf, "/proc/%d/cmdline", i);
    if ((fd = open(buf, O_RDONLY)) < 0)
      continue;
    memset(buf, 0, sizeof(buf));
    read(fd, buf, sizeof(buf) - 1);
    close(fd);
    if (strstr(buf, "/sbin/adb")) {
      found = i;
      break;
    }
  }
  return found;
}


x86 assembly (clang, -O0):
<find_adb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x104(%rbp)
movl   $0x0,-0x108(%rbp)
movl   $0x0,-0x10c(%rbp)
movl   $0x0,-0x104(%rbp)
cmpl   $0x7d00,-0x104(%rbp)
jge    1261 <find_adb+0x101>
lea    -0x100(%rbp),%rdi
mov    -0x104(%rbp),%edx
lea    0xe49(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x100(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x108(%rbp)
cmp    $0x0,%eax
jge    11e5 <find_adb+0x85>
jmp    124d <find_adb+0xed>
lea    -0x100(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
mov    -0x108(%rbp),%edi
lea    -0x100(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    -0x108(%rbp),%edi
call   1040 <close@plt>
lea    -0x100(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     1248 <find_adb+0xe8>
mov    -0x104(%rbp),%eax
mov    %eax,-0x10c(%rbp)
jmp    1261 <find_adb+0x101>
jmp    124d <find_adb+0xed>
mov    -0x104(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x104(%rbp)
jmp    1193 <find_adb+0x33>
mov    -0x10c(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_adb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
xor    %ebx,%ebx
lea    0xe86(%rip),%r14
mov    %rsp,%r15
mov    0x2e64(%rip),%r13
lea    0xe86(%rip),%r12
jmp    119b <find_adb+0x3b>
nopl   (%rax)
add    $0x1,%ebx
cmp    $0x7d00,%ebx
je     11f1 <find_adb+0x91>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1070 <sprintf@plt>
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1190 <find_adb+0x30>
mov    %eax,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1190 <find_adb+0x30>
jmp    11f3 <find_adb+0x93>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/sbin/adb\00"
define i32 @find_adb() {
%1 = alloca [256 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %2
br label %5
5: ; preds = %31, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 32000
br i1 %7, label %8, label %34
8: ; preds = %5
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %8
br label %31
17: ; preds = %8
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 0, i32 256)
%20 = load i32, i32* %3
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%22 = call i32 @read(i32 %20, i8* %21, i32 255)
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%26 = call i64 @strstr(i8* %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = load i32, i32* %2
store i32 %29, i32* %4
br label %34
30: ; preds = %17
br label %31
31: ; preds = %30, %16
%32 = load i32, i32* %2
%33 = add nsw i32 %32, 1
store i32 %33, i32* %2
br label %5
34: ; preds = %28, %5
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/sbin/adb\00"
define i32 @find_adb() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %15
%4 = phi i32 [ 0, %0 ], [ %16, %15 ]
%5 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %2, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %3
%10 = call i32 @memset(i8* %2, i32 0, i32 256)
%11 = call i32 @read(i32 %7, i8* %2, i32 255)
%12 = call i32 @close(i32 %7)
%13 = call i64 @strstr(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9, %3
%16 = add nuw nsw i32 %4, 1
%17 = icmp eq i32 %16, 32000
br i1 %17, label %18, label %3
18: ; preds = %9, %15
%19 = phi i32 [ 0, %15 ], [ %4, %9 ]
ret i32 %19
}