#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 ifreq {
  int /*<<< orphan*/ ifr_newname;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ req;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ AF_PACKET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCSIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ eslib_string_copy(int /*<<< orphan*/, char *, scalar_t__,
                             int /*<<< orphan*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
scalar_t__ strnlen(char *, scalar_t__);
int eslib_rtnetlink_linksetname(char *name, char *newname) {
  struct ifreq req;
  int fd;
  if (strnlen(name, IFNAMSIZ + 1) > IFNAMSIZ) {
    printf("name too long\n");
    return -1;
  }
  if (strnlen(newname, IFNAMSIZ + 1) > IFNAMSIZ) {
    printf("name too long\n");
    return -1;
  }
  memset(&req, 0, sizeof(req));
  if (eslib_string_copy(req.ifr_name, name, IFNAMSIZ, NULL))
    return -1;
  if (eslib_string_copy(req.ifr_newname, newname, IFNAMSIZ, NULL))
    return -1;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    fd = socket(AF_PACKET, SOCK_DGRAM, 0);
    if (fd == -1) {
      fd = socket(AF_INET6, SOCK_DGRAM, 0);
      if (fd == -1) {
        printf("couldn't open socket\n");
        return -1;
      }
    }
  }
  if (ioctl(fd, SIOCSIFNAME, &req)) {
    printf("SIOCSIFNAME: %s\n", strerror(errno));
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<eslib_rtnetlink_linksetname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
call   1060 <strnlen@plt>
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%rax
jle    11d1 <eslib_rtnetlink_linksetname+0x51>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <eslib_rtnetlink_linksetname+0x1f9>
mov    -0x18(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
call   1060 <strnlen@plt>
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jle    1212 <eslib_rtnetlink_linksetname+0x92>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <eslib_rtnetlink_linksetname+0x1f9>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2d90(%rip),%rax
mov    (%rax),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <eslib_string_copy@plt>
cmp    $0x0,%rax
je     1252 <eslib_rtnetlink_linksetname+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <eslib_rtnetlink_linksetname+0x1f9>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2d60(%rip),%rax
mov    (%rax),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <eslib_string_copy@plt>
cmp    $0x0,%rax
je     1282 <eslib_rtnetlink_linksetname+0x102>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <eslib_rtnetlink_linksetname+0x1f9>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1318 <eslib_rtnetlink_linksetname+0x198>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1313 <eslib_rtnetlink_linksetname+0x193>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    130e <eslib_rtnetlink_linksetname+0x18e>
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <eslib_rtnetlink_linksetname+0x1f9>
jmp    1313 <eslib_rtnetlink_linksetname+0x193>
jmp    1318 <eslib_rtnetlink_linksetname+0x198>
mov    -0x24(%rbp),%edi
mov    0x2c8e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     136a <eslib_rtnetlink_linksetname+0x1ea>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xcd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <eslib_rtnetlink_linksetname+0x1f9>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_rtnetlink_linksetname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e17(%rip),%r15
mov    (%r15),%rbp
lea    0x1(%rbp),%rbx
mov    %rbx,%rsi
call   1070 <strnlen@plt>
cmp    %rbp,%rax
jg     11cd <eslib_rtnetlink_linksetname+0x3d>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <strnlen@plt>
cmp    %rbp,%rax
jle    11ed <eslib_rtnetlink_linksetname+0x5d>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    (%r15),%rdx
mov    %r12,%rsi
xor    %ecx,%ecx
call   1090 <eslib_string_copy@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11de <eslib_rtnetlink_linksetname+0x4e>
mov    0x8(%rsp),%edi
mov    (%r15),%rdx
mov    %r14,%rsi
xor    %ecx,%ecx
call   1090 <eslib_string_copy@plt>
test   %rax,%rax
jne    11de <eslib_rtnetlink_linksetname+0x4e>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    0x2da1(%rip),%r14
mov    (%r14),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1284 <eslib_rtnetlink_linksetname+0xf4>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1284 <eslib_rtnetlink_linksetname+0xf4>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12d7 <eslib_rtnetlink_linksetname+0x147>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
je     12c9 <eslib_rtnetlink_linksetname+0x139>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd4d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    11de <eslib_rtnetlink_linksetname+0x4e>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %ebp,%ebp
jmp    11de <eslib_rtnetlink_linksetname+0x4e>
lea    0xd33(%rip),%rdi
call   1030 <puts@plt>
jmp    11de <eslib_rtnetlink_linksetname+0x4e>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i64 0
@.str = [15 x i8] c"name too long\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_PACKET = global i32 0
@AF_INET6 = global i32 0
@.str.1 = [22 x i8] c"couldn't open socket\0A\00"
@SIOCSIFNAME = global i32 0
@.str.2 = [17 x i8] c"SIOCSIFNAME: %s\0A\00"
@errno = global i32 0
define i32 @eslib_rtnetlink_linksetname(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = load i64, i64* @IFNAMSIZ
%10 = add nsw i64 %9, 1
%11 = call i64 @strnlen(i8* %8, i64 %10)
%12 = load i64, i64* @IFNAMSIZ
%13 = icmp sgt i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %78
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = load i64, i64* @IFNAMSIZ
%19 = add nsw i64 %18, 1
%20 = call i64 @strnlen(i8* %17, i64 %19)
%21 = load i64, i64* @IFNAMSIZ
%22 = icmp sgt i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %78
25: ; preds = %16
%26 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %4
%30 = load i64, i64* @IFNAMSIZ
%31 = call i64 @eslib_string_copy(i32 %28, i8* %29, i64 %30, i32* null)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 -1, i32* %3
br label %78
34: ; preds = %25
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i8*, i8** %5
%38 = load i64, i64* @IFNAMSIZ
%39 = call i64 @eslib_string_copy(i32 %36, i8* %37, i64 %38, i32* null)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %34
store i32 -1, i32* %3
br label %78
42: ; preds = %34
%43 = load i32, i32* @AF_INET
%44 = load i32, i32* @SOCK_DGRAM
%45 = call i32 @socket(i32 %43, i32 %44, i32 0)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %64
48: ; preds = %42
%49 = load i32, i32* @AF_PACKET
%50 = load i32, i32* @SOCK_DGRAM
%51 = call i32 @socket(i32 %49, i32 %50, i32 0)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %63
54: ; preds = %48
%55 = load i32, i32* @AF_INET6
%56 = load i32, i32* @SOCK_DGRAM
%57 = call i32 @socket(i32 %55, i32 %56, i32 0)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %78
62: ; preds = %54
br label %63
63: ; preds = %62, %48
br label %64
64: ; preds = %63, %42
%65 = load i32, i32* %7
%66 = load i32, i32* @SIOCSIFNAME
%67 = call i64 @ioctl(i32 %65, i32 %66, %struct.ifreq* %6)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %64
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %71)
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
store i32 -1, i32* %3
br label %78
75: ; preds = %64
%76 = load i32, i32* %7
%77 = call i32 @close(i32 %76)
store i32 0, i32* %3
br label %78
78: ; preds = %75, %69, %60, %41, %33, %23, %14
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i64 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_PACKET = global i32 0
@AF_INET6 = global i32 0
@SIOCSIFNAME = global i32 0
@.str.2 = [17 x i8] c"SIOCSIFNAME: %s\0A\00"
@errno = global i32 0
@str = [21 x i8] c"couldn't open socket\00"
@str.4 = [14 x i8] c"name too long\00"
define i32 @eslib_rtnetlink_linksetname(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i64, i64* @IFNAMSIZ
%6 = add nsw i64 %5, 1
%7 = tail call i64 @strnlen(i8* %0, i64 %6)
%8 = icmp sgt i64 %7, %5
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
br label %58
11: ; preds = %2
%12 = tail call i64 @strnlen(i8* %1, i64 %6)
%13 = icmp sgt i64 %12, %5
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
br label %58
16: ; preds = %11
%17 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%19 = load i32, i32* %18
%20 = load i64, i64* @IFNAMSIZ
%21 = call i64 @eslib_string_copy(i32 %19, i8* %0, i64 %20, i32* null)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %58
23: ; preds = %16
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = load i64, i64* @IFNAMSIZ
%27 = call i64 @eslib_string_copy(i32 %25, i8* %1, i64 %26, i32* null)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %58
29: ; preds = %23
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_DGRAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %46
34: ; preds = %29
%35 = load i32, i32* @AF_PACKET
%36 = load i32, i32* @SOCK_DGRAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %46
39: ; preds = %34
%40 = load i32, i32* @AF_INET6
%41 = load i32, i32* @SOCK_DGRAM
%42 = call i32 @socket(i32 %40, i32 %41, i32 0)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %58
46: ; preds = %34, %39, %29
%47 = phi i32 [ %42, %39 ], [ %37, %34 ], [ %32, %29 ]
%48 = load i32, i32* @SIOCSIFNAME
%49 = call i64 @ioctl(i32 %47, i32 %48, %struct.ifreq* %3)
%50 = icmp eq i64 %49, 0
br i1 %50, label %56, label %51
51: ; preds = %46
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %53)
%55 = call i32 @close(i32 %47)
br label %58
56: ; preds = %46
%57 = call i32 @close(i32 %47)
br label %58
58: ; preds = %23, %16, %56, %51, %44, %14, %9
%59 = phi i32 [ -1, %9 ], [ -1, %14 ], [ -1, %44 ], [ -1, %51 ], [ 0, %56 ], [ -1, %16 ], [ -1, %23 ]
ret i32 %59
}