#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*/ AT_FDCWD;
int /*<<< orphan*/ O_CREAT;
int /*<<< orphan*/ SYS_openat;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *get_tmp_file_path(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ remove(char *);
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, char *, int /*<<< orphan*/);
int gen_open_openat(bool is_sys_open) {
  char *file_path =
      get_tmp_file_path("/bpftrace_runtime_test_syscall_gen_open_temp");
  int fd = -1;
  if (is_sys_open) {
  } else {
    fd = syscall(SYS_openat, AT_FDCWD, file_path, O_CREAT);
  }
  if (fd < 0) {
    perror("Error in syscall open/openat");
    free(file_path);
    return 1;
  }
  close(fd);
  remove(file_path);
  free(file_path);
  return 0;
}


x86 assembly (clang, -O0):
<gen_open_openat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe8e(%rip),%rdi
call   1050 <get_tmp_file_path@plt>
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1191 <gen_open_openat+0x31>
jmp    11b8 <gen_open_openat+0x58>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    0x2e4a(%rip),%rax
mov    (%rax),%ecx
call   1070 <syscall@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e3 <gen_open_openat+0x83>
lea    0xe64(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1204 <gen_open_openat+0xa4>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_open_openat>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xe94(%rip),%rdi
call   1050 <get_tmp_file_path@plt>
mov    %rax,%rbx
test   %ebp,%ebp
jne    11c1 <gen_open_openat+0x61>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdx
call   1070 <syscall@plt>
test   %eax,%eax
js     11c1 <gen_open_openat+0x61>
mov    %eax,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1040 <remove@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe65(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    11b0 <gen_open_openat+0x50>

LLVM IR (-O0):
@.str = [45 x i8] c"/bpftrace_runtime_test_syscall_gen_open_temp\00"
@SYS_openat = global i32 0
@AT_FDCWD = global i32 0
@O_CREAT = global i32 0
@.str.1 = [29 x i8] c"Error in syscall open/openat\00"
define i32 @gen_open_openat(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
%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** %4
store i32 -1, i32* %5
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
br label %16
10: ; preds = %1
%11 = load i32, i32* @SYS_openat
%12 = load i32, i32* @AT_FDCWD
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_CREAT
%15 = call i32 @syscall(i32 %11, i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %5
br label %16
16: ; preds = %10, %9
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** %4
%22 = call i32 @free(i8* %21)
store i32 1, i32* %2
br label %30
23: ; preds = %16
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
%26 = load i8*, i8** %4
%27 = call i32 @remove(i8* %26)
%28 = load i8*, i8** %4
%29 = call i32 @free(i8* %28)
store i32 0, i32* %2
br label %30
30: ; preds = %23, %19
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [45 x i8] c"/bpftrace_runtime_test_syscall_gen_open_temp\00"
@SYS_openat = global i32 0
@AT_FDCWD = global i32 0
@O_CREAT = global i32 0
@.str.1 = [29 x i8] c"Error in syscall open/openat\00"
define i32 @gen_open_openat(i32 %0) {
%2 = tail call i8* @get_tmp_file_path(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32 %0, 0
br i1 %3, label %4, label %10
4: ; preds = %1
%5 = load i32, i32* @SYS_openat
%6 = load i32, i32* @AT_FDCWD
%7 = load i32, i32* @O_CREAT
%8 = tail call i32 @syscall(i32 %5, i32 %6, i8* %2, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1, %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %15
12: ; preds = %4
%13 = tail call i32 @close(i32 %8)
%14 = tail call i32 @remove(i8* %2)
br label %15
15: ; preds = %12, %10
%16 = phi i32 [ 1, %10 ], [ 0, %12 ]
%17 = tail call i32 @free(i8* %2)
ret i32 %16
}