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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int lookup_host(const char *host, int port, struct sockaddr_in *addr) {
  struct hostent *h = gethostbyname(host);
  if (h == NULL)
    return 0;
  bzero(addr, sizeof(struct sockaddr_in));
  addr->sin_family = AF_INET;
  addr->sin_port = htons(port);
  memcpy(&addr->sin_addr, h->h_addr, sizeof(struct in_addr));
  return 1;
}


x86 assembly (clang, -O0):
<lookup_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1177 <lookup_host+0x37>
movl   $0x0,-0x4(%rbp)
jmp    11c1 <lookup_host+0x81>
mov    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_host>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r14d
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1192 <lookup_host+0x52>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    $0x1,%eax
jmp    1194 <lookup_host+0x54>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
define i32 @lookup_host(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.hostent*
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%9 = load i8*, i8** %5
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %8
%11 = load %struct.hostent*, %struct.hostent** %8
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 0, i32* %4
br label %30
14: ; preds = %3
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%16 = call i32 @bzero(%struct.sockaddr_in* %15, i32 12)
%17 = load i32, i32* @AF_INET
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 2
store i32 %17, i32* %19
%20 = load i32, i32* %6
%21 = call i32 @htons(i32 %20)
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 1
store i32 %21, i32* %23
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 0
%26 = load %struct.hostent*, %struct.hostent** %8
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i32* %25, i32 %28, i32 4)
store i32 1, i32* %4
br label %30
30: ; preds = %14, %13
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
define i32 @lookup_host(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = tail call %struct.hostent* @gethostbyname(i8* %0)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = tail call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 %1)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @memcpy(i32* %12, i32 %14, i32 4)
br label %16
16: ; preds = %3, %6
%17 = phi i32 [ 1, %6 ], [ 0, %3 ]
ret i32 %17
}