#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;
  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;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int sock_init(struct sockaddr *saddrp, const char *hostname, int port) {
  struct hostent *hep;
  memset((char *)saddrp, 0, sizeof(struct sockaddr_in));
  if (hostname == NULL) {
    if ((hep = gethostbyname("localhost")) == NULL)
      return -1;
  } else if ((hep = gethostbyname(hostname)) == NULL)
    return -1;
  ((struct sockaddr_in *)saddrp)->sin_family = AF_INET;
  ((struct sockaddr_in *)saddrp)->sin_port = htons(port);
  memcpy(&((struct sockaddr_in *)saddrp)->sin_addr, hep->h_addr, hep->h_length);
  return 0;
}


x86 assembly (clang, -O0):
<sock_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1199 <sock_init+0x59>
lea    0xe8b(%rip),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1194 <sock_init+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <sock_init+0xbf>
jmp    11c1 <sock_init+0x81>
mov    -0x18(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11bc <sock_init+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <sock_init+0xbf>
jmp    11c1 <sock_init+0x81>
mov    0x2e20(%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   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_init>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
test   %rbp,%rbp
je     1163 <sock_init+0x23>
mov    %rbp,%rdi
jmp    116a <sock_init+0x2a>
lea    0xe96(%rip),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11a0 <sock_init+0x60>
mov    0x2e6a(%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   1060 <memcpy@plt>
xor    %eax,%eax
jmp    11a5 <sock_init+0x65>
mov    $0xffffffff,%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"
@AF_INET = global i32 0
define i32 @sock_init(%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 @memset(i8* %10, i32 0, i32 12)
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
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 %18
17: ; preds = %14
store i32 -1, i32* %4
br label %45
18: ; preds = %14
br label %25
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = call %struct.hostent* @gethostbyname(i8* %20)
store %struct.hostent* %21, %struct.hostent** %8
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %4
br label %45
24: ; preds = %19
br label %25
25: ; preds = %24, %18
%26 = load i32, i32* @AF_INET
%27 = load %struct.sockaddr*, %struct.sockaddr** %5
%28 = bitcast %struct.sockaddr* %27 to %struct.sockaddr_in*
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 2
store i32 %26, i32* %29
%30 = load i32, i32* %7
%31 = call i32 @htons(i32 %30)
%32 = load %struct.sockaddr*, %struct.sockaddr** %5
%33 = bitcast %struct.sockaddr* %32 to %struct.sockaddr_in*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 1
store i32 %31, i32* %34
%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 0
%38 = load %struct.hostent*, %struct.hostent** %8
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = load %struct.hostent*, %struct.hostent** %8
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @memcpy(i32* %37, i32 %40, i32 %43)
store i32 0, i32* %4
br label %45
45: ; preds = %25, %23, %17
%46 = load i32, i32* %4
ret i32 %46
}

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