#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*/ O_CREAT;
int /*<<< orphan*/ SYS_read;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *get_tmp_file_path(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ remove(char *);
int syscall(int /*<<< orphan*/, int, void *, int /*<<< orphan*/);
int gen_read() {
  char *file_path =
      get_tmp_file_path("/bpftrace_runtime_test_syscall_gen_read_temp");
  int fd = open(file_path, O_CREAT);
  if (fd < 0) {
    perror("Error in syscall read when creating temp file");
    free(file_path);
    return 1;
  }
  char buf[10];
  int r = syscall(SYS_read, fd, (void *)buf, 0);
  close(fd);
  remove(file_path);
  free(file_path);
  if (r < 0) {
    perror("Error in syscall read");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gen_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe81(%rip),%rdi
call   1050 <get_tmp_file_path@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c8 <gen_read+0x58>
lea    0xe7f(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1225 <gen_read+0xb5>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
lea    -0x1e(%rbp),%rdx
xor    %ecx,%ecx
call   1070 <syscall@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x24(%rbp)
jge    121e <gen_read+0xae>
lea    0xe4e(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1225 <gen_read+0xb5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe7f(%rip),%rdi
call   1050 <get_tmp_file_path@plt>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e7 <gen_read+0x77>
mov    %eax,%ebp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe(%rsp),%rdx
xor    %r14d,%r14d
mov    %ebp,%esi
xor    %ecx,%ecx
call   1070 <syscall@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1040 <remove@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r15d,%r15d
js     11fd <gen_read+0x8d>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3f(%rip),%rdi
call   1090 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1209 <gen_read+0x99>
lea    0xe57(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%r14d
jmp    11d9 <gen_read+0x69>

LLVM IR (-O0):
@.str = [45 x i8] c"/bpftrace_runtime_test_syscall_gen_read_temp\00"
@O_CREAT = global i32 0
@.str.1 = [46 x i8] c"Error in syscall read when creating temp file\00"
@SYS_read = global i32 0
@.str.2 = [22 x i8] c"Error in syscall read\00"
define i32 @gen_read() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [10 x i8]
%5 = alloca i32
%6 = call i8* @get_tmp_file_path(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_CREAT
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%14 = load i8*, i8** %2
%15 = call i32 @free(i8* %14)
store i32 1, i32* %1
br label %32
16: ; preds = %0
%17 = load i32, i32* @SYS_read
%18 = load i32, i32* %3
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%20 = call i32 @syscall(i32 %17, i32 %18, i8* %19, i32 0)
store i32 %20, i32* %5
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
%23 = load i8*, i8** %2
%24 = call i32 @remove(i8* %23)
%25 = load i8*, i8** %2
%26 = call i32 @free(i8* %25)
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %32
31: ; preds = %16
store i32 0, i32* %1
br label %32
32: ; preds = %31, %29, %12
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [45 x i8] c"/bpftrace_runtime_test_syscall_gen_read_temp\00"
@O_CREAT = global i32 0
@.str.1 = [46 x i8] c"Error in syscall read when creating temp file\00"
@SYS_read = global i32 0
@.str.2 = [22 x i8] c"Error in syscall read\00"
define i32 @gen_read() {
%1 = alloca [10 x i8]
%2 = tail call i8* @get_tmp_file_path(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%3 = load i32, i32* @O_CREAT
%4 = tail call i32 @open(i8* %2, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @free(i8* %2)
br label %21
9: ; preds = %0
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%11 = load i32, i32* @SYS_read
%12 = call i32 @syscall(i32 %11, i32 %4, i8* %10, i32 0)
%13 = call i32 @close(i32 %4)
%14 = call i32 @remove(i8* %2)
%15 = call i32 @free(i8* %2)
%16 = icmp slt i32 %12, 0
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %9, %17
%20 = phi i32 [ 1, %17 ], [ 0, %9 ]
br label %21
21: ; preds = %19, %6
%22 = phi i32 [ 1, %6 ], [ %20, %19 ]
ret i32 %22
}