#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 /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strrchr(char *, char);
int intf_master_name(const char *iface, char *master) {
  int fd, len;
  char path[256];
  char buf[256];
  char *ptr;
  snprintf(path, 256, "/sys/class/net/%s/master", iface);
  fd = open(path, O_RDONLY);
  if (fd == -1)
    return -1;
  len = read(fd, buf, sizeof(buf) - 1);
  if (len < 0) {
    len = readlink(path, buf, sizeof(buf) - 1);
    if (len < 0)
      goto cleanup;
  }
  buf[len] = '\0';
  ptr = strrchr(buf, '/');
  if (ptr) {
    ptr++;
    strcpy(buf, ptr);
  }
  strcpy(master, buf);
cleanup:
  close(fd);
  return (len > 0) ? 0 : -1;
}


x86 assembly (clang, -O0):
<intf_master_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe66(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11cd <intf_master_name+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <intf_master_name+0x12f>
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    121d <intf_master_name+0xad>
lea    -0x120(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0xff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1218 <intf_master_name+0xa8>
jmp    1284 <intf_master_name+0x114>
jmp    121d <intf_master_name+0xad>
movslq -0x20(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
je     1274 <intf_master_name+0x104>
mov    -0x228(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    -0x220(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovg  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<intf_master_name>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r14
mov    %rdi,%rcx
lea    0xe78(%rip),%rdx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1229 <intf_master_name+0xb9>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e5 <intf_master_name+0x75>
lea    0x100(%rsp),%rdi
mov    %rsp,%rsi
mov    $0xff,%edx
call   1040 <readlink@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1217 <intf_master_name+0xa7>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     120c <intf_master_name+0x9c>
add    $0x1,%rax
mov    %rsp,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
test   %ebx,%ebx
setle  %al
neg    %eax
jmp    122e <intf_master_name+0xbe>
mov    $0xffffffff,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"/sys/class/net/%s/master\00"
@O_RDONLY = global i32 0
define i32 @intf_master_name(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca [256 x i8]
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%12 = load i8*, i8** %4
%13 = call i32 @snprintf(i8* %11, i32 256, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %59
20: ; preds = %2
%21 = load i32, i32* %6
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 255)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %20
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%29 = call i32 @readlink(i8* %27, i8* %28, i32 255)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %52
33: ; preds = %26
br label %34
34: ; preds = %33, %20
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 %36
store i8 0, i8* %37
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%39 = call i8* @strrchr(i8* %38, i8 signext 47)
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %48
42: ; preds = %34
%43 = load i8*, i8** %10
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %10
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%46 = load i8*, i8** %10
%47 = call i32 @strcpy(i8* %45, i8* %46)
br label %48
48: ; preds = %42, %34
%49 = load i8*, i8** %5
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%51 = call i32 @strcpy(i8* %49, i8* %50)
br label %52
52: ; preds = %48, %32
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %7
%56 = icmp sgt i32 %55, 0
%57 = zext i1 %56 to i64
%58 = select i1 %56, i32 0, i32 -1
store i32 %58, i32* %3
br label %59
59: ; preds = %52, %19
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [25 x i8] c"/sys/class/net/%s/master\00"
@O_RDONLY = global i32 0
define i32 @intf_master_name(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %33, label %11
11: ; preds = %2
%12 = call i32 @read(i32 %9, i8* %6, i32 255)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @readlink(i8* %5, i8* %6, i32 255)
%16 = icmp slt i32 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %14, %11
%18 = phi i32 [ %15, %14 ], [ %12, %11 ]
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i8* @strrchr(i8* %6, i8 signext 47)
%22 = icmp eq i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %17
%24 = getelementptr inbounds i8, i8* %21, i64 1
%25 = call i32 @strcpy(i8* %6, i8* %24)
br label %26
26: ; preds = %23, %17
%27 = call i32 @strcpy(i8* %1, i8* %6)
br label %28
28: ; preds = %14, %26
%29 = phi i32 [ %15, %14 ], [ %18, %26 ]
%30 = call i32 @close(i32 %9)
%31 = icmp slt i32 %29, 1
%32 = sext i1 %31 to i32
br label %33
33: ; preds = %2, %28
%34 = phi i32 [ %32, %28 ], [ -1, %2 ]
ret i32 %34
}