#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 */
typedef int /*<<< orphan*/ u_short;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_aton(char const *, struct in_addr *);
int /*<<< orphan*/ memcpy(char *, struct in_addr *, int);
scalar_t__ strcasecmp(char const *, char *);
int BMI_sockio_init_sock(struct sockaddr *saddrp, const char *name,
                         int service) {
  int ret;
  struct in_addr addr;
  bzero((char *)saddrp, sizeof(struct sockaddr_in));
  if (name == NULL || strcasecmp(name, "localhost") == 0) {
    ret = inet_aton("127.0.0.1", &addr);
  } else {
    ret = inet_aton(name, &addr);
  }
  if (ret == 0)
    return -1;
  ((struct sockaddr_in *)saddrp)->sin_family = AF_INET;
  ((struct sockaddr_in *)saddrp)->sin_port = htons((u_short)service);
  memcpy((char *)&(((struct sockaddr_in *)saddrp)->sin_addr), &addr,
         sizeof(addr));
  return 0;
}


x86 assembly (clang, -O0):
<BMI_sockio_init_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
cmpq   $0x0,-0x18(%rbp)
je     1196 <BMI_sockio_init_sock+0x46>
mov    -0x18(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    11ae <BMI_sockio_init_sock+0x5e>
lea    0xe6d(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <inet_aton@plt>
mov    %eax,-0x20(%rbp)
jmp    11be <BMI_sockio_init_sock+0x6e>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <inet_aton@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11d4 <BMI_sockio_init_sock+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <BMI_sockio_init_sock+0xbe>
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BMI_sockio_init_sock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    $0xc,%esi
call   1070 <bzero@plt>
test   %rbp,%rbp
je     118e <BMI_sockio_init_sock+0x3e>
lea    0xe89(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     118e <BMI_sockio_init_sock+0x3e>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
jmp    119a <BMI_sockio_init_sock+0x4a>
lea    0xe75(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <inet_aton@plt>
test   %eax,%eax
je     11d0 <BMI_sockio_init_sock+0x80>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rbx)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
jmp    11d5 <BMI_sockio_init_sock+0x85>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
@.str = [10 x i8] c"localhost\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
define i32 @BMI_sockio_init_sock(%struct.sockaddr* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.in_addr
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load %struct.sockaddr*, %struct.sockaddr** %5
%11 = bitcast %struct.sockaddr* %10 to i8*
%12 = call i32 @bzero(i8* %11, i32 12)
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i64 @strcasecmp(i8* %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15, %3
%20 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.in_addr* %9)
store i32 %20, i32* %8
br label %24
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = call i32 @inet_aton(i8* %22, %struct.in_addr* %9)
store i32 %23, i32* %8
br label %24
24: ; preds = %21, %19
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 -1, i32* %4
br label %43
28: ; preds = %24
%29 = load i32, i32* @AF_INET
%30 = load %struct.sockaddr*, %struct.sockaddr** %5
%31 = bitcast %struct.sockaddr* %30 to %struct.sockaddr_in*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i32 0, i32 2
store i32 %29, i32* %32
%33 = load i32, i32* %7
%34 = call i32 @htons(i32 %33)
%35 = load %struct.sockaddr*, %struct.sockaddr** %5
%36 = bitcast %struct.sockaddr* %35 to %struct.sockaddr_in*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i32 0, i32 1
store i32 %34, i32* %37
%38 = load %struct.sockaddr*, %struct.sockaddr** %5
%39 = bitcast %struct.sockaddr* %38 to %struct.sockaddr_in*
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %39, i32 0, i32 0
%41 = bitcast i32* %40 to i8*
%42 = call i32 @memcpy(i8* %41, %struct.in_addr* %9, i32 4)
store i32 0, i32* %4
br label %43
43: ; preds = %28, %27
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.in_addr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
define i32 @BMI_sockio_init_sock(%struct.sockaddr* %0, i8* %1, i32 %2) {
%4 = alloca %struct.in_addr
%5 = bitcast %struct.in_addr* %4 to i8*
%6 = bitcast %struct.sockaddr* %0 to i8*
%7 = tail call i32 @bzero(i8* %6, i32 12)
%8 = icmp eq i8* %1, null
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = tail call i64 @strcasecmp(i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9, %3
%13 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %struct.in_addr* %4)
br label %16
14: ; preds = %9
%15 = call i32 @inet_aton(i8* %1, %struct.in_addr* %4)
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ %13, %12 ], [ %15, %14 ]
%18 = icmp eq i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 2, i32 0
store i32 %20, i32* %21
%22 = call i32 @htons(i32 %2)
%23 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 1, i32 0
store i32 %22, i32* %23
%24 = call i32 @memcpy(i8* %6, %struct.in_addr* %4, i32 4)
br label %25
25: ; preds = %16, %19
%26 = phi i32 [ 0, %19 ], [ -1, %16 ]
ret i32 %26
}