#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);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int init_sock(struct sockaddr *saddrp, char *name, int service) {
  struct hostent *hep;
  bzero((char *)saddrp, sizeof(struct sockaddr_in));
  if (name == NULL) {
    if ((hep = gethostbyname("localhost")) == NULL) {
      return (-1);
    }
  } else if ((hep = gethostbyname(name)) == NULL) {
    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), hep->h_addr,
         hep->h_length);
  return (0);
}


x86 assembly (clang, -O0):
<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    1197 <init_sock+0x57>
lea    0xe8d(%rip),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1192 <init_sock+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <init_sock+0xbd>
jmp    11bf <init_sock+0x7f>
mov    -0x18(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ba <init_sock+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <init_sock+0xbd>
jmp    11bf <init_sock+0x7f>
mov    0x2e22(%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):
<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     1161 <init_sock+0x21>
mov    %rbp,%rdi
jmp    1168 <init_sock+0x28>
lea    0xe98(%rip),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
je     119e <init_sock+0x5e>
mov    0x2e6c(%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    11a3 <init_sock+0x63>
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 @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 %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 %46
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 %46
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 = bitcast i32* %37 to i8*
%39 = load %struct.hostent*, %struct.hostent** %8
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = load %struct.hostent*, %struct.hostent** %8
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @memcpy(i8* %38, i32 %41, i32 %44)
store i32 0, i32* %4
br label %46
46: ; preds = %25, %23, %17
%47 = load i32, i32* %4
ret i32 %47
}

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 @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 %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 %24, label %13
10: ; preds = %3
%11 = tail call %struct.hostent* @gethostbyname(i8* %1)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %24, 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 inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @memcpy(i8* %4, i32 %20, i32 %22)
br label %24
24: ; preds = %10, %7, %13
%25 = phi i32 [ 0, %13 ], [ -1, %7 ], [ -1, %10 ]
ret i32 %25
}