#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 size_t ssize_t;
typedef int /*<<< orphan*/ dest;

/* Variables and functions */
long EINVAL;
int PATH_MAX;
long errno;
size_t readlink(char const *, char *, int);
scalar_t__ strncmp(char *, char *, int);
char *strrchr(char *, char);
long strtol(char *, char **, int);
long netns_get_kernel_id(const char *path) {
  char dest[PATH_MAX], *s, *endptr;
  ssize_t len;
  long result;
  len = readlink(path, dest, sizeof(dest));
  if (len < 0)
    return -errno;
  dest[len] = 0;
  if (strncmp("net:[", dest, 5))
    return -EINVAL;
  s = strrchr(dest, ']');
  if (!s)
    return -EINVAL;
  *s = '\0';
  s = dest + 5;
  result = strtol(s, &endptr, 10);
  if (!*s || *endptr)
    return -EINVAL;
  return result;
}


x86 assembly (clang, -O0):
<netns_get_kernel_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x50(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    %eax,%edx
call   1040 <readlink@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jae    11ae <netns_get_kernel_id+0x6e>
xor    %eax,%eax
mov    0x2e3d(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    129b <netns_get_kernel_id+0x15b>
mov    -0x50(%rbp),%rsi
mov    -0x38(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xe3f(%rip),%rdi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11f1 <netns_get_kernel_id+0xb1>
xor    %eax,%eax
mov    0x2df2(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    129b <netns_get_kernel_id+0x15b>
mov    -0x50(%rbp),%rdi
mov    $0x5d,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122a <netns_get_kernel_id+0xea>
xor    %eax,%eax
mov    0x2db9(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    129b <netns_get_kernel_id+0x15b>
mov    -0x50(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rcx)
add    $0x5,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1270 <netns_get_kernel_id+0x130>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     128c <netns_get_kernel_id+0x14c>
xor    %eax,%eax
mov    0x2d57(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    129b <netns_get_kernel_id+0x15b>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netns_get_kernel_id>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2ea3(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1040 <readlink@plt>
movb   $0x0,(%rax,%rbx,1)
lea    0xe8a(%rip),%rdi
mov    %rbx,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     119b <netns_get_kernel_id+0x5b>
mov    0x2e49(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%rax
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret
mov    %rbx,%rdi
mov    $0x5d,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1188 <netns_get_kernel_id+0x48>
movb   $0x0,(%rax)
lea    0x5(%rbx),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
cmpb   $0x0,0x5(%rbx)
je     1188 <netns_get_kernel_id+0x48>
mov    -0x10(%rbp),%rcx
cmpb   $0x0,(%rcx)
jne    1188 <netns_get_kernel_id+0x48>
jmp    1194 <netns_get_kernel_id+0x54>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@errno = global i64 0
@.str = [6 x i8] c"net:[\00"
@EINVAL = global i64 0
define i64 @netns_get_kernel_id(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %4
%14 = alloca i8, i64 %12
store i64 %12, i64* %5
%15 = load i8*, i8** %3
%16 = trunc i64 %12 to i32
%17 = call i64 @readlink(i8* %15, i8* %14, i32 %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp ult i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = load i64, i64* @errno
%22 = sub nsw i64 0, %21
store i64 %22, i64* %2
store i32 1, i32* %10
br label %56
23: ; preds = %1
%24 = load i64, i64* %8
%25 = getelementptr inbounds i8, i8* %14, i64 %24
store i8 0, i8* %25
%26 = call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14, i32 5)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i64, i64* @EINVAL
%30 = sub nsw i64 0, %29
store i64 %30, i64* %2
store i32 1, i32* %10
br label %56
31: ; preds = %23
%32 = call i8* @strrchr(i8* %14, i8 signext 93)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = icmp ne i8* %33, null
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = load i64, i64* @EINVAL
%37 = sub nsw i64 0, %36
store i64 %37, i64* %2
store i32 1, i32* %10
br label %56
38: ; preds = %31
%39 = load i8*, i8** %6
store i8 0, i8* %39
%40 = getelementptr inbounds i8, i8* %14, i64 5
store i8* %40, i8** %6
%41 = load i8*, i8** %6
%42 = call i64 @strtol(i8* %41, i8** %7, i32 10)
store i64 %42, i64* %9
%43 = load i8*, i8** %6
%44 = load i8, i8* %43
%45 = icmp ne i8 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %38
%47 = load i8*, i8** %7
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %46, %38
%52 = load i64, i64* @EINVAL
%53 = sub nsw i64 0, %52
store i64 %53, i64* %2
store i32 1, i32* %10
br label %56
54: ; preds = %46
%55 = load i64, i64* %9
store i64 %55, i64* %2
store i32 1, i32* %10
br label %56
56: ; preds = %54, %51, %35, %28, %20
%57 = load i8*, i8** %4
%58 = load i64, i64* %2
ret i64 %58
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@errno = global i64 0
@.str = [6 x i8] c"net:[\00"
@EINVAL = global i64 0
define i64 @netns_get_kernel_id(i8* nocapture %0) {
%2 = alloca i8*
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast i8** %2 to i8*
%7 = call i64 @readlink(i8* %0, i8* %5, i32 %3)
%8 = getelementptr inbounds i8, i8* %5, i64 %7
store i8 0, i8* %8
%9 = call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %5, i32 5)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i64, i64* @EINVAL
%13 = sub nsw i64 0, %12
br label %32
14: ; preds = %1
%15 = call i8* @strrchr(i8* %5, i8 signext 93)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i64, i64* @EINVAL
%19 = sub nsw i64 0, %18
br label %32
20: ; preds = %14
store i8 0, i8* %15
%21 = getelementptr inbounds i8, i8* %5, i64 5
%22 = call i64 @strtol(i8* %21, i8** %2, i32 10)
%23 = load i8, i8* %21
%24 = icmp eq i8 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = load i8*, i8** %2
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25, %20
%30 = load i64, i64* @EINVAL
%31 = sub nsw i64 0, %30
br label %32
32: ; preds = %25, %29, %17, %11
%33 = phi i64 [ %13, %11 ], [ %31, %29 ], [ %19, %17 ], [ %22, %25 ]
ret i64 %33
}