#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;
};

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCSIFNAME;
int /*<<< orphan*/ close(int);
int get_ctl_fd();
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int do_changename(const char *dev, const char *newdev) {
  struct ifreq ifr;
  int fd;
  int err;
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  strncpy(ifr.ifr_newname, newdev, IFNAMSIZ);
  fd = get_ctl_fd();
  if (fd < 0)
    return -1;
  err = ioctl(fd, SIOCSIFNAME, &ifr);
  if (err) {
    perror("SIOCSIFNAME");
    close(fd);
    return -1;
  }
  close(fd);
  return err;
}


x86 assembly (clang, -O0):
<do_changename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    $0x0,%al
call   1070 <get_ctl_fd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11aa <do_changename+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <do_changename+0xaa>
mov    -0x24(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11ec <do_changename+0x9c>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <do_changename+0xaa>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_changename>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e83(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rdi,%rsi
call   1030 <strncpy@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <get_ctl_fd@plt>
test   %eax,%eax
js     11a2 <do_changename+0x52>
mov    %eax,%ebp
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
jne    11b0 <do_changename+0x60>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11a7 <do_changename+0x57>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe49(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1199 <do_changename+0x49>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCSIFNAME = global i32 0
@.str = [12 x i8] c"SIOCSIFNAME\00"
define i32 @do_changename(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%10 = load i32, i32* %9
%11 = load i8*, i8** %4
%12 = load i32, i32* @IFNAMSIZ
%13 = call i32 @strncpy(i32 %10, i8* %11, i32 %12)
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %5
%17 = load i32, i32* @IFNAMSIZ
%18 = call i32 @strncpy(i32 %15, i8* %16, i32 %17)
%19 = call i32 (...) @get_ctl_fd()
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %2
store i32 -1, i32* %3
br label %37
23: ; preds = %2
%24 = load i32, i32* %7
%25 = load i32, i32* @SIOCSIFNAME
%26 = call i32 @ioctl(i32 %24, i32 %25, %struct.ifreq* %6)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %37
33: ; preds = %23
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %8
store i32 %36, i32* %3
br label %37
37: ; preds = %33, %29, %22
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCSIFNAME = global i32 0
@.str = [12 x i8] c"SIOCSIFNAME\00"
define i32 @do_changename(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @IFNAMSIZ
%6 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %5)
%7 = load i32, i32* @IFNAMSIZ
%8 = tail call i32 @strncpy(i32 undef, i8* %1, i32 %7)
%9 = tail call i32 (...) @get_ctl_fd()
%10 = icmp slt i32 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %2
%12 = load i32, i32* @SIOCSIFNAME
%13 = call i32 @ioctl(i32 %9, i32 %12, %struct.ifreq* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %11, %15
%18 = phi i32 [ -1, %15 ], [ 0, %11 ]
%19 = call i32 @close(i32 %9)
br label %20
20: ; preds = %17, %2
%21 = phi i32 [ -1, %2 ], [ %18, %17 ]
ret i32 %21
}