#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 statfs {
  int f_flags;
};
struct stat {
  scalar_t__ st_nlink;
};

/* Variables and functions */
int ENOTRECOVERABLE;
int MS_RDONLY;
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int fcntl(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ fstatfs(int, struct statfs *);
int /*<<< orphan*/ getenv(char *);
int open(char *, int);
int /*<<< orphan*/ stderr;
int is_self_cloned(void) {
  int fd, ret, is_cloned = 0;
  struct stat statbuf = {};
  struct statfs fsbuf = {};
  fd = open("/proc/self/exe", O_RDONLY | O_CLOEXEC);
  if (fd < 0) {
    fprintf(stderr, "you have no read access to runc binary file\n");
    return -ENOTRECOVERABLE;
  }
  ret = fcntl(fd, (1024 + 10));
  if (ret >= 0) {
    is_cloned = (ret == (0x0001 | 0x0002 | 0x0004 | 0x0008));
    goto out;
  }
  if (!getenv("_LIBCONTAINER_CLONED_BINARY")) {
    is_cloned = false;
    goto out;
  }
  if (fstatfs(fd, &fsbuf) >= 0)
    is_cloned |= (fsbuf.f_flags & MS_RDONLY);
  if (fstat(fd, &statbuf) >= 0)
    is_cloned |= (statbuf.st_nlink == 0);
out:
  close(fd);
  return is_cloned;
}


x86 assembly (clang, -O0):
<is_self_cloned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
or     (%rax),%esi
lea    0xe38(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1202 <is_self_cloned+0x82>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
mov    0x2de8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <is_self_cloned+0x135>
mov    -0x8(%rbp),%edi
mov    $0x40a,%esi
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1230 <is_self_cloned+0xb0>
cmpl   $0xf,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
jmp    12a7 <is_self_cloned+0x127>
lea    0xe05(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%eax
jne    1251 <is_self_cloned+0xd1>
movl   $0x0,-0x10(%rbp)
jmp    12a7 <is_self_cloned+0x127>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstatfs@plt>
cmp    $0x0,%rax
jl     1279 <is_self_cloned+0xf9>
mov    -0x20(%rbp),%eax
mov    0x2d67(%rip),%rcx
and    (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
jl     12a2 <is_self_cloned+0x122>
cmpq   $0x0,-0x18(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    12a7 <is_self_cloned+0x127>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_self_cloned>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
movl   $0x0,0x8(%rsp)
mov    0x2e5a(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe60(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11cd <is_self_cloned+0x5d>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x40a,%esi
call   1040 <fcntl@plt>
test   %eax,%eax
js     11f6 <is_self_cloned+0x86>
xor    %ebp,%ebp
cmp    $0xf,%eax
sete   %bpl
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11ed <is_self_cloned+0x7d>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2df7(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe3f(%rip),%rdi
call   1030 <getenv@plt>
test   %eax,%eax
je     124a <is_self_cloned+0xda>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1080 <fstatfs@plt>
mov    0x2dbf(%rip),%rcx
mov    (%rcx),%ecx
and    0x8(%rsp),%ecx
xor    %ebp,%ebp
test   %rax,%rax
cmovns %ecx,%ebp
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     11c4 <is_self_cloned+0x54>
xor    %eax,%eax
cmpq   $0x0,0x10(%rsp)
sete   %al
or     %eax,%ebp
jmp    11c4 <is_self_cloned+0x54>
xor    %ebp,%ebp
jmp    11c4 <is_self_cloned+0x54>

LLVM IR (-O0):
%struct.stat = type { i64 }
%struct.statfs = type { i32 }
@.str = [15 x i8] c"/proc/self/exe\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@stderr = global i32 0
@.str.1 = [45 x i8] c"you have no read access to runc binary file\0A\00"
@ENOTRECOVERABLE = global i32 0
@.str.2 = [28 x i8] c"_LIBCONTAINER_CLONED_BINARY\00"
@MS_RDONLY = global i32 0
define i32 @is_self_cloned() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca %struct.statfs
store i32 0, i32* %4
%7 = bitcast %struct.stat* %5 to i8*
%8 = bitcast %struct.statfs* %6 to i8*
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_CLOEXEC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @ENOTRECOVERABLE
%19 = sub nsw i32 0, %18
store i32 %19, i32* %1
br label %60
20: ; preds = %0
%21 = load i32, i32* %2
%22 = call i32 @fcntl(i32 %21, i32 1034)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 15
%28 = zext i1 %27 to i32
store i32 %28, i32* %4
br label %56
29: ; preds = %20
%30 = call i32 @getenv(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %29
store i32 0, i32* %4
br label %56
33: ; preds = %29
%34 = load i32, i32* %2
%35 = call i64 @fstatfs(i32 %34, %struct.statfs* %6)
%36 = icmp sge i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = getelementptr inbounds %struct.statfs, %struct.statfs* %6, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load i32, i32* @MS_RDONLY
%41 = and i32 %39, %40
%42 = load i32, i32* %4
%43 = or i32 %42, %41
store i32 %43, i32* %4
br label %44
44: ; preds = %37, %33
%45 = load i32, i32* %2
%46 = call i64 @fstat(i32 %45, %struct.stat* %5)
%47 = icmp sge i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%50 = load i64, i64* %49
%51 = icmp eq i64 %50, 0
%52 = zext i1 %51 to i32
%53 = load i32, i32* %4
%54 = or i32 %53, %52
store i32 %54, i32* %4
br label %55
55: ; preds = %48, %44
br label %56
56: ; preds = %55, %32, %25
%57 = load i32, i32* %2
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %4
store i32 %59, i32* %1
br label %60
60: ; preds = %56, %15
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.statfs = type { i32 }
@.str = [15 x i8] c"/proc/self/exe\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@stderr = global i32 0
@.str.1 = [45 x i8] c"you have no read access to runc binary file\0A\00"
@ENOTRECOVERABLE = global i32 0
@.str.2 = [28 x i8] c"_LIBCONTAINER_CLONED_BINARY\00"
@MS_RDONLY = global i32 0
define i32 @is_self_cloned() {
%1 = alloca %struct.stat
%2 = alloca %struct.statfs
%3 = bitcast %struct.stat* %1 to i8*
%4 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
store i64 0, i64* %4
%5 = bitcast %struct.statfs* %2 to i8*
%6 = getelementptr inbounds %struct.statfs, %struct.statfs* %2, i64 0, i32 0
store i32 0, i32* %6
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_CLOEXEC
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @ENOTRECOVERABLE
%16 = sub nsw i32 0, %15
br label %43
17: ; preds = %0
%18 = tail call i32 @fcntl(i32 %10, i32 1034)
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = icmp eq i32 %18, 15
%22 = zext i1 %21 to i32
br label %40
23: ; preds = %17
%24 = tail call i32 @getenv(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32 %24, 0
br i1 %25, label %40, label %26
26: ; preds = %23
%27 = call i64 @fstatfs(i32 %10, %struct.statfs* %2)
%28 = icmp sgt i64 %27, -1
%29 = load i32, i32* %6
%30 = load i32, i32* @MS_RDONLY
%31 = and i32 %30, %29
%32 = select i1 %28, i32 %31, i32 0
%33 = call i64 @fstat(i32 %10, %struct.stat* %1)
%34 = icmp sgt i64 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %26
%36 = load i64, i64* %4
%37 = icmp eq i64 %36, 0
%38 = zext i1 %37 to i32
%39 = or i32 %32, %38
br label %40
40: ; preds = %23, %26, %35, %20
%41 = phi i32 [ %22, %20 ], [ %39, %35 ], [ %32, %26 ], [ 0, %23 ]
%42 = call i32 @close(i32 %10)
br label %43
43: ; preds = %40, %12
%44 = phi i32 [ %16, %12 ], [ %41, %40 ]
ret i32 %44
}