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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             unsigned int *);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sock, n;
  unsigned int length;
  struct sockaddr_in server, from;
  struct hostent *hp;
  char buffer[256];
  if (argc != 3) {
    printf("Usage: server port\n");
    exit(1);
  }
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)
    error("socket");
  server.sin_family = AF_INET;
  hp = gethostbyname(argv[1]);
  if (hp == 0)
    error("Unknown host");
  bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
  server.sin_port = htons(atoi(argv[2]));
  length = sizeof(struct sockaddr_in);
  printf("Please enter the message: ");
  bzero(buffer, 256);
  fgets(buffer, 255, stdin);
  n = send(sock, buffer, strlen(buffer), 0);
  if (n < 0)
    error("Sendto");
  n = recvfrom(sock, buffer, 256, 0, (struct sockaddr *)&from, &length);
  if (n < 0)
    error("recvfrom");
  write(1, "Got an ack: ", 12);
  write(1, buffer, n);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1229 <main+0x39>
lea    0xde6(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125b <main+0x6b>
lea    0xdbe(%rip),%rdi
call   10d0 <error@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    128f <main+0x9f>
lea    0xd91(%rip),%rdi
call   10d0 <error@plt>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   10c0 <bcopy@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
movl   $0xc,-0x1c(%rbp)
lea    0xd59(%rip),%rdi
call   1070 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
lea    -0x140(%rbp),%rdi
mov    0x2ced(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1090 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x140(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1351 <main+0x161>
lea    0xcf7(%rip),%rdi
call   10d0 <error@plt>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x1c(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1388 <main+0x198>
lea    0xcc7(%rip),%rdi
call   10d0 <error@plt>
mov    $0x1,%edi
lea    0xcbf(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
lea    -0x140(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x128,%rsp
cmp    $0x3,%edi
jne    1325 <main+0x135>
mov    %rsi,%rbx
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1230 <main+0x40>
lea    0xdd5(%rip),%rdi
call   10d0 <error@plt>
mov    0x8(%rbx),%rdi
call   10b0 <gethostbyname@plt>
test   %rax,%rax
jne    124a <main+0x5a>
lea    0xdc2(%rip),%rdi
call   10d0 <error@plt>
mov    0x10(%rbx),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
movl   $0xc,0xc(%rsp)
lea    0xdab(%rip),%rdi
call   1080 <printf@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10e0 <bzero@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
test   %eax,%eax
jns    12bc <main+0xcc>
lea    0xd78(%rip),%rdi
call   10d0 <error@plt>
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebp,%edi
mov    $0x100,%edx
xor    %ecx,%ecx
call   10c0 <recvfrom@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12eb <main+0xfb>
lea    0xd50(%rip),%rdi
call   10d0 <error@plt>
lea    0xd4d(%rip),%rsi
mov    $0x1,%edi
mov    $0xc,%edx
call   1040 <write@plt>
lea    0x20(%rsp),%rsi
mov    $0x1,%edi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd20(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Usage: server port\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [13 x i8] c"Unknown host\00"
@.str.3 = [27 x i8] c"Please enter the message: \00"
@stdin = global i32 0
@.str.4 = [7 x i8] c"Sendto\00"
@.str.5 = [9 x i8] c"recvfrom\00"
@.str.6 = [13 x i8] c"Got an ack: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
%12 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call %struct.hostent* @gethostbyname(i8* %31)
store %struct.hostent* %32, %struct.hostent** %11
%33 = load %struct.hostent*, %struct.hostent** %11
%34 = icmp eq %struct.hostent* %33, null
br i1 %34, label %35, label %37
35: ; preds = %26
%36 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %26
%38 = load %struct.hostent*, %struct.hostent** %11
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 1
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to i8*
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%43 = bitcast i32* %42 to i8*
%44 = load %struct.hostent*, %struct.hostent** %11
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @bcopy(i8* %41, i8* %43, i32 %46)
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 2
%50 = load i8*, i8** %49
%51 = call i32 @atoi(i8* %50)
%52 = call i32 @htons(i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %52, i32* %53
store i32 12, i32* %8
%54 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%56 = call i32 @bzero(i8* %55, i32 256)
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%58 = load i32, i32* @stdin
%59 = call i32 @fgets(i8* %57, i32 255, i32 %58)
%60 = load i32, i32* %6
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%63 = call i32 @strlen(i8* %62)
%64 = call i32 @send(i32 %60, i8* %61, i32 %63, i32 0)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %37
%68 = call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %69
69: ; preds = %67, %37
%70 = load i32, i32* %6
%71 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%72 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%73 = call i32 @recvfrom(i32 %70, i8* %71, i32 256, i32 0, %struct.sockaddr* %72, i32* %8)
store i32 %73, i32* %7
%74 = load i32, i32* %7
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %69
%77 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %78
78: ; preds = %76, %69
%79 = call i32 @write(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 12)
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%81 = load i32, i32* %7
%82 = call i32 @write(i32 1, i8* %80, i32 %81)
%83 = load i32, i32* %6
%84 = call i32 @close(i32 %83)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [13 x i8] c"Unknown host\00"
@.str.3 = [27 x i8] c"Please enter the message: \00"
@stdin = global i32 0
@.str.4 = [7 x i8] c"Sendto\00"
@.str.5 = [9 x i8] c"recvfrom\00"
@.str.6 = [13 x i8] c"Got an ack: \00"
@str = [19 x i8] c"Usage: server port\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [256 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 3
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call %struct.hostent* @gethostbyname(i8* %22)
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = tail call i32 @atoi(i8* %29)
%31 = tail call i32 @htons(i32 %30)
store i32 12, i32* %3
%32 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @bzero(i8* %8, i32 256)
%34 = load i32, i32* @stdin
%35 = call i32 @fgets(i8* %8, i32 255, i32 %34)
%36 = call i32 @strlen(i8* %8)
%37 = call i32 @send(i32 %16, i8* %8, i32 %36, i32 0)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %27
%40 = call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %27
%42 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%43 = call i32 @recvfrom(i32 %16, i8* %8, i32 256, i32 0, %struct.sockaddr* %42, i32* %3)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %47
47: ; preds = %45, %41
%48 = call i32 @write(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 12)
%49 = call i32 @write(i32 1, i8* %8, i32 %43)
%50 = call i32 @close(i32 %16)
ret i32 0
}