#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 ethhdr {
  int /*<<< orphan*/ h_proto;
  int /*<<< orphan*/ h_dest;
  int /*<<< orphan*/ h_source;
};

/* Variables and functions */
int /*<<< orphan*/ ETH_ALEN;
scalar_t__ ether_aton(char *);
int /*<<< orphan*/ htons(int);
struct ethhdr *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, void *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
struct ethhdr *eth_header_init(char *src_mac, char *dst_mac, int proto) {
  struct ethhdr *header = malloc(sizeof(struct ethhdr));
  if (header == NULL) {
    perror("malloc() failure: ");
    return NULL;
  }
  memcpy(header->h_source, (void *)ether_aton(src_mac), ETH_ALEN);
  memcpy(header->h_dest, (void *)ether_aton(dst_mac), ETH_ALEN);
  header->h_proto = htons(proto);
  return header;
}


x86 assembly (clang, -O0):
<eth_header_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1195 <eth_header_init+0x45>
lea    0xe7d(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fb <eth_header_init+0xab>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <ether_aton@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rsi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <ether_aton@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rsi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eth_header_init>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0xc,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c2 <eth_header_init+0x72>
mov    0x8(%rbx),%ebp
mov    %r12,%rdi
call   1050 <ether_aton@plt>
mov    0x2e63(%rip),%r12
mov    (%r12),%edx
mov    %ebp,%edi
mov    %rax,%rsi
call   1040 <memcpy@plt>
mov    0x4(%rbx),%ebp
mov    %r15,%rdi
call   1050 <ether_aton@plt>
mov    (%r12),%edx
mov    %ebp,%edi
mov    %rax,%rsi
call   1040 <memcpy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
jmp    11b6 <eth_header_init+0x66>

LLVM IR (-O0):
%struct.ethhdr = type { i32, i32, i32 }
@.str = [19 x i8] c"malloc() failure: \00"
@ETH_ALEN = global i32 0
define %struct.ethhdr* @eth_header_init(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.ethhdr*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.ethhdr*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = call %struct.ethhdr* @malloc(i32 12)
store %struct.ethhdr* %9, %struct.ethhdr** %8
%10 = load %struct.ethhdr*, %struct.ethhdr** %8
%11 = icmp eq %struct.ethhdr* %10, null
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store %struct.ethhdr* null, %struct.ethhdr** %4
br label %36
14: ; preds = %3
%15 = load %struct.ethhdr*, %struct.ethhdr** %8
%16 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %15, i32 0, i32 2
%17 = load i32, i32* %16
%18 = load i8*, i8** %5
%19 = call i64 @ether_aton(i8* %18)
%20 = inttoptr i64 %19 to i8*
%21 = load i32, i32* @ETH_ALEN
%22 = call i32 @memcpy(i32 %17, i8* %20, i32 %21)
%23 = load %struct.ethhdr*, %struct.ethhdr** %8
%24 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load i8*, i8** %6
%27 = call i64 @ether_aton(i8* %26)
%28 = inttoptr i64 %27 to i8*
%29 = load i32, i32* @ETH_ALEN
%30 = call i32 @memcpy(i32 %25, i8* %28, i32 %29)
%31 = load i32, i32* %7
%32 = call i32 @htons(i32 %31)
%33 = load %struct.ethhdr*, %struct.ethhdr** %8
%34 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load %struct.ethhdr*, %struct.ethhdr** %8
store %struct.ethhdr* %35, %struct.ethhdr** %4
br label %36
36: ; preds = %14, %12
%37 = load %struct.ethhdr*, %struct.ethhdr** %4
ret %struct.ethhdr* %37
}

LLVM IR (-O2):
%struct.ethhdr = type { i32, i32, i32 }
@.str = [19 x i8] c"malloc() failure: \00"
@ETH_ALEN = global i32 0
define noalias %struct.ethhdr* @eth_header_init(i8* %0, i8* %1, i32 %2) {
%4 = tail call %struct.ethhdr* @malloc(i32 12)
%5 = icmp eq %struct.ethhdr* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %23
8: ; preds = %3
%9 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %4, i64 0, i32 2
%10 = load i32, i32* %9
%11 = tail call i64 @ether_aton(i8* %0)
%12 = inttoptr i64 %11 to i8*
%13 = load i32, i32* @ETH_ALEN
%14 = tail call i32 @memcpy(i32 %10, i8* %12, i32 %13)
%15 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %4, i64 0, i32 1
%16 = load i32, i32* %15
%17 = tail call i64 @ether_aton(i8* %1)
%18 = inttoptr i64 %17 to i8*
%19 = load i32, i32* @ETH_ALEN
%20 = tail call i32 @memcpy(i32 %16, i8* %18, i32 %19)
%21 = tail call i32 @htons(i32 %2)
%22 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %4, i64 0, i32 0
store i32 %21, i32* %22
br label %23
23: ; preds = %8, %6
ret %struct.ethhdr* %4
}