#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);
scalar_t__ fstat(int, struct stat *);
scalar_t__ fstatfs(int, struct statfs *);
int /*<<< orphan*/ getenv(char *);
int open(char *, int);
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)
    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    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11dd <is_self_cloned+0x6d>
xor    %eax,%eax
mov    0x2e0d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1290 <is_self_cloned+0x120>
mov    -0x8(%rbp),%edi
mov    $0x40a,%esi
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     120b <is_self_cloned+0x9b>
cmpl   $0xf,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
jmp    1282 <is_self_cloned+0x112>
lea    0xdfd(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%eax
jne    122c <is_self_cloned+0xbc>
movl   $0x0,-0x10(%rbp)
jmp    1282 <is_self_cloned+0x112>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstatfs@plt>
cmp    $0x0,%rax
jl     1254 <is_self_cloned+0xe4>
mov    -0x20(%rbp),%eax
mov    0x2d8c(%rip),%rcx
and    (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jl     127d <is_self_cloned+0x10d>
cmpq   $0x0,-0x18(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1282 <is_self_cloned+0x112>
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    0x2e6a(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe70(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11bd <is_self_cloned+0x5d>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x40a,%esi
call   1040 <fcntl@plt>
test   %eax,%eax
js     11d1 <is_self_cloned+0x71>
xor    %ebp,%ebp
cmp    $0xf,%eax
sete   %bpl
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11c8 <is_self_cloned+0x68>
mov    0x2e1c(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe37(%rip),%rdi
call   1030 <getenv@plt>
test   %eax,%eax
je     1222 <is_self_cloned+0xc2>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1070 <fstatfs@plt>
mov    0x2de4(%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   1080 <fstat@plt>
test   %rax,%rax
js     11b4 <is_self_cloned+0x54>
xor    %eax,%eax
cmpq   $0x0,0x10(%rsp)
sete   %al
or     %eax,%ebp
jmp    11b4 <is_self_cloned+0x54>
xor    %ebp,%ebp
jmp    11b4 <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
@ENOTRECOVERABLE = global i32 0
@.str.1 = [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 %18
15: ; preds = %0
%16 = load i32, i32* @ENOTRECOVERABLE
%17 = sub nsw i32 0, %16
store i32 %17, i32* %1
br label %58
18: ; preds = %0
%19 = load i32, i32* %2
%20 = call i32 @fcntl(i32 %19, i32 1034)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, 15
%26 = zext i1 %25 to i32
store i32 %26, i32* %4
br label %54
27: ; preds = %18
%28 = call i32 @getenv(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %27
store i32 0, i32* %4
br label %54
31: ; preds = %27
%32 = load i32, i32* %2
%33 = call i64 @fstatfs(i32 %32, %struct.statfs* %6)
%34 = icmp sge i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = getelementptr inbounds %struct.statfs, %struct.statfs* %6, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @MS_RDONLY
%39 = and i32 %37, %38
%40 = load i32, i32* %4
%41 = or i32 %40, %39
store i32 %41, i32* %4
br label %42
42: ; preds = %35, %31
%43 = load i32, i32* %2
%44 = call i64 @fstat(i32 %43, %struct.stat* %5)
%45 = icmp sge i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %42
%47 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%48 = load i64, i64* %47
%49 = icmp eq i64 %48, 0
%50 = zext i1 %49 to i32
%51 = load i32, i32* %4
%52 = or i32 %51, %50
store i32 %52, i32* %4
br label %53
53: ; preds = %46, %42
br label %54
54: ; preds = %53, %30, %23
%55 = load i32, i32* %2
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %4
store i32 %57, i32* %1
br label %58
58: ; preds = %54, %15
%59 = load i32, i32* %1
ret i32 %59
}

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
@ENOTRECOVERABLE = global i32 0
@.str.1 = [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 %15
12: ; preds = %0
%13 = load i32, i32* @ENOTRECOVERABLE
%14 = sub nsw i32 0, %13
br label %41
15: ; preds = %0
%16 = tail call i32 @fcntl(i32 %10, i32 1034)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = icmp eq i32 %16, 15
%20 = zext i1 %19 to i32
br label %38
21: ; preds = %15
%22 = tail call i32 @getenv(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32 %22, 0
br i1 %23, label %38, label %24
24: ; preds = %21
%25 = call i64 @fstatfs(i32 %10, %struct.statfs* %2)
%26 = icmp sgt i64 %25, -1
%27 = load i32, i32* %6
%28 = load i32, i32* @MS_RDONLY
%29 = and i32 %28, %27
%30 = select i1 %26, i32 %29, i32 0
%31 = call i64 @fstat(i32 %10, %struct.stat* %1)
%32 = icmp sgt i64 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %24
%34 = load i64, i64* %4
%35 = icmp eq i64 %34, 0
%36 = zext i1 %35 to i32
%37 = or i32 %30, %36
br label %38
38: ; preds = %21, %24, %33, %18
%39 = phi i32 [ %20, %18 ], [ %37, %33 ], [ %30, %24 ], [ 0, %21 ]
%40 = call i32 @close(i32 %10)
br label %41
41: ; preds = %38, %12
%42 = phi i32 [ %14, %12 ], [ %39, %38 ]
ret i32 %42
}