#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int BUFSIZ;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int make_dgram_server_socket(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
size_t recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ reply_to_sender(int, char *, struct sockaddr_in *, int);
int /*<<< orphan*/ say_who_called(struct sockaddr_in *);
int /*<<< orphan*/ stderr;
int main(int ac, char *av[]) {
  int port;
  int sock;
  char buf[BUFSIZ];
  size_t msglen;
  struct sockaddr_in saddr;
  socklen_t saddrlen;
  if (ac == 1 || (port = atoi(av[1])) <= 0) {
    fprintf(stderr, "usage: dgrecv portnumber\n");
    exit(1);
  }
  if ((sock = make_dgram_server_socket(port)) == -1) {
    perror("cannot make socket");
    exit(2);
  };
  saddrlen = sizeof(saddr);
  while ((msglen = recvfrom(sock, buf, BUFSIZ, 0, (struct sockaddr *)&saddr,
                            &saddrlen)) > 0) {
    buf[msglen] = '\0';
    printf("dgrecv: got a message: %s\n", buf);
    say_who_called(&saddr);
    reply_to_sender(sock, buf, &saddr, saddrlen);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     11f5 <main+0x65>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     1214 <main+0x84>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <make_dgram_server_socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    123e <main+0xae>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
movl   $0x4,-0x3c(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jbe    12ab <main+0x11b>
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xda8(%rip),%rdi
call   1030 <printf@plt>
lea    -0x38(%rbp),%rdi
call   1070 <say_who_called@plt>
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x3c(%rbp),%ecx
lea    -0x38(%rbp),%rdx
call   10b0 <reply_to_sender@plt>
jmp    1245 <main+0xb5>
movl   $0x0,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e33(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x1,%edi
je     127a <main+0xea>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
test   %eax,%eax
jle    127a <main+0xea>
mov    %eax,%edi
call   1060 <make_dgram_server_socket@plt>
cmp    $0xffffffff,%eax
je     1299 <main+0x109>
mov    %eax,%r14d
movl   $0x4,-0x2c(%rbp)
mov    (%r15),%edx
lea    -0x30(%rbp),%r8
lea    -0x2c(%rbp),%r9
mov    %eax,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1050 <recvfrom@plt>
test   %rax,%rax
je     1269 <main+0xd9>
lea    0xe1d(%rip),%r15
lea    -0x30(%rbp),%r12
lea    -0x2c(%rbp),%r13
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rbx,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %r12,%rdi
call   1070 <say_who_called@plt>
mov    -0x2c(%rbp),%ecx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10b0 <reply_to_sender@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   1050 <recvfrom@plt>
test   %rax,%rax
jne    1220 <main+0x90>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd7a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"usage: dgrecv portnumber\0A\00"
@.str.1 = [19 x i8] c"cannot make socket\00"
@.str.2 = [27 x i8] c"dgrecv: got a message: %s\0A\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 i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = alloca i8, i64 %14
store i64 %14, i64* %9
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 1
br i1 %18, label %25, label %19
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %6
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19, %2
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = load i32, i32* %6
%31 = call i32 @make_dgram_server_socket(i32 %30)
store i32 %31, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %29
store i32 4, i32* %12
br label %37
37: ; preds = %43, %36
%38 = load i32, i32* %7
%39 = load i32, i32* @BUFSIZ
%40 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%41 = call i64 @recvfrom(i32 %38, i8* %16, i32 %39, i32 0, %struct.sockaddr* %40, i32* %12)
store i64 %41, i64* %10
%42 = icmp ugt i64 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %37
%44 = load i64, i64* %10
%45 = getelementptr inbounds i8, i8* %16, i64 %44
store i8 0, i8* %45
%46 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%47 = call i32 @say_who_called(%struct.sockaddr_in* %11)
%48 = load i32, i32* %7
%49 = load i32, i32* %12
%50 = call i32 @reply_to_sender(i32 %48, i8* %16, %struct.sockaddr_in* %11, i32 %49)
br label %37
51: ; preds = %37
store i32 0, i32* %3
%52 = load i8*, i8** %8
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"usage: dgrecv portnumber\0A\00"
@.str.1 = [19 x i8] c"cannot make socket\00"
@.str.2 = [27 x i8] c"dgrecv: got a message: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = icmp eq i32 %0, 1
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %11, %2
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = tail call i32 @make_dgram_server_socket(i32 %14)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 2)
unreachable
26: ; preds = %20
store i32 4, i32* %4
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = load i32, i32* @BUFSIZ
%29 = call i64 @recvfrom(i32 %21, i8* %7, i32 %28, i32 0, %struct.sockaddr* %27, i32* %4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %41, label %31
31: ; preds = %26, %31
%32 = phi i64 [ %39, %31 ], [ %29, %26 ]
%33 = getelementptr inbounds i8, i8* %7, i64 %32
store i8 0, i8* %33
%34 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%35 = call i32 @say_who_called(%struct.sockaddr_in* %3)
%36 = load i32, i32* %4
%37 = call i32 @reply_to_sender(i32 %21, i8* %7, %struct.sockaddr_in* %3, i32 %36)
%38 = load i32, i32* @BUFSIZ
%39 = call i64 @recvfrom(i32 %21, i8* %7, i32 %38, i32 0, %struct.sockaddr* %27, i32* %4)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %31
41: ; preds = %31, %26
ret i32 0
}