#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 */
struct kp_file {
  int dummy;
};

/* Variables and functions */
int AT_FDCWD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ kpatch_open_fd(int, struct kp_file *);
int /*<<< orphan*/ kplog(int, char *, ...);
int openat(int, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int kpatch_openat_file(int atfd, const char *fname, struct kp_file *kpatch) {
  int fd;
  if (!kpatch)
    return -1;
  if (atfd == AT_FDCWD)
    kplog(3, "Opening patch file '%s'...", fname);
  else
    kplog(3, "Opening patch file '%s' at dirfd %d...", fname, atfd);
  fd = openat(atfd, fname, O_RDONLY);
  if (fd == -1) {
    kplog(3, "FAIL: %s\n", strerror(errno));
    return -1;
  }
  if (kpatch_open_fd(fd, kpatch) < 0)
    goto error;
  kplog(3, "OK\n");
  close(fd);
  return 0;
error:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<kpatch_openat_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117a <kpatch_openat_file+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <kpatch_openat_file+0x114>
mov    -0x8(%rbp),%eax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%eax
jne    11a8 <kpatch_openat_file+0x58>
mov    -0x10(%rbp),%rdx
mov    $0x3,%edi
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1060 <kplog@plt>
jmp    11c2 <kpatch_openat_file+0x72>
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%ecx
mov    $0x3,%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1060 <kplog@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
call   1030 <openat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1213 <kpatch_openat_file+0xc3>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%edx
mov    $0x3,%edi
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1060 <kplog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <kpatch_openat_file+0x114>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <kpatch_open_fd@plt>
cmp    $0x0,%rax
jge    122e <kpatch_openat_file+0xde>
jmp    1255 <kpatch_openat_file+0x105>
mov    $0x3,%edi
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1060 <kplog@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1264 <kpatch_openat_file+0x114>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kpatch_openat_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdx,%rdx
je     1214 <kpatch_openat_file+0xc4>
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2e53(%rip),%rax
cmp    %edi,(%rax)
jne    1191 <kpatch_openat_file+0x41>
lea    0xe80(%rip),%rsi
mov    $0x3,%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <kplog@plt>
jmp    11a9 <kpatch_openat_file+0x59>
lea    0xe83(%rip),%rsi
mov    $0x3,%edi
mov    %rbp,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <kplog@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <openat@plt>
cmp    $0xffffffff,%eax
je     11f1 <kpatch_openat_file+0xa1>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
call   1040 <kpatch_open_fd@plt>
test   %rax,%rax
js     11e8 <kpatch_openat_file+0x98>
lea    0xe73(%rip),%rsi
xor    %r14d,%r14d
mov    $0x3,%edi
xor    %eax,%eax
call   1060 <kplog@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1214 <kpatch_openat_file+0xc4>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe3c(%rip),%rsi
mov    $0x3,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <kplog@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.kp_file = type { i32 }
@AT_FDCWD = global i32 0
@.str = [27 x i8] c"Opening patch file '%s'...\00"
@.str.1 = [39 x i8] c"Opening patch file '%s' at dirfd %d...\00"
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"FAIL: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"OK\0A\00"
define i32 @kpatch_openat_file(i32 %0, i8* %1, %struct.kp_file* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.kp_file*
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store %struct.kp_file* %2, %struct.kp_file** %7
%9 = load %struct.kp_file*, %struct.kp_file** %7
%10 = icmp ne %struct.kp_file* %9, null
br i1 %10, label %12, label %11
11: ; preds = %3
store i32 -1, i32* %4
br label %47
12: ; preds = %3
%13 = load i32, i32* %5
%14 = load i32, i32* @AT_FDCWD
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8*, i8** %6
%18 = call i32 (i32, i8*, ...) @kplog(i32 3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %23
19: ; preds = %12
%20 = load i8*, i8** %6
%21 = load i32, i32* %5
%22 = call i32 (i32, i8*, ...) @kplog(i32 3, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %21)
br label %23
23: ; preds = %19, %16
%24 = load i32, i32* %5
%25 = load i8*, i8** %6
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @openat(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @kplog(i32 3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %32)
store i32 -1, i32* %4
br label %47
34: ; preds = %23
%35 = load i32, i32* %8
%36 = load %struct.kp_file*, %struct.kp_file** %7
%37 = call i64 @kpatch_open_fd(i32 %35, %struct.kp_file* %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
br label %44
40: ; preds = %34
%41 = call i32 (i32, i8*, ...) @kplog(i32 3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
store i32 0, i32* %4
br label %47
44: ; preds = %39
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %4
br label %47
47: ; preds = %44, %40, %30, %11
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
%struct.kp_file = type { i32 }
@AT_FDCWD = global i32 0
@.str = [27 x i8] c"Opening patch file '%s'...\00"
@.str.1 = [39 x i8] c"Opening patch file '%s' at dirfd %d...\00"
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"FAIL: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"OK\0A\00"
define i32 @kpatch_openat_file(i32 %0, i8* %1, %struct.kp_file* %2) {
%4 = icmp eq %struct.kp_file* %2, null
br i1 %4, label %28, label %5
5: ; preds = %3
%6 = load i32, i32* @AT_FDCWD
%7 = icmp eq i32 %6, %0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 (i32, i8*, ...) @kplog(i32 3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %12
10: ; preds = %5
%11 = tail call i32 (i32, i8*, ...) @kplog(i32 3, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %0)
br label %12
12: ; preds = %10, %8
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @openat(i32 %0, i8* %1, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = tail call i32 @strerror(i32 %17)
%19 = tail call i32 (i32, i8*, ...) @kplog(i32 3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %28
20: ; preds = %12
%21 = tail call i64 @kpatch_open_fd(i32 %14, %struct.kp_file* %2)
%22 = icmp slt i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = tail call i32 (i32, i8*, ...) @kplog(i32 3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @close(i32 %14)
br label %28
26: ; preds = %20
%27 = tail call i32 @close(i32 %14)
br label %28
28: ; preds = %3, %26, %23, %16
%29 = phi i32 [ -1, %16 ], [ -1, %26 ], [ 0, %23 ], [ -1, %3 ]
ret i32 %29
}