#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 port_attributes {
  int dummy;
};
typedef int /*<<< orphan*/ link;

/* Variables and functions */
struct port_attributes *get_port_attribs(char *);
int readlink(char *, char *, int);
int strlen(char *);
char *strstr(char *, char *);
struct port_attributes *get_port_attribs_by_device(char *path) {
  char link[1024];
  char *host;
  char *rport;
  int ret;
  ret = readlink(path, link, sizeof(link));
  if (ret == -1)
    return NULL;
  host = strstr(link, "host");
  rport = strstr(link, "rport");
  host[strlen(host) - strlen(rport) - 1] = '\0';
  return get_port_attribs(host);
}


x86 assembly (clang, -O0):
<get_port_attribs_by_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    1184 <get_port_attribs_by_device+0x44>
movq   $0x0,-0x8(%rbp)
jmp    120f <get_port_attribs_by_device+0xcf>
lea    -0x410(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x420(%rbp)
mov    -0x418(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x418(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x434(%rbp)
mov    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x434(%rbp),%ecx
mov    %eax,%edx
mov    -0x430(%rbp),%rax
sub    %edx,%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x418(%rbp),%rdi
call   1050 <get_port_attribs@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_port_attribs_by_device>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsp,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
je     11aa <get_port_attribs_by_device+0x6a>
lea    0xe9c(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1060 <strstr@plt>
mov    %rax,%rbx
lea    0xe8c(%rip),%rsi
mov    %r14,%rdi
call   1060 <strstr@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
not    %eax
add    %ebp,%eax
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1050 <get_port_attribs@plt>
jmp    11ac <get_port_attribs_by_device+0x6c>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.port_attributes = type { i32 }
@.str = [5 x i8] c"host\00"
@.str.1 = [6 x i8] c"rport\00"
define %struct.port_attributes* @get_port_attribs_by_device(i8* %0) {
%2 = alloca %struct.port_attributes*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = call i32 @readlink(i8* %8, i8* %9, i32 1024)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store %struct.port_attributes* null, %struct.port_attributes** %2
br label %30
14: ; preds = %1
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = call i8* @strstr(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %5
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%18 = call i8* @strstr(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %6
%19 = load i8*, i8** %5
%20 = load i8*, i8** %5
%21 = call i32 @strlen(i8* %20)
%22 = load i8*, i8** %6
%23 = call i32 @strlen(i8* %22)
%24 = sub nsw i32 %21, %23
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %19, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %5
%29 = call %struct.port_attributes* @get_port_attribs(i8* %28)
store %struct.port_attributes* %29, %struct.port_attributes** %2
br label %30
30: ; preds = %14, %13
%31 = load %struct.port_attributes*, %struct.port_attributes** %2
ret %struct.port_attributes* %31
}

LLVM IR (-O2):
%struct.port_attributes = type { i32 }
@.str = [5 x i8] c"host\00"
@.str.1 = [6 x i8] c"rport\00"
define %struct.port_attributes* @get_port_attribs_by_device(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @readlink(i8* %0, i8* %3, i32 1024)
%5 = icmp eq i32 %4, -1
br i1 %5, label %16, label %6
6: ; preds = %1
%7 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @strlen(i8* %7)
%10 = call i32 @strlen(i8* %8)
%11 = xor i32 %10, -1
%12 = add i32 %9, %11
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %7, i64 %13
store i8 0, i8* %14
%15 = call %struct.port_attributes* @get_port_attribs(i8* %7)
br label %16
16: ; preds = %1, %6
%17 = phi %struct.port_attributes* [ %15, %6 ], [ null, %1 ]
ret %struct.port_attributes* %17
}