#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*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
scalar_t__ memcmp(void *, char *, int);
int /*<<< orphan*/ peek_text(int /*<<< orphan*/, size_t, char *, int);
size_t find_syscall_addr(pid_t pid, size_t addr) {
  int buf_size = 1024;
  void *tmp_ptr;
  char *syscall_op = "\x0f\x05";
  char *buf = malloc(buf_size);
  addr -= buf_size;
  peek_text(pid, addr, buf, buf_size);
  tmp_ptr = buf;
  while (memcmp(tmp_ptr, syscall_op, 2)) {
    tmp_ptr++;
    if (buf_size-- == 0) {
      free(buf);
      return (size_t)NULL;
    }
  }
  free(buf);
  return addr - ((size_t)buf - (size_t)tmp_ptr);
}


x86 assembly (clang, -O0):
<find_syscall_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x400,-0x1c(%rbp)
lea    0xea3(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1050 <peek_text@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0x2,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     11ee <find_syscall_addr+0xae>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11e9 <find_syscall_addr+0xa9>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    120d <find_syscall_addr+0xcd>
jmp    1197 <find_syscall_addr+0x57>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_syscall_addr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    -0x400(%r14),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
mov    $0x400,%ecx
call   1050 <peek_text@plt>
mov    $0x400,%r13d
lea    0xe82(%rip),%r12
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x2,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     11c0 <find_syscall_addr+0x80>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r13
cmp    $0xffffffff,%r13d
jne    1190 <find_syscall_addr+0x50>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    11cb <find_syscall_addr+0x8b>
mov    %r15,%rdi
call   1030 <free@plt>
sub    %r13,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0F\05\00"
define i64 @find_syscall_addr(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 1024, i32* %6
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %8
%10 = load i32, i32* %6
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %9
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = load i64, i64* %5
%15 = sub i64 %14, %13
store i64 %15, i64* %5
%16 = load i32, i32* %4
%17 = load i64, i64* %5
%18 = load i8*, i8** %9
%19 = load i32, i32* %6
%20 = call i32 @peek_text(i32 %16, i64 %17, i8* %18, i32 %19)
%21 = load i8*, i8** %9
store i8* %21, i8** %7
br label %22
22: ; preds = %36, %2
%23 = load i8*, i8** %7
%24 = load i8*, i8** %8
%25 = call i64 @memcmp(i8* %23, i8* %24, i32 2)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %22
%28 = load i8*, i8** %7
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %7
%30 = load i32, i32* %6
%31 = add nsw i32 %30, -1
store i32 %31, i32* %6
%32 = icmp eq i32 %30, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i8*, i8** %9
%35 = call i32 @free(i8* %34)
store i64 0, i64* %3
br label %47
36: ; preds = %27
br label %22
37: ; preds = %22
%38 = load i8*, i8** %9
%39 = call i32 @free(i8* %38)
%40 = load i64, i64* %5
%41 = load i8*, i8** %9
%42 = ptrtoint i8* %41 to i64
%43 = load i8*, i8** %7
%44 = ptrtoint i8* %43 to i64
%45 = sub i64 %42, %44
%46 = sub i64 %40, %45
store i64 %46, i64* %3
br label %47
47: ; preds = %37, %33
%48 = load i64, i64* %3
ret i64 %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0F\05\00"
define i64 @find_syscall_addr(i32 %0, i64 %1) {
%3 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%4 = add i64 %1, -1024
%5 = tail call i32 @peek_text(i32 %0, i64 %4, i8* %3, i32 1024)
br label %6
6: ; preds = %11, %2
%7 = phi i32 [ 1024, %2 ], [ %13, %11 ]
%8 = phi i8* [ %3, %2 ], [ %12, %11 ]
%9 = tail call i64 @memcmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %6
%12 = getelementptr inbounds i8, i8* %8, i64 1
%13 = add nsw i32 %7, -1
%14 = icmp eq i32 %7, 0
br i1 %14, label %15, label %6
15: ; preds = %11
%16 = tail call i32 @free(i8* %3)
br label %23
17: ; preds = %6
%18 = tail call i32 @free(i8* %3)
%19 = ptrtoint i8* %3 to i64
%20 = ptrtoint i8* %8 to i64
%21 = sub i64 %4, %19
%22 = add i64 %21, %20
br label %23
23: ; preds = %17, %15
%24 = phi i64 [ 0, %15 ], [ %22, %17 ]
ret i64 %24
}