#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 hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bzero(char *, int);
int conv_h_errno(int /*<<< orphan*/);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ h_errno;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int BMI_sockio_init_sock(struct sockaddr *saddrp, const char *name,
                         int service) {
  struct hostent *hep;
  bzero((char *)saddrp, sizeof(struct sockaddr_in));
  if (name == NULL) {
    if ((hep = gethostbyname("localhost")) == NULL) {
      return (-conv_h_errno(h_errno));
    }
  } else if ((hep = gethostbyname(name)) == NULL) {
    return (-conv_h_errno(h_errno));
  }
  ((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), hep->h_addr,
         hep->h_length);
  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   1060 <bzero@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <BMI_sockio_init_sock+0x67>
lea    0xe7d(%rip),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b2 <BMI_sockio_init_sock+0x62>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <conv_h_errno@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    122d <BMI_sockio_init_sock+0xdd>
jmp    11ef <BMI_sockio_init_sock+0x9f>
mov    -0x18(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ea <BMI_sockio_init_sock+0x9a>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1070 <conv_h_errno@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    122d <BMI_sockio_init_sock+0xdd>
jmp    11ef <BMI_sockio_init_sock+0x9f>
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%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
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    $0xc,%esi
call   1060 <bzero@plt>
test   %rbp,%rbp
je     1171 <BMI_sockio_init_sock+0x21>
mov    %rbp,%rdi
jmp    1178 <BMI_sockio_init_sock+0x28>
lea    0xe88(%rip),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11ae <BMI_sockio_init_sock+0x5e>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %rbx,%rdi
call   1050 <memcpy@plt>
xor    %eax,%eax
jmp    11be <BMI_sockio_init_sock+0x6e>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1070 <conv_h_errno@plt>
neg    %eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
@.str = [10 x i8] c"localhost\00"
@h_errno = global i32 0
@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 %struct.hostent*
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load %struct.sockaddr*, %struct.sockaddr** %5
%10 = bitcast %struct.sockaddr* %9 to i8*
%11 = call i32 @bzero(i8* %10, i32 12)
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %22
14: ; preds = %3
%15 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %15, %struct.hostent** %8
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @h_errno
%19 = call i32 @conv_h_errno(i32 %18)
%20 = sub nsw i32 0, %19
store i32 %20, i32* %4
br label %52
21: ; preds = %14
br label %31
22: ; preds = %3
%23 = load i8*, i8** %6
%24 = call %struct.hostent* @gethostbyname(i8* %23)
store %struct.hostent* %24, %struct.hostent** %8
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @h_errno
%28 = call i32 @conv_h_errno(i32 %27)
%29 = sub nsw i32 0, %28
store i32 %29, i32* %4
br label %52
30: ; preds = %22
br label %31
31: ; preds = %30, %21
%32 = load i32, i32* @AF_INET
%33 = load %struct.sockaddr*, %struct.sockaddr** %5
%34 = bitcast %struct.sockaddr* %33 to %struct.sockaddr_in*
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 2
store i32 %32, i32* %35
%36 = load i32, i32* %7
%37 = call i32 @htons(i32 %36)
%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 1
store i32 %37, i32* %40
%41 = load %struct.sockaddr*, %struct.sockaddr** %5
%42 = bitcast %struct.sockaddr* %41 to %struct.sockaddr_in*
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %42, i32 0, i32 0
%44 = bitcast i32* %43 to i8*
%45 = load %struct.hostent*, %struct.hostent** %8
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = load %struct.hostent*, %struct.hostent** %8
%49 = getelementptr inbounds %struct.hostent, %struct.hostent* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @memcpy(i8* %44, i32 %47, i32 %50)
store i32 0, i32* %4
br label %52
52: ; preds = %31, %26, %17
%53 = load i32, i32* %4
ret i32 %53
}

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