#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 sockaddr_ll {
  unsigned char *sll_addr;
  unsigned char sll_halen;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int snprintf(char *, size_t, char *, unsigned char, char *);
__attribute__((unused)) char *get_packet_address(struct sockaddr *sockaddr_ptr,
                                                 char *buf, size_t buflen) {
  char *slider = buf;
  unsigned char *m = ((struct sockaddr_ll *)sockaddr_ptr)->sll_addr;
  unsigned char n = ((struct sockaddr_ll *)sockaddr_ptr)->sll_halen;
  for (unsigned char i = 0; i < n; i++) {
    int ret;
    ret = snprintf(slider, buflen, "%02x%s", m[i], (i + 1) < n ? ":" : "");
    if (ret < 0 || (size_t)ret >= buflen)
      return NULL;
    buflen -= ret;
    slider = (slider + ret);
  }
  return buf;
}


x86 assembly (clang, -O0):
<get_packet_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%al
mov    %al,-0x31(%rbp)
movb   $0x0,-0x32(%rbp)
movzbl -0x32(%rbp),%eax
movzbl -0x31(%rbp),%ecx
cmp    %ecx,%eax
jge    11eb <get_packet_address+0xdb>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rax
movzbl -0x32(%rbp),%ecx
mov    (%rax,%rcx,1),%al
movzbl -0x32(%rbp),%edx
add    $0x1,%edx
movzbl -0x31(%rbp),%r9d
lea    0xe8d(%rip),%r8
lea    0xe85(%rip),%rcx
cmp    %r9d,%edx
cmovl  %rcx,%r8
lea    0xe70(%rip),%rdx
movzbl %al,%ecx
call   1030 <snprintf@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jl     11b3 <get_packet_address+0xa3>
movslq -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11c0 <get_packet_address+0xb0>
movq   $0x0,-0x8(%rbp)
jmp    11f3 <get_packet_address+0xe3>
movslq -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x32(%rbp),%al
add    $0x1,%al
mov    %al,-0x32(%rbp)
jmp    1145 <get_packet_address+0x35>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_packet_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rax
movzbl 0x8(%rdi),%ebp
test   %ebp,%ebp
je     118f <get_packet_address+0x7f>
mov    %rdx,%r12
mov    (%rdi),%r13
mov    %ebp,%r14d
xor    %r15d,%r15d
mov    %rax,(%rsp)
mov    %rax,%rbx
nopl   0x0(%rax)
movzbl 0x0(%r13,%r15,1),%ecx
lea    0x1(%r15),%eax
cmp    %ebp,%eax
lea    0xeb5(%rip),%r8
lea    0xead(%rip),%rax
cmovb  %rax,%r8
mov    %rbx,%rdi
mov    %r12,%rsi
lea    0xe95(%rip),%rdx
call   1030 <snprintf@plt>
test   %eax,%eax
js     118d <get_packet_address+0x7d>
mov    %eax,%eax
sub    %rax,%r12
jbe    118d <get_packet_address+0x7d>
add    %rax,%rbx
add    $0x1,%r15
cmp    %r15,%r14
jne    1140 <get_packet_address+0x30>
mov    (%rsp),%rax
jmp    118f <get_packet_address+0x7f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_ll = type { i8*, i8 }
@.str = [7 x i8] c"%02x%s\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @get_packet_address(%struct.sockaddr* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.sockaddr*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i8
%12 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%13 = load i8*, i8** %6
store i8* %13, i8** %8
%14 = load %struct.sockaddr*, %struct.sockaddr** %5
%15 = bitcast %struct.sockaddr* %14 to %struct.sockaddr_ll*
%16 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %15, i32 0, i32 0
%17 = load i8*, i8** %16
store i8* %17, i8** %9
%18 = load %struct.sockaddr*, %struct.sockaddr** %5
%19 = bitcast %struct.sockaddr* %18 to %struct.sockaddr_ll*
%20 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %19, i32 0, i32 1
%21 = load i8, i8* %20
store i8 %21, i8* %10
store i8 0, i8* %11
br label %22
22: ; preds = %62, %3
%23 = load i8, i8* %11
%24 = zext i8 %23 to i32
%25 = load i8, i8* %10
%26 = zext i8 %25 to i32
%27 = icmp slt i32 %24, %26
br i1 %27, label %28, label %65
28: ; preds = %22
%29 = load i8*, i8** %8
%30 = load i64, i64* %7
%31 = load i8*, i8** %9
%32 = load i8, i8* %11
%33 = zext i8 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = load i8, i8* %11
%37 = zext i8 %36 to i32
%38 = add nsw i32 %37, 1
%39 = load i8, i8* %10
%40 = zext i8 %39 to i32
%41 = icmp slt i32 %38, %40
%42 = zext i1 %41 to i64
%43 = select i1 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%44 = call i32 @snprintf(i8* %29, i64 %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %35, i8* %43)
store i32 %44, i32* %12
%45 = load i32, i32* %12
%46 = icmp slt i32 %45, 0
br i1 %46, label %52, label %47
47: ; preds = %28
%48 = load i32, i32* %12
%49 = sext i32 %48 to i64
%50 = load i64, i64* %7
%51 = icmp uge i64 %49, %50
br i1 %51, label %52, label %53
52: ; preds = %47, %28
store i8* null, i8** %4
br label %67
53: ; preds = %47
%54 = load i32, i32* %12
%55 = sext i32 %54 to i64
%56 = load i64, i64* %7
%57 = sub i64 %56, %55
store i64 %57, i64* %7
%58 = load i8*, i8** %8
%59 = load i32, i32* %12
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
store i8* %61, i8** %8
br label %62
62: ; preds = %53
%63 = load i8, i8* %11
%64 = add i8 %63, 1
store i8 %64, i8* %11
br label %22
65: ; preds = %22
%66 = load i8*, i8** %6
store i8* %66, i8** %4
br label %67
67: ; preds = %65, %52
%68 = load i8*, i8** %4
ret i8* %68
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"%02x%s\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @get_packet_address(%struct.sockaddr* nocapture %0, i8* %1, i64 %2) {
%4 = bitcast %struct.sockaddr* %0 to i8**
%5 = load i8*, i8** %4
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 2
%7 = bitcast %struct.sockaddr* %6 to i8*
%8 = load i8, i8* %7
%9 = zext i8 %8 to i32
%10 = icmp eq i8 %8, 0
br i1 %10, label %33, label %11
11: ; preds = %3
%12 = zext i8 %8 to i64
br label %13
13: ; preds = %11, %28
%14 = phi i64 [ 0, %11 ], [ %31, %28 ]
%15 = phi i64 [ %2, %11 ], [ %29, %28 ]
%16 = phi i8* [ %1, %11 ], [ %30, %28 ]
%17 = trunc i64 %14 to i32
%18 = getelementptr inbounds i8, i8* %5, i64 %14
%19 = load i8, i8* %18
%20 = add nuw nsw i32 %17, 1
%21 = icmp ult i32 %20, %9
%22 = select i1 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%23 = tail call i32 @snprintf(i8* %16, i64 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %19, i8* %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %33, label %25
25: ; preds = %13
%26 = zext i32 %23 to i64
%27 = icmp ugt i64 %15, %26
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = sub i64 %15, %26
%30 = getelementptr inbounds i8, i8* %16, i64 %26
%31 = add nuw nsw i64 %14, 1
%32 = icmp eq i64 %31, %12
br i1 %32, label %33, label %13
33: ; preds = %28, %25, %13, %3
%34 = phi i8* [ %1, %3 ], [ null, %13 ], [ null, %25 ], [ %1, %28 ]
ret i8* %34
}