#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  scalar_t__ *sa_data;
};
struct ifreq {
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, ...);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int get_all_macs(char result[5][20]) {
  struct ifreq ifreq;
  int sock;
  int i;
  for (i = 0; i < 10; i++) {
    char eth[5];
    sock = socket(AF_INET, SOCK_STREAM, 0);
    sprintf(eth, "eth%d", i);
    strcpy(ifreq.ifr_name, eth);
    if (ioctl(sock, SIOCGIFHWADDR, &ifreq) < 0) {
      continue;
    }
    sprintf(result[i], "%02x:%02x:%02x:%02x:%02x:%02x",
            (unsigned char)ifreq.ifr_hwaddr.sa_data[0],
            (unsigned char)ifreq.ifr_hwaddr.sa_data[1],
            (unsigned char)ifreq.ifr_hwaddr.sa_data[2],
            (unsigned char)ifreq.ifr_hwaddr.sa_data[3],
            (unsigned char)ifreq.ifr_hwaddr.sa_data[4],
            (unsigned char)ifreq.ifr_hwaddr.sa_data[5]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_all_macs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0xa,-0x20(%rbp)
jge    123a <get_all_macs+0xfa>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x25(%rbp),%rdi
mov    -0x20(%rbp),%eax
lea    0xe79(%rip),%rsi
movzbl %al,%edx
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%edi
lea    -0x25(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11c1 <get_all_macs+0x81>
jmp    122c <get_all_macs+0xec>
mov    -0x8(%rbp),%rdi
movslq -0x20(%rbp),%rax
imul   $0x14,%rax,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
movzbl %al,%ecx
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
movzbl %al,%r8d
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rax
movzbl %al,%r9d
mov    -0x18(%rbp),%rax
mov    0x20(%rax),%rax
movzbl %al,%r10d
mov    -0x18(%rbp),%rax
mov    0x28(%rax),%rax
movzbl %al,%eax
lea    0xdec(%rip),%rsi
movzbl %dl,%edx
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1153 <get_all_macs+0x13>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_all_macs>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0x3(%rsp),%r15
mov    0x2e7b(%rip),%r13
lea    0x8(%rsp),%r12
xor    %r14d,%r14d
jmp    1182 <get_all_macs+0x42>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14
add    $0x14,%rbx
cmp    $0xa,%r14
je     120d <get_all_macs+0xcd>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebp
movzbl %r14b,%edx
mov    %r15,%rdi
lea    0xe55(%rip),%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    0x10(%rsp),%edi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     1170 <get_all_macs+0x30>
mov    0x8(%rsp),%rax
movzbl (%rax),%edx
movzbl 0x8(%rax),%ecx
movzbl 0x10(%rax),%r8d
movzbl 0x18(%rax),%r9d
movzbl 0x20(%rax),%r10d
movzbl 0x28(%rax),%ebp
mov    %rbx,%rdi
lea    0xe0c(%rip),%rsi
xor    %eax,%eax
push   %rbp
push   %r10
call   1050 <sprintf@plt>
add    $0x10,%rsp
jmp    1170 <get_all_macs+0x30>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [6 x i8] c"eth%d\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [30 x i8] c"%02x:%02x:%02x:%02x:%02x:%02x\00"
define i32 @get_all_macs([20 x i8]* %0) {
%2 = alloca [20 x i8]*
%3 = alloca %struct.ifreq
%4 = alloca i32
%5 = alloca i32
%6 = alloca [5 x i8]
store [20 x i8]* %0, [20 x i8]** %2
store i32 0, i32* %5
br label %7
7: ; preds = %75, %1
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 10
br i1 %9, label %10, label %78
10: ; preds = %7
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %4
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%15 = load i32, i32* %5
%16 = trunc i32 %15 to i8
%17 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %16)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%21 = call i32 @strcpy(i32 %19, i8* %20)
%22 = load i32, i32* %4
%23 = load i32, i32* @SIOCGIFHWADDR
%24 = call i64 @ioctl(i32 %22, i32 %23, %struct.ifreq* %3)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %10
br label %75
27: ; preds = %10
%28 = load [20 x i8]*, [20 x i8]** %2
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %28, i64 %30
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %31, i64 0, i64 0
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load i64*, i64** %34
%36 = getelementptr inbounds i64, i64* %35, i64 0
%37 = load i64, i64* %36
%38 = trunc i64 %37 to i8
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
%41 = load i64*, i64** %40
%42 = getelementptr inbounds i64, i64* %41, i64 1
%43 = load i64, i64* %42
%44 = trunc i64 %43 to i8
%45 = zext i8 %44 to i32
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
%48 = load i64*, i64** %47
%49 = getelementptr inbounds i64, i64* %48, i64 2
%50 = load i64, i64* %49
%51 = trunc i64 %50 to i8
%52 = zext i8 %51 to i32
%53 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
%55 = load i64*, i64** %54
%56 = getelementptr inbounds i64, i64* %55, i64 3
%57 = load i64, i64* %56
%58 = trunc i64 %57 to i8
%59 = zext i8 %58 to i32
%60 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%61 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %60, i32 0, i32 0
%62 = load i64*, i64** %61
%63 = getelementptr inbounds i64, i64* %62, i64 4
%64 = load i64, i64* %63
%65 = trunc i64 %64 to i8
%66 = zext i8 %65 to i32
%67 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%68 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %67, i32 0, i32 0
%69 = load i64*, i64** %68
%70 = getelementptr inbounds i64, i64* %69, i64 5
%71 = load i64, i64* %70
%72 = trunc i64 %71 to i8
%73 = zext i8 %72 to i32
%74 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* %32, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %38, i32 %45, i32 %52, i32 %59, i32 %66, i32 %73)
br label %75
75: ; preds = %27, %26
%76 = load i32, i32* %5
%77 = add nsw i32 %76, 1
store i32 %77, i32* %5
br label %7
78: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [6 x i8] c"eth%d\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [30 x i8] c"%02x:%02x:%02x:%02x:%02x:%02x\00"
define i32 @get_all_macs([20 x i8]* nocapture writeonly %0) {
%2 = alloca %struct.ifreq
%3 = alloca [5 x i8]
%4 = bitcast %struct.ifreq* %2 to i8*
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0, i32 0
br label %8
8: ; preds = %1, %46
%9 = phi i64 [ 0, %1 ], [ %47, %46 ]
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = trunc i64 %9 to i8
%14 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %13)
%15 = load i32, i32* %6
%16 = call i32 @strcpy(i32 %15, i8* %5)
%17 = load i32, i32* @SIOCGIFHWADDR
%18 = call i64 @ioctl(i32 %12, i32 %17, %struct.ifreq* %2)
%19 = icmp slt i64 %18, 0
br i1 %19, label %46, label %20
20: ; preds = %8
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %0, i64 %9, i64 0
%22 = load i64*, i64** %7
%23 = load i64, i64* %22
%24 = trunc i64 %23 to i8
%25 = getelementptr inbounds i64, i64* %22, i64 1
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i32
%28 = and i32 %27, 255
%29 = getelementptr inbounds i64, i64* %22, i64 2
%30 = load i64, i64* %29
%31 = trunc i64 %30 to i32
%32 = and i32 %31, 255
%33 = getelementptr inbounds i64, i64* %22, i64 3
%34 = load i64, i64* %33
%35 = trunc i64 %34 to i32
%36 = and i32 %35, 255
%37 = getelementptr inbounds i64, i64* %22, i64 4
%38 = load i64, i64* %37
%39 = trunc i64 %38 to i32
%40 = and i32 %39, 255
%41 = getelementptr inbounds i64, i64* %22, i64 5
%42 = load i64, i64* %41
%43 = trunc i64 %42 to i32
%44 = and i32 %43, 255
%45 = call i32 (i8*, i8*, i8, ...) @sprintf(i8* dereferenceable(1) %21, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %24, i32 %28, i32 %32, i32 %36, i32 %40, i32 %44)
br label %46
46: ; preds = %8, %20
%47 = add nuw nsw i64 %9, 1
%48 = icmp eq i64 %47, 10
br i1 %48, label %49, label %8
49: ; preds = %46
ret i32 0
}