#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*/ SIOCSIFNAME;
int /*<<< orphan*/ close(int);
int get_ctl_fd();
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int do_changename(char *dev, char *newdev) {
  struct ifreq ifr;
  int fd;
  int err;
  strcpy(ifr.ifr_name, dev);
  strcpy(ifr.ifr_newname, newdev);
  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
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    $0x0,%al
call   1070 <get_ctl_fd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1198 <do_changename+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <do_changename+0x98>
mov    -0x24(%rbp),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11da <do_changename+0x8a>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <do_changename+0x98>
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    %rdi,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rsi
call   1030 <strcpy@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <get_ctl_fd@plt>
test   %eax,%eax
js     1195 <do_changename+0x45>
mov    %eax,%ebp
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
jne    11a3 <do_changename+0x53>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    119a <do_changename+0x4a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    118c <do_changename+0x3c>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@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 = call i32 @strcpy(i32 %10, i8* %11)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%14 = load i32, i32* %13
%15 = load i8*, i8** %5
%16 = call i32 @strcpy(i32 %14, i8* %15)
%17 = call i32 (...) @get_ctl_fd()
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 -1, i32* %3
br label %35
21: ; preds = %2
%22 = load i32, i32* %7
%23 = load i32, i32* @SIOCSIFNAME
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %6)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %3
br label %35
31: ; preds = %21
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %8
store i32 %34, i32* %3
br label %35
35: ; preds = %31, %27, %20
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@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 = tail call i32 @strcpy(i32 undef, i8* %0)
%6 = tail call i32 @strcpy(i32 undef, i8* %1)
%7 = tail call i32 (...) @get_ctl_fd()
%8 = icmp slt i32 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %2
%10 = load i32, i32* @SIOCSIFNAME
%11 = call i32 @ioctl(i32 %7, i32 %10, %struct.ifreq* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %9, %13
%16 = phi i32 [ -1, %13 ], [ 0, %9 ]
%17 = call i32 @close(i32 %7)
br label %18
18: ; preds = %15, %2
%19 = phi i32 [ -1, %2 ], [ %16, %15 ]
ret i32 %19
}