#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__ {
  unsigned char *sa_data;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int, char *);
scalar_t__ sethostid(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int main() {
  struct ifreq s;
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  union {
    unsigned char chwaddr[4];
    unsigned int lhwaddr;
  } conv;
  strncpy(s.ifr_name, "eth0", sizeof("eth0"));
  if (ioctl(fd, SIOCGIFHWADDR, &s) == 0) {
    conv.lhwaddr = 0;
    conv.chwaddr[3] = s.ifr_addr.sa_data[1];
    conv.chwaddr[2] = s.ifr_addr.sa_data[2];
    conv.chwaddr[1] = s.ifr_addr.sa_data[3];
    conv.chwaddr[0] = s.ifr_addr.sa_data[4];
    printf("Setting host Id to 0x%08x (part of %s's hwaddr)\n", conv.lhwaddr,
           "eth0");
    if (sethostid((int)conv.lhwaddr) == 0) {
      return 0;
    } else {
      perror("sethostid");
      return 1;
    }
  }
  perror("ioctl");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe64(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jne    1240 <main+0xe0>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x1d(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2(%rax),%al
mov    %al,-0x1e(%rbp)
mov    -0x18(%rbp),%rax
mov    0x3(%rax),%al
mov    %al,-0x1f(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%al
mov    %al,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe07(%rip),%rdi
lea    0xdfb(%rip),%rdx
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <sethostid@plt>
cmp    $0x0,%rax
jne    1228 <main+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    1253 <main+0xf3>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1253 <main+0xf3>
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebx
lea    0xe72(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
jne    11e5 <main+0x85>
mov    (%rsp),%rax
mov    0x1(%rax),%ebx
bswap  %ebx
lea    0xe45(%rip),%rdi
lea    0xe39(%rip),%rdx
mov    %ebx,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <sethostid@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11ee <main+0x8e>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe54(%rip),%rdi
jmp    11f5 <main+0x95>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11df <main+0x7f>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%union.anon = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [5 x i8] c"eth0\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [49 x i8] c"Setting host Id to 0x%08x (part of %s's hwaddr)\0A\00"
@.str.2 = [10 x i8] c"sethostid\00"
@.str.3 = [6 x i8] c"ioctl\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.ifreq
%3 = alloca i32
%4 = alloca %union.anon
store i32 0, i32* %1
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @IPPROTO_IP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %3
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 1
%10 = load i32, i32* %9
%11 = call i32 @strncpy(i32 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 5)
%12 = load i32, i32* %3
%13 = load i32, i32* @SIOCGIFHWADDR
%14 = call i64 @ioctl(i32 %12, i32 %13, %struct.ifreq* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %56
16: ; preds = %0
%17 = bitcast %union.anon* %4 to i32*
store i32 0, i32* %17
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 0
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = load i8, i8* %21
%23 = bitcast %union.anon* %4 to [4 x i8]*
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %23, i64 0, i64 3
store i8 %22, i8* %24
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 2
%29 = load i8, i8* %28
%30 = bitcast %union.anon* %4 to [4 x i8]*
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %30, i64 0, i64 2
store i8 %29, i8* %31
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = getelementptr inbounds i8, i8* %34, i64 3
%36 = load i8, i8* %35
%37 = bitcast %union.anon* %4 to [4 x i8]*
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %37, i64 0, i64 1
store i8 %36, i8* %38
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = getelementptr inbounds i8, i8* %41, i64 4
%43 = load i8, i8* %42
%44 = bitcast %union.anon* %4 to [4 x i8]*
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %44, i64 0, i64 0
store i8 %43, i8* %45
%46 = bitcast %union.anon* %4 to i32*
%47 = load i32, i32* %46
%48 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %47, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%49 = bitcast %union.anon* %4 to i32*
%50 = load i32, i32* %49
%51 = call i64 @sethostid(i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %16
store i32 0, i32* %1
br label %58
54: ; preds = %16
%55 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %58
56: ; preds = %0
%57 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %58
58: ; preds = %56, %54, %53
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [5 x i8] c"eth0\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [49 x i8] c"Setting host Id to 0x%08x (part of %s's hwaddr)\0A\00"
@.str.2 = [10 x i8] c"sethostid\00"
@.str.3 = [6 x i8] c"ioctl\00"
define i32 @main() {
%1 = alloca %struct.ifreq
%2 = bitcast %struct.ifreq* %1 to i8*
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = load i32, i32* @IPPROTO_IP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 5)
%8 = load i32, i32* @SIOCGIFHWADDR
%9 = call i64 @ioctl(i32 %6, i32 %8, %struct.ifreq* %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %35
11: ; preds = %0
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0, i32 0
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = getelementptr inbounds i8, i8* %13, i64 2
%17 = load i8, i8* %16
%18 = getelementptr inbounds i8, i8* %13, i64 3
%19 = load i8, i8* %18
%20 = getelementptr inbounds i8, i8* %13, i64 4
%21 = load i8, i8* %20
%22 = zext i8 %15 to i32
%23 = shl nuw i32 %22, 24
%24 = zext i8 %17 to i32
%25 = shl nuw nsw i32 %24, 16
%26 = or i32 %25, %23
%27 = zext i8 %19 to i32
%28 = shl nuw nsw i32 %27, 8
%29 = or i32 %26, %28
%30 = zext i8 %21 to i32
%31 = or i32 %29, %30
%32 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%33 = call i64 @sethostid(i32 %31)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %0, %11
%36 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %0 ]
%37 = call i32 @perror(i8* %36)
br label %38
38: ; preds = %35, %11
%39 = phi i32 [ 0, %11 ], [ 1, %35 ]
ret i32 %39
}