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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, size_t);
void proc_read_impl(char *buf, size_t buf_size) {
  int fd = open("/proc/systemtap/attach_gdb/wrapper_path", O_RDONLY);
  if (fd == -1) {
    perror("Open proc interface");
    exit(EXIT_FAILURE);
  }
  if (read(fd, buf, buf_size) == -1) {
    perror("Read from proc interface");
    close(fd);
    exit(EXIT_FAILURE);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<proc_read_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119c <proc_read_impl+0x4c>
lea    0xe9f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11d7 <proc_read_impl+0x87>
lea    0xe80(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_read_impl>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1195 <proc_read_impl+0x45>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11a3 <proc_read_impl+0x53>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <close@plt>
lea    0xe8c(%rip),%rdi
call   1060 <perror@plt>
jmp    11b6 <proc_read_impl+0x66>
lea    0xe92(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"/proc/systemtap/attach_gdb/wrapper_path\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"Open proc interface\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [25 x i8] c"Read from proc interface\00"
define void @proc_read_impl(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* %5
%16 = load i8*, i8** %3
%17 = load i64, i64* %4
%18 = call i32 @read(i32 %15, i8* %16, i64 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
ret void
}

LLVM IR (-O2):
@.str = [40 x i8] c"/proc/systemtap/attach_gdb/wrapper_path\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"Open proc interface\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [25 x i8] c"Read from proc interface\00"
define void @proc_read_impl(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i32 @read(i32 %4, i8* %0, i64 %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @close(i32 %4)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
%19 = tail call i32 @close(i32 %4)
ret void
}