#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_in {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SIOCGIFBRDADDR;
int /*<<< orphan*/ SIOCGIFNETMASK;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int get_kernel_stack_if_params(const char *ifname, struct sockaddr_in *addr,
                               struct sockaddr_in *baddr,
                               struct sockaddr_in *netmask) {
  int s;
  int error;
  struct ifreq ifr;
  s = socket(PF_INET, SOCK_DGRAM, 0);
  if (-1 == s) {
    return (1);
  }
  strcpy(ifr.ifr_name, ifname);
  error = ioctl(s, SIOCGIFADDR, &ifr);
  if (0 != error) {
    printf("SIOCGIFADDR failed %d\n", error);
    return (1);
  }
  memcpy(addr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
  error = ioctl(s, SIOCGIFBRDADDR, &ifr);
  if (0 != error) {
    printf("SIOCGIFBRDADDR failed %d\n", error);
    return (1);
  }
  memcpy(baddr, &ifr.ifr_addr, sizeof(struct sockaddr_in));
  error = ioctl(s, SIOCGIFNETMASK, &ifr);
  if (0 != error) {
    printf("SIOCGIFNETMASK failed %d\n", error);
    return (1);
  }
  memcpy(netmask, &ifr.ifr_addr, sizeof(struct sockaddr_in));
  close(s);
  return (0);
}


x86 assembly (clang, -O0):
<get_kernel_stack_if_params>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    11ae <get_kernel_stack_if_params+0x4e>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <get_kernel_stack_if_params+0x159>
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%eax
je     11f8 <get_kernel_stack_if_params+0x98>
mov    -0x30(%rbp),%esi
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <get_kernel_stack_if_params+0x159>
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%eax
je     1248 <get_kernel_stack_if_params+0xe8>
mov    -0x30(%rbp),%esi
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <get_kernel_stack_if_params+0x159>
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%eax
je     1298 <get_kernel_stack_if_params+0x138>
mov    -0x30(%rbp),%esi
lea    0xdaa(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <get_kernel_stack_if_params+0x159>
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_kernel_stack_if_params>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbx
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0x1,%r12d
cmp    $0xffffffff,%eax
je     1234 <get_kernel_stack_if_params+0xd4>
mov    %eax,%ebp
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
je     11c9 <get_kernel_stack_if_params+0x69>
lea    0xe39(%rip),%rdi
jmp    122d <get_kernel_stack_if_params+0xcd>
mov    %rsp,%rbx
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
je     11fc <get_kernel_stack_if_params+0x9c>
lea    0xe1d(%rip),%rdi
jmp    122d <get_kernel_stack_if_params+0xcd>
mov    %rsp,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
je     1246 <get_kernel_stack_if_params+0xe6>
lea    0xe04(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %r12d,%r12d
jmp    1234 <get_kernel_stack_if_params+0xd4>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
@.str = [23 x i8] c"SIOCGIFADDR failed %d\0A\00"
@SIOCGIFBRDADDR = global i32 0
@.str.1 = [26 x i8] c"SIOCGIFBRDADDR failed %d\0A\00"
@SIOCGIFNETMASK = global i32 0
@.str.2 = [26 x i8] c"SIOCGIFNETMASK failed %d\0A\00"
define i32 @get_kernel_stack_if_params(i8* %0, %struct.sockaddr_in* %1, %struct.sockaddr_in* %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.sockaddr_in*
%9 = alloca %struct.sockaddr_in*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.ifreq
store i8* %0, i8** %6
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %7
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %8
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %9
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp eq i32 -1, %16
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 1, i32* %5
br label %62
19: ; preds = %4
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load i8*, i8** %6
%23 = call i32 @strcpy(i32 %21, i8* %22)
%24 = load i32, i32* %10
%25 = load i32, i32* @SIOCGIFADDR
%26 = call i32 @ioctl(i32 %24, i32 %25, %struct.ifreq* %12)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp ne i32 0, %27
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i32, i32* %11
%31 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %30)
store i32 1, i32* %5
br label %62
32: ; preds = %19
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i32 0, i32 0
%35 = call i32 @memcpy(%struct.sockaddr_in* %33, i32* %34, i32 4)
%36 = load i32, i32* %10
%37 = load i32, i32* @SIOCGIFBRDADDR
%38 = call i32 @ioctl(i32 %36, i32 %37, %struct.ifreq* %12)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp ne i32 0, %39
br i1 %40, label %41, label %44
41: ; preds = %32
%42 = load i32, i32* %11
%43 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %42)
store i32 1, i32* %5
br label %62
44: ; preds = %32
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i32 0, i32 0
%47 = call i32 @memcpy(%struct.sockaddr_in* %45, i32* %46, i32 4)
%48 = load i32, i32* %10
%49 = load i32, i32* @SIOCGIFNETMASK
%50 = call i32 @ioctl(i32 %48, i32 %49, %struct.ifreq* %12)
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = icmp ne i32 0, %51
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = load i32, i32* %11
%55 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %54)
store i32 1, i32* %5
br label %62
56: ; preds = %44
%57 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%58 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i32 0, i32 0
%59 = call i32 @memcpy(%struct.sockaddr_in* %57, i32* %58, i32 4)
%60 = load i32, i32* %10
%61 = call i32 @close(i32 %60)
store i32 0, i32* %5
br label %62
62: ; preds = %56, %53, %41, %29, %18
%63 = load i32, i32* %5
ret i32 %63
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.ifreq = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
@.str = [23 x i8] c"SIOCGIFADDR failed %d\0A\00"
@SIOCGIFBRDADDR = global i32 0
@.str.1 = [26 x i8] c"SIOCGIFBRDADDR failed %d\0A\00"
@SIOCGIFNETMASK = global i32 0
@.str.2 = [26 x i8] c"SIOCGIFNETMASK failed %d\0A\00"
define i32 @get_kernel_stack_if_params(i8* %0, %struct.sockaddr_in* %1, %struct.sockaddr_in* %2, %struct.sockaddr_in* %3) {
%5 = alloca %struct.ifreq
%6 = bitcast %struct.ifreq* %5 to i8*
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %36, label %11
11: ; preds = %4
%12 = tail call i32 @strcpy(i32 undef, i8* %0)
%13 = load i32, i32* @SIOCGIFADDR
%14 = call i32 @ioctl(i32 %9, i32 %13, %struct.ifreq* %5)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %36
18: ; preds = %11
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%20 = call i32 @memcpy(%struct.sockaddr_in* %1, i32* %19, i32 4)
%21 = load i32, i32* @SIOCGIFBRDADDR
%22 = call i32 @ioctl(i32 %9, i32 %21, %struct.ifreq* %5)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %36
26: ; preds = %18
%27 = call i32 @memcpy(%struct.sockaddr_in* %2, i32* %19, i32 4)
%28 = load i32, i32* @SIOCGIFNETMASK
%29 = call i32 @ioctl(i32 %9, i32 %28, %struct.ifreq* %5)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %36
33: ; preds = %26
%34 = call i32 @memcpy(%struct.sockaddr_in* %3, i32* %19, i32 4)
%35 = call i32 @close(i32 %9)
br label %36
36: ; preds = %4, %33, %31, %24, %16
%37 = phi i32 [ 1, %16 ], [ 1, %24 ], [ 1, %31 ], [ 0, %33 ], [ 1, %4 ]
ret i32 %37
}