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

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
char const *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char const *);
char *get_ipconfig(const char *netcard_name, char *ip) {
  int sockfd;
  sockfd = socket(PF_INET, SOCK_STREAM, 0);
  if (-1 == sockfd) {
    perror("socket");
    return NULL;
  }
  struct ifreq req;
  struct sockaddr_in *host = NULL;
  memset(&req, 0, sizeof(struct ifreq));
  strcpy(req.ifr_name, netcard_name);
  if (ioctl(sockfd, SIOCGIFADDR, &req)) {
    perror("ioctl");
    return NULL;
  }
  host = (struct sockaddr_in *)&req.ifr_addr;
  strcpy(ip, inet_ntoa(host->sin_addr));
  close(sockfd);
  return ip;
}


x86 assembly (clang, -O0):
<get_ipconfig>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11c3 <get_ipconfig+0x53>
lea    0xe4f(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    125b <get_ipconfig+0xeb>
movq   $0x0,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     1220 <get_ipconfig+0xb0>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    125b <get_ipconfig+0xeb>
lea    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ipconfig>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1202 <get_ipconfig+0x92>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
jne    120b <get_ipconfig+0x9b>
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf7(%rip),%rdi
jmp    1212 <get_ipconfig+0xa2>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
xor    %r14d,%r14d
jmp    11f4 <get_ipconfig+0x84>

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
%struct.sockaddr_in = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i8* @get_ipconfig(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca %struct.sockaddr_in*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %40
16: ; preds = %2
store %struct.sockaddr_in* null, %struct.sockaddr_in** %8
%17 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 16)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = load i8*, i8** %4
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i32, i32* %6
%23 = load i32, i32* @SIOCGIFADDR
%24 = call i64 @ioctl(i32 %22, i32 %23, %struct.ifreq* %7)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %40
28: ; preds = %16
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%30 = bitcast i32* %29 to %struct.sockaddr_in*
store %struct.sockaddr_in* %30, %struct.sockaddr_in** %8
%31 = load i8*, i8** %5
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i8* @inet_ntoa(i32 %34)
%36 = call i32 @strcpy(i8* %31, i8* %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i8*, i8** %5
store i8* %39, i8** %3
br label %40
40: ; preds = %28, %26, %14
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
%struct.sockaddr_in = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i8* @get_ipconfig(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %29
10: ; preds = %2
%11 = bitcast %struct.ifreq* %3 to i8*
%12 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = call i32 @strcpy(i8* %14, i8* %0)
%16 = load i32, i32* @SIOCGIFADDR
%17 = call i64 @ioctl(i32 %6, i32 %16, %struct.ifreq* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %27
21: ; preds = %10
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%23 = load i32, i32* %22
%24 = call i8* @inet_ntoa(i32 %23)
%25 = call i32 @strcpy(i8* %1, i8* %24)
%26 = call i32 @close(i32 %6)
br label %27
27: ; preds = %21, %19
%28 = phi i8* [ null, %19 ], [ %1, %21 ]
br label %29
29: ; preds = %27, %8
%30 = phi i8* [ null, %8 ], [ %28, %27 ]
ret i8* %30
}