#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 stat {
  scalar_t__ st_nlink;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int ENOTRECOVERABLE;
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int open(char *, int);
int is_self_cloned(void) {
  int fd, ret, is_cloned = 0;
  fd = open("/proc/self/exe", O_RDONLY | O_CLOEXEC);
  if (fd < 0)
    return -ENOTRECOVERABLE;
  struct stat statbuf = {0};
  ret = fstat(fd, &statbuf);
  if (ret >= 0)
    is_cloned = (statbuf.st_nlink == 0);
  close(fd);
  return is_cloned;
}


x86 assembly (clang, -O0):
<is_self_cloned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x10(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118d <is_self_cloned+0x4d>
xor    %eax,%eax
mov    0x2e5d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <is_self_cloned+0x94>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11c6 <is_self_cloned+0x86>
cmpq   $0x0,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <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
mov    0x2eab(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xeb1(%rip),%rdi
call   1040 <open@plt>
test   %eax,%eax
js     1186 <is_self_cloned+0x56>
mov    %eax,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <fstat@plt>
cmpq   $0x0,(%rsp)
sete   %cl
test   %eax,%eax
setns  %al
and    %cl,%al
movzbl %al,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
jmp    1191 <is_self_cloned+0x61>
mov    0x2e53(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [15 x i8] c"/proc/self/exe\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@ENOTRECOVERABLE = global i32 0
define i32 @is_self_cloned() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
store i32 0, i32* %4
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_CLOEXEC
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%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
store i32 %14, i32* %1
br label %30
15: ; preds = %0
%16 = bitcast %struct.stat* %5 to i8*
%17 = load i32, i32* %2
%18 = call i32 @fstat(i32 %17, %struct.stat* %5)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
%25 = zext i1 %24 to i32
store i32 %25, i32* %4
br label %26
26: ; preds = %21, %15
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %4
store i32 %29, i32* %1
br label %30
30: ; preds = %26, %12
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [15 x i8] c"/proc/self/exe\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@ENOTRECOVERABLE = global i32 0
define i32 @is_self_cloned() {
%1 = alloca %struct.stat
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @O_CLOEXEC
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @ENOTRECOVERABLE
%9 = sub nsw i32 0, %8
br label %20
10: ; preds = %0
%11 = bitcast %struct.stat* %1 to i8*
%12 = call i32 @fstat(i32 %5, %struct.stat* %1)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%14 = load i64, i64* %13
%15 = icmp eq i64 %14, 0
%16 = icmp sgt i32 %12, -1
%17 = select i1 %16, i1 %15, i1 false
%18 = zext i1 %17 to i32
%19 = tail call i32 @close(i32 %5)
br label %20
20: ; preds = %10, %7
%21 = phi i32 [ %9, %7 ], [ %18, %10 ]
ret i32 %21
}