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

/* Variables and functions */
int asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ free(char *);
int readlink(char *, char *, int);
char *strdup(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *get_ifname_from_rport(char *rport) {
  char link[1024];
  char ifname[32];
  ssize_t ret;
  char *path;
  char *offs;
  int err;
  int i = 0;
  err = asprintf(&path, "%s/%s", "/sys/class/fc_remote_ports", rport);
  if (err == -1)
    return NULL;
  ret = readlink(path, link, sizeof(link));
  free(path);
  if (ret == -1)
    return NULL;
  if (link[ret] != '\0')
    link[ret] = '\0';
  offs = strstr(link, "/net/");
  if (!offs)
    return NULL;
  offs = offs + 5;
  for (i = 0; offs[i] != '\0'; i++)
    if (offs[i] == '/')
      break;
  strncpy(ifname, offs, i);
  if (ifname[i] != '\0')
    ifname[i] = '\0';
  return strdup(ifname);
}


x86 assembly (clang, -O0):
<get_ifname_from_rport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x450(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x440(%rbp),%rdi
lea    0xe75(%rip),%rsi
lea    0xe74(%rip),%rdx
call   1060 <asprintf@plt>
mov    %eax,-0x44c(%rbp)
cmpl   $0xffffffff,-0x44c(%rbp)
jne    11b7 <get_ifname_from_rport+0x57>
movq   $0x0,-0x8(%rbp)
jmp    1317 <get_ifname_from_rport+0x1b7>
mov    -0x440(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <readlink@plt>
mov    %eax,-0x434(%rbp)
mov    -0x440(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x434(%rbp)
jne    11fb <get_ifname_from_rport+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    1317 <get_ifname_from_rport+0x1b7>
movslq -0x434(%rbp),%rax
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
je     1222 <get_ifname_from_rport+0xc2>
movslq -0x434(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
jne    1257 <get_ifname_from_rport+0xf7>
movq   $0x0,-0x8(%rbp)
jmp    1317 <get_ifname_from_rport+0x1b7>
mov    -0x448(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x448(%rbp)
movl   $0x0,-0x450(%rbp)
mov    -0x448(%rbp),%rax
movslq -0x450(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     12c7 <get_ifname_from_rport+0x167>
mov    -0x448(%rbp),%rax
movslq -0x450(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    12ae <get_ifname_from_rport+0x14e>
jmp    12c7 <get_ifname_from_rport+0x167>
jmp    12b3 <get_ifname_from_rport+0x153>
mov    -0x450(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x450(%rbp)
jmp    1273 <get_ifname_from_rport+0x113>
lea    -0x430(%rbp),%rdi
mov    -0x448(%rbp),%rsi
mov    -0x450(%rbp),%edx
call   1040 <strncpy@plt>
movslq -0x450(%rbp),%rax
movsbl -0x430(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
je     1307 <get_ifname_from_rport+0x1a7>
movslq -0x450(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ifname_from_rport>:
push   %rbx
sub    $0x430,%rsp
mov    %rdi,%rcx
lea    0xe8e(%rip),%rsi
lea    0xe8d(%rip),%rdx
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     11f3 <get_ifname_from_rport+0x93>
mov    0x8(%rsp),%rdi
lea    0x30(%rsp),%rsi
mov    $0x400,%edx
call   1050 <readlink@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
je     11f3 <get_ifname_from_rport+0x93>
movslq %ebx,%rax
cmpb   $0x0,0x30(%rsp,%rax,1)
je     11bc <get_ifname_from_rport+0x5c>
movb   $0x0,0x30(%rsp,%rax,1)
lea    0xe5e(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     11f3 <get_ifname_from_rport+0x93>
add    $0x5,%rax
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
movzbl (%rax,%rdx,1),%ecx
test   %cl,%cl
je     11f7 <get_ifname_from_rport+0x97>
cmp    $0x2f,%cl
je     11f7 <get_ifname_from_rport+0x97>
add    $0x1,%rdx
jmp    11e0 <get_ifname_from_rport+0x80>
xor    %eax,%eax
jmp    121c <get_ifname_from_rport+0xbc>
mov    %edx,%ebx
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
call   1040 <strncpy@plt>
cmpb   $0x0,0x10(%rsp,%rbx,1)
je     1212 <get_ifname_from_rport+0xb2>
movb   $0x0,0x10(%rsp,%rbx,1)
lea    0x10(%rsp),%rdi
call   1070 <strdup@plt>
add    $0x430,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [27 x i8] c"/sys/class/fc_remote_ports\00"
@.str.2 = [6 x i8] c"/net/\00"
define i8* @get_ifname_from_rport(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca [32 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %10
%11 = load i8*, i8** %3
%12 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %1
store i8* null, i8** %2
br label %84
16: ; preds = %1
%17 = load i8*, i8** %7
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%19 = call i32 @readlink(i8* %17, i8* %18, i32 1024)
store i32 %19, i32* %6
%20 = load i8*, i8** %7
%21 = call i32 @free(i8* %20)
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %16
store i8* null, i8** %2
br label %84
25: ; preds = %16
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %32, %25
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%38 = call i8* @strstr(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* %38, i8** %8
%39 = load i8*, i8** %8
%40 = icmp ne i8* %39, null
br i1 %40, label %42, label %41
41: ; preds = %36
store i8* null, i8** %2
br label %84
42: ; preds = %36
%43 = load i8*, i8** %8
%44 = getelementptr inbounds i8, i8* %43, i64 5
store i8* %44, i8** %8
store i32 0, i32* %10
br label %45
45: ; preds = %63, %42
%46 = load i8*, i8** %8
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %66
53: ; preds = %45
%54 = load i8*, i8** %8
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 47
br i1 %60, label %61, label %62
61: ; preds = %53
br label %66
62: ; preds = %53
br label %63
63: ; preds = %62
%64 = load i32, i32* %10
%65 = add nsw i32 %64, 1
store i32 %65, i32* %10
br label %45
66: ; preds = %61, %45
%67 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%68 = load i8*, i8** %8
%69 = load i32, i32* %10
%70 = call i32 @strncpy(i8* %67, i8* %68, i32 %69)
%71 = load i32, i32* %10
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 %72
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %81
77: ; preds = %66
%78 = load i32, i32* %10
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 %79
store i8 0, i8* %80
br label %81
81: ; preds = %77, %66
%82 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%83 = call i8* @strdup(i8* %82)
store i8* %83, i8** %2
br label %84
84: ; preds = %81, %41, %24, %15
%85 = load i8*, i8** %2
ret i8* %85
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [27 x i8] c"/sys/class/fc_remote_ports\00"
@.str.2 = [6 x i8] c"/net/\00"
define noalias i8* @get_ifname_from_rport(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [32 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = bitcast i8** %4 to i8*
%8 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %43, label %10
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = call i32 @readlink(i8* %11, i8* %5, i32 1024)
%13 = load i8*, i8** %4
%14 = call i32 @free(i8* %13)
%15 = icmp eq i32 %12, -1
br i1 %15, label %43, label %16
16: ; preds = %10
%17 = sext i32 %12 to i64
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %16
store i8 0, i8* %18
br label %22
22: ; preds = %21, %16
%23 = call i8* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %43, label %25
25: ; preds = %22
%26 = getelementptr inbounds i8, i8* %23, i64 5
br label %27
27: ; preds = %31, %25
%28 = phi i64 [ %32, %31 ], [ 0, %25 ]
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
switch i8 %30, label %31 [
i8 0, label %33
i8 47, label %33
]
31: ; preds = %27
%32 = add nuw i64 %28, 1
br label %27
33: ; preds = %27, %27
%34 = trunc i64 %28 to i32
%35 = and i64 %28, 4294967295
%36 = call i32 @strncpy(i8* %6, i8* %26, i32 %34)
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %35
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %33
store i8 0, i8* %37
br label %41
41: ; preds = %40, %33
%42 = call i8* @strdup(i8* %6)
br label %43
43: ; preds = %22, %10, %1, %41
%44 = phi i8* [ %42, %41 ], [ null, %1 ], [ null, %10 ], [ null, %22 ]
ret i8* %44
}