#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*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCSIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("Please specify original and new interface names\n");
    return 1;
  }
  int fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    perror("Failed to open network socket");
    return 1;
  }
  char const *original_name = argv[1];
  char const *new_name = argv[2];
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, original_name, IFNAMSIZ);
  strncpy(ifr.ifr_newname, new_name, IFNAMSIZ);
  int ret = ioctl(fd, SIOCSIFNAME, &ifr);
  if (ret == 0) {
    printf("Network interface %s renamed to %s\n", original_name, new_name);
    return 0;
  } else {
    perror("Failed to rename network interface");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1281 <main+0x121>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d8 <main+0x78>
lea    0xe6a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1281 <main+0x121>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    126e <main+0x10e>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1281 <main+0x121>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    121a <main+0xaa>
mov    %rsi,%rbx
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     123c <main+0xcc>
mov    %eax,%ebp
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%r15
mov    %rsp,%r12
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2dfe(%rip),%r13
mov    0x0(%r13),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    (%rsp),%edi
mov    0x0(%r13),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1070 <ioctl@plt>
test   %eax,%eax
jne    1245 <main+0xd5>
lea    0xe13(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    122b <main+0xbb>
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbd(%rip),%rdi
jmp    124c <main+0xdc>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
jmp    1226 <main+0xb6>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [49 x i8] c"Please specify original and new interface names\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [30 x i8] c"Failed to open network socket\00"
@IFNAMSIZ = global i32 0
@SIOCSIFNAME = global i32 0
@.str.2 = [36 x i8] c"Network interface %s renamed to %s\0A\00"
@.str.3 = [35 x i8] c"Failed to rename network interface\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.ifreq
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %52
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %52
23: ; preds = %15
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
store i8* %26, i8** %7
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
store i8* %29, i8** %8
%30 = call i32 @memset(%struct.ifreq* %9, i32 0, i32 8)
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load i8*, i8** %7
%34 = load i32, i32* @IFNAMSIZ
%35 = call i32 @strncpy(i32 %32, i8* %33, i32 %34)
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i8*, i8** %8
%39 = load i32, i32* @IFNAMSIZ
%40 = call i32 @strncpy(i32 %37, i8* %38, i32 %39)
%41 = load i32, i32* %6
%42 = load i32, i32* @SIOCSIFNAME
%43 = call i32 @ioctl(i32 %41, i32 %42, %struct.ifreq* %9)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %23
%47 = load i8*, i8** %7
%48 = load i8*, i8** %8
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %47, i8* %48)
store i32 0, i32* %3
br label %52
50: ; preds = %23
%51 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %52
52: ; preds = %50, %46, %21, %13
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [30 x i8] c"Failed to open network socket\00"
@IFNAMSIZ = global i32 0
@SIOCSIFNAME = global i32 0
@.str.2 = [36 x i8] c"Network interface %s renamed to %s\0A\00"
@.str.3 = [35 x i8] c"Failed to rename network interface\00"
@str = [48 x i8] c"Please specify original and new interface names\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.ifreq
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %38
7: ; preds = %2
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %38
14: ; preds = %7
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = bitcast %struct.ifreq* %3 to i8*
%20 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @IFNAMSIZ
%24 = call i32 @strncpy(i32 %22, i8* %16, i32 %23)
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* @IFNAMSIZ
%28 = call i32 @strncpy(i32 %26, i8* %18, i32 %27)
%29 = load i32, i32* @SIOCSIFNAME
%30 = call i32 @ioctl(i32 %10, i32 %29, %struct.ifreq* %3)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %14
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %16, i8* %18)
br label %36
34: ; preds = %14
%35 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %32
%37 = phi i32 [ 0, %32 ], [ 1, %34 ]
br label %38
38: ; preds = %12, %36, %5
%39 = phi i32 [ 1, %5 ], [ 1, %12 ], [ %37, %36 ]
ret i32 %39
}