#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 {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
__attribute__((constructor)) void ctor(void) {
  pid_t me = getpid();
  struct stat buf;
  fprintf(stderr, "LD_PRELOAD module got loaded by process %d\n", me);
  if (stat("/.flatpak-info", &buf) == 0) {
    fprintf(stderr, "OK: pid %d is in a Flatpak sandbox\n", me);
  } else {
    fprintf(stderr, "Error: pid %d is not in a Flatpak sandbox\n", me);
    abort();
  }
}


x86 assembly (clang, -O0):
<ctor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe9b(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xebb(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    11a1 <ctor+0x61>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xea4(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11c0 <ctor+0x80>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xeab(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctor>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
mov    0x2e8c(%rip),%r14
mov    (%r14),%edi
lea    0xea2(%rip),%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
lea    0xec0(%rip),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
mov    (%r14),%edi
test   %rax,%rax
jne    1192 <ctor+0x52>
lea    0xeb8(%rip),%rsi
mov    %ebx,%edx
call   1050 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xec6(%rip),%rsi
mov    %ebx,%edx
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [44 x i8] c"LD_PRELOAD module got loaded by process %d\0A\00"
@.str.1 = [15 x i8] c"/.flatpak-info\00"
@.str.2 = [36 x i8] c"OK: pid %d is in a Flatpak sandbox\0A\00"
@.str.3 = [43 x i8] c"Error: pid %d is not in a Flatpak sandbox\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @ctor, i8* null }]
define void @ctor() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = call i32 (...) @getpid()
store i32 %3, i32* %1
%4 = load i32, i32* @stderr
%5 = load i32, i32* %1
%6 = call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i64 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* %1
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %18
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = load i32, i32* %1
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %9
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [44 x i8] c"LD_PRELOAD module got loaded by process %d\0A\00"
@.str.1 = [15 x i8] c"/.flatpak-info\00"
@.str.2 = [36 x i8] c"OK: pid %d is in a Flatpak sandbox\0A\00"
@.str.3 = [43 x i8] c"Error: pid %d is not in a Flatpak sandbox\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @ctor, i8* null }]
define void @ctor() {
%1 = alloca %struct.stat
%2 = tail call i32 (...) @getpid()
%3 = bitcast %struct.stat* %1 to i8*
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %2)
%6 = call i64 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%7 = icmp eq i64 %6, 0
%8 = load i32, i32* @stderr
br i1 %7, label %9, label %11
9: ; preds = %0
%10 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %2)
ret void
11: ; preds = %0
%12 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %2)
%13 = call i32 (...) @abort()
unreachable
}