#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 scalar_t__ u_short;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct hostent {
  int /*<<< orphan*/ h_addrtype;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr_in *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ htons(scalar_t__);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int CallSocket(char *name, u_short portnum) {
  struct sockaddr_in sa;
  struct hostent *hp;
  char hostname[100 + 1];
  int a, s;
  if (!name)
    gethostname(hostname, 100);
  else
    strcpy(hostname, name);
  if ((hp = gethostbyname(hostname)) == NULL) {
    fprintf(stderr, "%s: unknown host\n", hostname);
    exit(1);
  }
  bzero(&sa, sizeof(sa));
  bcopy(hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
  sa.sin_family = hp->h_addrtype;
  sa.sin_port = htons((u_short)portnum);
  if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
    perror("opening stream socket");
    exit(1);
  }
  if (connect(s, &sa, sizeof(sa)) < 0) {
    perror("connecting stream socket");
    close(s);
    exit(1);
  }
  return (s);
}


x86 assembly (clang, -O0):
<CallSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11f4 <CallSocket+0x34>
lea    -0x90(%rbp),%rdi
mov    $0x64,%esi
call   10b0 <gethostname@plt>
jmp    1204 <CallSocket+0x44>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x90(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1244 <CallSocket+0x84>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    -0x90(%rbp),%rdx
lea    0xdcb(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edi
lea    -0x20(%rbp),%rsi
add    $0x8,%rsi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edx
call   1080 <bcopy@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x98(%rbp)
cmp    $0x0,%eax
jge    12bd <CallSocket+0xfd>
lea    0xd64(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x98(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    12fc <CallSocket+0x13c>
lea    0xd46(%rip),%rdi
call   1090 <perror@plt>
mov    -0x98(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x98(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CallSocket>:
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%r14
test   %rdi,%rdi
je     11de <CallSocket+0x1e>
mov    %rdi,%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
jmp    11ed <CallSocket+0x2d>
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
call   10b0 <gethostname@plt>
lea    0x10(%rsp),%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1263 <CallSocket+0xa3>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%edi
lea    0x8(%rsp),%rsi
call   1080 <bcopy@plt>
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     1287 <CallSocket+0xc7>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     129d <CallSocket+0xdd>
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [18 x i8] c"%s: unknown host\0A\00"
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"opening stream socket\00"
@.str.2 = [25 x i8] c"connecting stream socket\00"
define i32 @CallSocket(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.hostent*
%7 = alloca [101 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%14 = call i32 @gethostname(i8* %13, i32 100)
br label %19
15: ; preds = %2
%16 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = call i32 @strcpy(i8* %16, i8* %17)
br label %19
19: ; preds = %15, %12
%20 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%21 = call %struct.hostent* @gethostbyname(i8* %20)
store %struct.hostent* %21, %struct.hostent** %6
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %19
%29 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%30 = load %struct.hostent*, %struct.hostent** %6
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 2
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
%34 = bitcast i32* %33 to i8*
%35 = load %struct.hostent*, %struct.hostent** %6
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = call i32 @bcopy(i32 %32, i8* %34, i32 %37)
%39 = load %struct.hostent*, %struct.hostent** %6
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %41, i32* %42
%43 = load i64, i64* %4
%44 = call i32 @htons(i64 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %44, i32* %45
%46 = load %struct.hostent*, %struct.hostent** %6
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* @SOCK_STREAM
%50 = call i32 @socket(i32 %48, i32 %49, i32 0)
store i32 %50, i32* %9
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %28
%53 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %28
%56 = load i32, i32* %9
%57 = call i64 @connect(i32 %56, %struct.sockaddr_in* %5, i32 12)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%61 = load i32, i32* %9
%62 = call i32 @close(i32 %61)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %55
%65 = load i32, i32* %9
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [18 x i8] c"%s: unknown host\0A\00"
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"opening stream socket\00"
@.str.2 = [25 x i8] c"connecting stream socket\00"
define i32 @CallSocket(i8* %0, i64 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [101 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [101 x i8], [101 x i8]* %4, i64 0, i64 0
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @gethostname(i8* %6, i32 100)
br label %12
10: ; preds = %2
%11 = call i32 @strcpy(i8* %6, i8* %0)
br label %12
12: ; preds = %10, %8
%13 = call %struct.hostent* @gethostbyname(i8* %6)
%14 = icmp eq %struct.hostent* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %6)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 2
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%24 = bitcast i32* %23 to i8*
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 1
%26 = load i32, i32* %25
%27 = call i32 @bcopy(i32 %22, i8* %24, i32 %26)
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i64 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %29, i32* %30
%31 = call i32 @htons(i64 %1)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %28
%34 = load i32, i32* @SOCK_STREAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %19
%38 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %19
%41 = call i64 @connect(i32 %35, %struct.sockaddr_in* %3, i32 12)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @close(i32 %35)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %40
ret i32 %35
}