#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 */
typedef int pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
size_t max_pid_length_base_10();
int /*<<< orphan*/ perror(char *);
char *realloc(char *, size_t);
int /*<<< orphan*/ snprintf(char *, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ *strstr(char *, char const *);
bool process_is_in_docker_ns(int argc, const char **docker_ids,
                             pid_t child_pid) {
  int i;
  size_t max_pid_length = max_pid_length_base_10();
  char buff[13 + max_pid_length + 1];
  snprintf(buff, PATH_MAX, "/proc/%d/cgroup", child_pid);
  FILE *proc_file = fopen(buff, "r");
  if (proc_file == NULL) {
    return false;
  }
  size_t slurp_size = 1024;
  char *proc_cgroup = malloc(slurp_size + 1);
  size_t read = 0;
  while (true) {
    read += fread(proc_cgroup, slurp_size, 1, proc_file);
    if (ferror(proc_file)) {
      perror("Failed to read /proc/PID/cgroup file");
      fclose(proc_file);
      free(proc_cgroup);
      return false;
    }
    if (!feof(proc_file)) {
      slurp_size += 1024;
      proc_cgroup = realloc(proc_cgroup, slurp_size);
    } else {
      break;
    }
  }
  fclose(proc_file);
  if (read != 0) {
    proc_cgroup[read * 1024] = '\0';
    for (i = 0; i < argc; ++i) {
      if (strstr(proc_cgroup, docker_ids[i]) != NULL) {
        free(proc_cgroup);
        return true;
      }
    }
  }
  free(proc_cgroup);
  return false;
}


x86 assembly (clang, -O0):
<process_is_in_docker_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
call   10b0 <max_pid_length_base_10@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    %rcx,%rax
add    $0xe,%rax
mov    %rsp,%rdx
mov    %rdx,-0x28(%rbp)
add    $0x1d,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%ecx
lea    0xde5(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x60(%rbp),%rdi
lea    0xde5(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1252 <process_is_in_docker_ns+0x92>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    13bb <process_is_in_docker_ns+0x1fb>
movq   $0x400,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
jmp    1278 <process_is_in_docker_ns+0xb8>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
add    -0x58(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12da <process_is_in_docker_ns+0x11a>
lea    0xd62(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    13bb <process_is_in_docker_ns+0x1fb>
mov    -0x38(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
jne    1310 <process_is_in_docker_ns+0x150>
mov    -0x48(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   10a0 <realloc@plt>
mov    %rax,-0x50(%rbp)
jmp    1315 <process_is_in_docker_ns+0x155>
jmp    131a <process_is_in_docker_ns+0x15a>
jmp    1273 <process_is_in_docker_ns+0xb3>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
cmpq   $0x0,-0x58(%rbp)
je     13a4 <process_is_in_docker_ns+0x1e4>
mov    -0x50(%rbp),%rax
mov    -0x58(%rbp),%rcx
shl    $0xa,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    139f <process_is_in_docker_ns+0x1df>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   10e0 <strstr@plt>
cmp    $0x0,%rax
je     138c <process_is_in_docker_ns+0x1cc>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    13bb <process_is_in_docker_ns+0x1fb>
jmp    1391 <process_is_in_docker_ns+0x1d1>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1345 <process_is_in_docker_ns+0x185>
jmp    13a4 <process_is_in_docker_ns+0x1e4>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_is_in_docker_ns>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,-0x38(%rbp)
mov    %edi,%r15d
xor    %r13d,%r13d
xor    %eax,%eax
call   10b0 <max_pid_length_base_10@plt>
mov    %rsp,%rbx
add    $0x1d,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xdfa(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%ecx
call   1070 <snprintf@plt>
lea    0xdf8(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     132d <process_is_in_docker_ns+0x16d>
mov    %rax,%r12
mov    %r15d,-0x2c(%rbp)
mov    $0x401,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    $0x400,%esi
mov    %rax,%rdi
mov    $0x1,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    12b4 <process_is_in_docker_ns+0xf4>
mov    $0x800,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    12cd <process_is_in_docker_ns+0x10d>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
add    %rax,%r15
mov    %r12,%rdi
call   1040 <ferror@plt>
add    $0x400,%rbx
test   %rax,%rax
je     1270 <process_is_in_docker_ns+0xb0>
lea    0xd57(%rip),%rdi
call   10d0 <perror@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
xor    %r13d,%r13d
jmp    1325 <process_is_in_docker_ns+0x165>
mov    %r12,%rdi
call   1060 <fclose@plt>
xor    %r13d,%r13d
test   %r15,%r15
je     1325 <process_is_in_docker_ns+0x165>
shl    $0xa,%r15
movb   $0x0,(%r14,%r15,1)
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jle    1325 <process_is_in_docker_ns+0x165>
mov    %eax,%r15d
xor    %ebx,%ebx
mov    -0x38(%rbp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    (%r12,%rbx,8),%rsi
mov    %r14,%rdi
call   10e0 <strstr@plt>
test   %rax,%rax
jne    131f <process_is_in_docker_ns+0x15f>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1300 <process_is_in_docker_ns+0x140>
xor    %r13d,%r13d
jmp    1325 <process_is_in_docker_ns+0x165>
mov    $0x1,%r13d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/proc/%d/cgroup\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [37 x i8] c"Failed to read /proc/PID/cgroup file\00"
define i32 @process_is_in_docker_ns(i32 %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i64
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%17 = call i64 (...) @max_pid_length_base_10()
store i64 %17, i64* %9
%18 = load i64, i64* %9
%19 = add i64 13, %18
%20 = add i64 %19, 1
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %10
%22 = alloca i8, i64 %20
store i64 %20, i64* %11
%23 = load i32, i32* @PATH_MAX
%24 = load i32, i32* %7
%25 = call i32 @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %12
%27 = load i32*, i32** %12
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %3
store i32 0, i32* %4
store i32 1, i32* %13
br label %97
30: ; preds = %3
store i64 1024, i64* %14
%31 = load i64, i64* %14
%32 = add i64 %31, 1
%33 = call i8* @malloc(i64 %32)
store i8* %33, i8** %15
store i64 0, i64* %16
br label %34
34: ; preds = %62, %30
br label %35
35: ; preds = %34
%36 = load i8*, i8** %15
%37 = load i64, i64* %14
%38 = load i32*, i32** %12
%39 = call i64 @fread(i8* %36, i64 %37, i32 1, i32* %38)
%40 = load i64, i64* %16
%41 = add i64 %40, %39
store i64 %41, i64* %16
%42 = load i32*, i32** %12
%43 = call i64 @ferror(i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %35
%46 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32*, i32** %12
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %15
%50 = call i32 @free(i8* %49)
store i32 0, i32* %4
store i32 1, i32* %13
br label %97
51: ; preds = %35
%52 = load i32*, i32** %12
%53 = call i32 @feof(i32* %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %61, label %55
55: ; preds = %51
%56 = load i64, i64* %14
%57 = add i64 %56, 1024
store i64 %57, i64* %14
%58 = load i8*, i8** %15
%59 = load i64, i64* %14
%60 = call i8* @realloc(i8* %58, i64 %59)
store i8* %60, i8** %15
br label %62
61: ; preds = %51
br label %63
62: ; preds = %55
br label %34
63: ; preds = %61
%64 = load i32*, i32** %12
%65 = call i32 @fclose(i32* %64)
%66 = load i64, i64* %16
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %94
68: ; preds = %63
%69 = load i8*, i8** %15
%70 = load i64, i64* %16
%71 = mul i64 %70, 1024
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 0, i8* %72
store i32 0, i32* %8
br label %73
73: ; preds = %90, %68
%74 = load i32, i32* %8
%75 = load i32, i32* %5
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %93
77: ; preds = %73
%78 = load i8*, i8** %15
%79 = load i8**, i8*** %6
%80 = load i32, i32* %8
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8*, i8** %79, i64 %81
%83 = load i8*, i8** %82
%84 = call i32* @strstr(i8* %78, i8* %83)
%85 = icmp ne i32* %84, null
br i1 %85, label %86, label %89
86: ; preds = %77
%87 = load i8*, i8** %15
%88 = call i32 @free(i8* %87)
store i32 1, i32* %4
store i32 1, i32* %13
br label %97
89: ; preds = %77
br label %90
90: ; preds = %89
%91 = load i32, i32* %8
%92 = add nsw i32 %91, 1
store i32 %92, i32* %8
br label %73
93: ; preds = %73
br label %94
94: ; preds = %93, %63
%95 = load i8*, i8** %15
%96 = call i32 @free(i8* %95)
store i32 0, i32* %4
store i32 1, i32* %13
br label %97
97: ; preds = %94, %86, %45, %29
%98 = load i8*, i8** %10
%99 = load i32, i32* %4
ret i32 %99
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/proc/%d/cgroup\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [37 x i8] c"Failed to read /proc/PID/cgroup file\00"
define i32 @process_is_in_docker_ns(i32 %0, i8** nocapture %1, i32 %2) {
%4 = tail call i64 (...) @max_pid_length_base_10()
%5 = add i64 %4, 14
%6 = alloca i8, i64 %5
%7 = load i32, i32* @PATH_MAX
%8 = call i32 @snprintf(i8* %6, i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %2)
%9 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %55, label %11
11: ; preds = %3
%12 = call dereferenceable_or_null(1025) i8* @malloc(i64 1025)
%13 = call i64 @fread(i8* %12, i64 1024, i32 1, i32* %9)
%14 = call i64 @ferror(i32* %9)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %26, %11
%17 = phi i8* [ %12, %11 ], [ %28, %26 ]
%18 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @fclose(i32* %9)
br label %51
20: ; preds = %11, %26
%21 = phi i64 [ %30, %26 ], [ %13, %11 ]
%22 = phi i8* [ %28, %26 ], [ %12, %11 ]
%23 = phi i64 [ %27, %26 ], [ 1024, %11 ]
%24 = call i32 @feof(i32* %9)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = add i64 %23, 1024
%28 = call i8* @realloc(i8* %22, i64 %27)
%29 = call i64 @fread(i8* %28, i64 %27, i32 1, i32* %9)
%30 = add i64 %29, %21
%31 = call i64 @ferror(i32* %9)
%32 = icmp eq i64 %31, 0
br i1 %32, label %20, label %16
33: ; preds = %20
%34 = call i32 @fclose(i32* %9)
%35 = icmp eq i64 %21, 0
br i1 %35, label %51, label %36
36: ; preds = %33
%37 = shl i64 %21, 10
%38 = getelementptr inbounds i8, i8* %22, i64 %37
store i8 0, i8* %38
%39 = icmp sgt i32 %0, 0
br i1 %39, label %40, label %51
40: ; preds = %36
%41 = zext i32 %0 to i64
br label %44
42: ; preds = %44
%43 = icmp eq i64 %50, %41
br i1 %43, label %51, label %44
44: ; preds = %40, %42
%45 = phi i64 [ 0, %40 ], [ %50, %42 ]
%46 = getelementptr inbounds i8*, i8** %1, i64 %45
%47 = load i8*, i8** %46
%48 = call i32* @strstr(i8* dereferenceable(1) %22, i8* dereferenceable(1) %47)
%49 = icmp eq i32* %48, null
%50 = add nuw nsw i64 %45, 1
br i1 %49, label %42, label %51
51: ; preds = %42, %44, %33, %36, %16
%52 = phi i8* [ %17, %16 ], [ %22, %36 ], [ %22, %33 ], [ %22, %44 ], [ %22, %42 ]
%53 = phi i32 [ 0, %16 ], [ 0, %36 ], [ 0, %33 ], [ 0, %42 ], [ 1, %44 ]
%54 = call i32 @free(i8* %52)
br label %55
55: ; preds = %51, %3
%56 = phi i32 [ 0, %3 ], [ %53, %51 ]
ret i32 %56
}