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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htonl(int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int /*<<< orphan*/ *, int);
int netutil_start_dns(const char *host, int *fdp) {
  int fds[2];
  pid_t childpid;
  if (pipe(fds) < 0) {
    return -1;
  }
  childpid = fork();
  if (childpid == -1) {
    return -1;
  }
  if (childpid == 0) {
    struct hostent *hent;
    if (fork())
      exit(0);
    close(fds[0]);
    hent = gethostbyname(host);
    if (hent == NULL) {
      in_addr_t error = htonl(-1);
      write(fds[1], &error, 4);
    } else {
      write(fds[1], hent->h_addr, 4);
    }
    close(fds[1]);
    exit(0);
  } else {
    close(fds[1]);
    waitpid(childpid, NULL, 0);
    *fdp = fds[0];
    return 0;
  }
}


x86 assembly (clang, -O0):
<netutil_start_dns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11af <netutil_start_dns+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <netutil_start_dns+0xfc>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11cf <netutil_start_dns+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <netutil_start_dns+0xfc>
cmpl   $0x0,-0x24(%rbp)
jne    1256 <netutil_start_dns+0xd6>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%eax
je     11f0 <netutil_start_dns+0x70>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1233 <netutil_start_dns+0xb3>
mov    $0xffffffff,%edi
call   1040 <htonl@plt>
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    1247 <netutil_start_dns+0xc7>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netutil_start_dns>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x4(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     11cd <netutil_start_dns+0x4d>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11d2 <netutil_start_dns+0x52>
test   %ebp,%ebp
je     11dd <netutil_start_dns+0x5d>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x4(%rsp),%eax
mov    %eax,(%rbx)
xor    %ebp,%ebp
jmp    11d2 <netutil_start_dns+0x52>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1217 <netutil_start_dns+0x97>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    %r14,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
jne    121e <netutil_start_dns+0x9e>
mov    $0xffffffff,%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xc(%rsp),%rsi
jmp    1225 <netutil_start_dns+0xa5>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x8(%rsp),%edi
mov    (%rax),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32* }
define i32 @netutil_start_dns(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca %struct.hostent*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %61
14: ; preds = %2
%15 = call i32 (...) @fork()
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 -1, i32* %3
br label %61
19: ; preds = %14
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %52
22: ; preds = %19
%23 = call i32 (...) @fork()
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %22
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %4
%32 = call %struct.hostent* @gethostbyname(i8* %31)
store %struct.hostent* %32, %struct.hostent** %8
%33 = load %struct.hostent*, %struct.hostent** %8
%34 = icmp eq %struct.hostent* %33, null
br i1 %34, label %35, label %40
35: ; preds = %27
%36 = call i32 @htonl(i32 -1)
store i32 %36, i32* %9
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @write(i32 %38, i32* %9, i32 4)
br label %47
40: ; preds = %27
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%42 = load i32, i32* %41
%43 = load %struct.hostent*, %struct.hostent** %8
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 0
%45 = load i32*, i32** %44
%46 = call i32 @write(i32 %42, i32* %45, i32 4)
br label %47
47: ; preds = %40, %35
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %19
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %7
%57 = call i32 @waitpid(i32 %56, i32* null, i32 0)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%59 = load i32, i32* %58
%60 = load i32*, i32** %5
store i32 %59, i32* %60
store i32 0, i32* %3
br label %61
61: ; preds = %52, %18, %13
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.hostent = type { i32* }
define i32 @netutil_start_dns(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %44, label %9
9: ; preds = %2
%10 = call i32 (...) @fork()
switch i32 %10, label %38 [
i32 -1, label %44
i32 0, label %11
]
11: ; preds = %9
%12 = call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %11
%17 = load i32, i32* %6
%18 = call i32 @close(i32 %17)
%19 = call %struct.hostent* @gethostbyname(i8* %0)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = bitcast i32* %4 to i8*
%23 = call i32 @htonl(i32 -1)
store i32 %23, i32* %4
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @write(i32 %25, i32* %4, i32 4)
br label %33
27: ; preds = %16
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%31 = load i32*, i32** %30
%32 = call i32 @write(i32 %29, i32* %31, i32 4)
br label %33
33: ; preds = %27, %21
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %9
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = call i32 @waitpid(i32 %10, i32* null, i32 0)
%43 = load i32, i32* %6
store i32 %43, i32* %1
br label %44
44: ; preds = %9, %2, %38
%45 = phi i32 [ 0, %38 ], [ -1, %2 ], [ %10, %9 ]
ret i32 %45
}