#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ die(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ report(char *, char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_init(int tcp, char *host, int port) {
  struct sockaddr_in sa[1];
  struct hostent *hent;
  int s;
  hent = gethostbyname(host);
  if (!hent) {
    report("gethostbyname failed for %s", host);
    return -1;
  }
  if (tcp) {
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
      die("tcp socket open");
  } else {
    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
      die("udp socket open");
  }
  memset((char *)sa, 0, sizeof(*sa));
  sa->sin_family = AF_INET;
  sa->sin_port = htons(port);
  sa->sin_addr = *((struct in_addr *)hent->h_addr);
  if (connect(s, (struct sockaddr *)sa, sizeof(*sa)) < 0) {
    report("connect failed to %s:%d", host, port);
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<socket_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <socket_init+0x48>
mov    -0x10(%rbp),%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1090 <report@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <socket_init+0x142>
cmpl   $0x0,-0x8(%rbp)
je     1208 <socket_init+0x88>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1203 <socket_init+0x83>
lea    0xe1e(%rip),%rdi
call   1070 <die@plt>
jmp    1245 <socket_init+0xc5>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1240 <socket_init+0xc0>
lea    0xdf1(%rip),%rdi
call   1070 <die@plt>
jmp    1245 <socket_init+0xc5>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    12bc <socket_init+0x13c>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1090 <report@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <socket_init+0x142>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
mov    %rsi,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     11d4 <socket_init+0x54>
mov    %rax,%rbx
mov    0x2e3b(%rip),%r13
mov    0x0(%r13),%edi
test   %ebp,%ebp
je     11ea <socket_init+0x6a>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1213 <socket_init+0x93>
lea    0xe4a(%rip),%rdi
jmp    120e <socket_init+0x8e>
lea    0xe25(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1090 <report@plt>
jmp    1273 <socket_init+0xf3>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1213 <socket_init+0x93>
lea    0xe1e(%rip),%rdi
call   1070 <die@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    1278 <socket_init+0xf8>
lea    0xddd(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1090 <report@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [28 x i8] c"gethostbyname failed for %s\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"tcp socket open\00"
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.2 = [16 x i8] c"udp socket open\00"
@.str.3 = [24 x i8] c"connect failed to %s:%d\00"
define i32 @socket_init(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [1 x %struct.sockaddr_in]
%9 = alloca %struct.hostent*
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
%12 = call %struct.hostent* @gethostbyname(i8* %11)
store %struct.hostent* %12, %struct.hostent** %9
%13 = load %struct.hostent*, %struct.hostent** %9
%14 = icmp ne %struct.hostent* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i32 (i8*, i8*, ...) @report(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %4
br label %70
18: ; preds = %3
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %10
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
br label %38
29: ; preds = %18
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_DGRAM
%32 = load i32, i32* @IPPROTO_UDP
%33 = call i32 @socket(i32 %30, i32 %31, i32 %32)
store i32 %33, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
br label %38
38: ; preds = %37, %28
%39 = getelementptr inbounds [1 x %struct.sockaddr_in], [1 x %struct.sockaddr_in]* %8, i64 0, i64 0
%40 = bitcast %struct.sockaddr_in* %39 to i8*
%41 = call i32 @memset(i8* %40, i32 0, i32 12)
%42 = load i32, i32* @AF_INET
%43 = getelementptr inbounds [1 x %struct.sockaddr_in], [1 x %struct.sockaddr_in]* %8, i64 0, i64 0
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %43, i32 0, i32 2
store i32 %42, i32* %44
%45 = load i32, i32* %7
%46 = call i32 @htons(i32 %45)
%47 = getelementptr inbounds [1 x %struct.sockaddr_in], [1 x %struct.sockaddr_in]* %8, i64 0, i64 0
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %47, i32 0, i32 1
store i32 %46, i32* %48
%49 = getelementptr inbounds [1 x %struct.sockaddr_in], [1 x %struct.sockaddr_in]* %8, i64 0, i64 0
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %49, i32 0, i32 0
%51 = load %struct.hostent*, %struct.hostent** %9
%52 = getelementptr inbounds %struct.hostent, %struct.hostent* %51, i32 0, i32 0
%53 = load i64, i64* %52
%54 = inttoptr i64 %53 to %struct.in_addr*
%55 = bitcast %struct.in_addr* %50 to i8*
%56 = bitcast %struct.in_addr* %54 to i8*
%57 = load i32, i32* %10
%58 = getelementptr inbounds [1 x %struct.sockaddr_in], [1 x %struct.sockaddr_in]* %8, i64 0, i64 0
%59 = bitcast %struct.sockaddr_in* %58 to %struct.sockaddr*
%60 = call i64 @connect(i32 %57, %struct.sockaddr* %59, i32 12)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %38
%63 = load i8*, i8** %6
%64 = load i32, i32* %7
%65 = call i32 (i8*, i8*, ...) @report(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %63, i32 %64)
%66 = load i32, i32* %10
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %4
br label %70
68: ; preds = %38
%69 = load i32, i32* %10
store i32 %69, i32* %4
br label %70
70: ; preds = %68, %62, %15
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [28 x i8] c"gethostbyname failed for %s\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"tcp socket open\00"
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.2 = [16 x i8] c"udp socket open\00"
@.str.3 = [24 x i8] c"connect failed to %s:%d\00"
define i32 @socket_init(i32 %0, i8* %1, i32 %2) {
%4 = alloca [1 x %struct.sockaddr_in]
%5 = bitcast [1 x %struct.sockaddr_in]* %4 to i8*
%6 = tail call %struct.hostent* @gethostbyname(i8* %1)
%7 = icmp eq %struct.hostent* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (i8*, i8*, ...) @report(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %44
10: ; preds = %3
%11 = icmp eq i32 %0, 0
%12 = load i32, i32* @AF_INET
br i1 %11, label %17, label %13
13: ; preds = %10
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %12, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %22, label %26
17: ; preds = %10
%18 = load i32, i32* @SOCK_DGRAM
%19 = load i32, i32* @IPPROTO_UDP
%20 = tail call i32 @socket(i32 %12, i32 %18, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17, %13
%23 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %17 ]
%24 = phi i32 [ %15, %13 ], [ %20, %17 ]
%25 = tail call i32 @die(i8* %23)
br label %26
26: ; preds = %22, %17, %13
%27 = phi i32 [ %15, %13 ], [ %20, %17 ], [ %24, %22 ]
%28 = call i32 @memset(i8* %5, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds [1 x %struct.sockaddr_in], [1 x %struct.sockaddr_in]* %4, i64 0, i64 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @htons(i32 %2)
%32 = getelementptr inbounds [1 x %struct.sockaddr_in], [1 x %struct.sockaddr_in]* %4, i64 0, i64 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 0
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to i32*
%36 = getelementptr inbounds [1 x %struct.sockaddr_in], [1 x %struct.sockaddr_in]* %4, i64 0, i64 0, i32 0, i32 0
%37 = load i32, i32* %35
store i32 %37, i32* %36
%38 = bitcast [1 x %struct.sockaddr_in]* %4 to %struct.sockaddr*
%39 = call i64 @connect(i32 %27, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %26
%42 = call i32 (i8*, i8*, ...) @report(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 %2)
%43 = call i32 @close(i32 %27)
br label %44
44: ; preds = %26, %41, %8
%45 = phi i32 [ -1, %41 ], [ -1, %8 ], [ %27, %26 ]
ret i32 %45
}