#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_port;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  int /*<<< orphan*/ h_addrtype;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr_in *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
scalar_t__ getsockname(int, struct sockaddr_in *, int *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int SetUpReceiver(u_short portnum) {
  char myname[100 + 1];
  int s, length;
  struct sockaddr_in sa;
  struct hostent *hp;
  bzero(&sa, sizeof(struct sockaddr_in));
  gethostname(myname, 100);
  hp = gethostbyname(myname);
  if (hp == NULL) {
    fprintf(stderr, "%s: unknown host\n", myname);
    exit(1);
  }
  sa.sin_family = hp->h_addrtype;
  sa.sin_port = htons(portnum);
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("opening stream socket");
    exit(1);
  }
  if (bind(s, &sa, sizeof(sa)) < 0) {
    perror("binding stream socket");
    close(s);
    return (-1);
  }
  length = sizeof(sa);
  if (getsockname(s, &sa, &length)) {
    perror("getting socket name");
    exit(1);
  }
  fprintf(stderr, "socket has port #%d\n", ntohs(sa.sin_port));
  listen(s, 3);
  return (s);
}


x86 assembly (clang, -O0):
<SetUpReceiver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x80(%rbp),%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
call   10d0 <gethostname@plt>
lea    -0x70(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    123d <SetUpReceiver+0x6d>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rdx
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x88(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x80(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x74(%rbp)
cmp    $0x0,%eax
jge    128f <SetUpReceiver+0xbf>
lea    0xd92(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x74(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12ca <SetUpReceiver+0xfa>
lea    0xd77(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x74(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1341 <SetUpReceiver+0x171>
movl   $0x8,-0x78(%rbp)
mov    -0x74(%rbp),%edi
lea    -0x80(%rbp),%rsi
lea    -0x78(%rbp),%rdx
call   10c0 <getsockname@plt>
cmp    $0x0,%rax
je     1301 <SetUpReceiver+0x131>
lea    0xd4c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x80(%rbp),%edi
call   1080 <ntohs@plt>
mov    -0x8c(%rbp),%edi
mov    %eax,%edx
lea    0xd2b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x74(%rbp),%edi
mov    $0x3,%esi
call   1070 <listen@plt>
mov    -0x74(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetUpReceiver>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
mov    %edi,%ebp
mov    %rsp,%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   10d0 <gethostname@plt>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     12c1 <SetUpReceiver+0xf1>
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12e7 <SetUpReceiver+0x117>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     12a7 <SetUpReceiver+0xd7>
movl   $0x8,0xc(%rsp)
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <getsockname@plt>
test   %rax,%rax
jne    12f0 <SetUpReceiver+0x120>
mov    0x2d62(%rip),%rax
mov    (%rax),%ebp
mov    (%rsp),%edi
call   1080 <ntohs@plt>
lea    0xdcb(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
mov    $0x3,%esi
call   1070 <listen@plt>
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd7a(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    129e <SetUpReceiver+0xce>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd2f(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd24(%rip),%rdi
jmp    12f7 <SetUpReceiver+0x127>
lea    0xd47(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

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

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