#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;
};
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_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, &saddr, &saddrlen)) > 0) {
    buf[msglen] = '\0';
    printf("dgrecv: got a message: %s\n", buf);
    say_who_called(&saddr);
  }
  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    0x2e3b(%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     11e5 <main+0x65>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     1204 <main+0x84>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%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    122e <main+0xae>
lea    0xdfb(%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    0x2d95(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
lea    -0x38(%rbp),%r8
lea    -0x3c(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jbe    1288 <main+0x108>
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xdb8(%rip),%rdi
call   1030 <printf@plt>
lea    -0x38(%rbp),%rdi
call   1070 <say_who_called@plt>
jmp    1235 <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    0x2e43(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x1,%edi
je     1259 <main+0xd9>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
test   %eax,%eax
jle    1259 <main+0xd9>
mov    %eax,%edi
call   1060 <make_dgram_server_socket@plt>
cmp    $0xffffffff,%eax
je     1278 <main+0xf8>
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     1248 <main+0xc8>
lea    0xe2d(%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    0x2daa(%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    1210 <main+0x90>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd9b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = 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 = %42, %36
%38 = load i32, i32* %7
%39 = load i32, i32* @BUFSIZ
%40 = call i64 @recvfrom(i32 %38, i8* %16, i32 %39, i32 0, %struct.sockaddr_in* %11, i32* %12)
store i64 %40, i64* %10
%41 = icmp ugt i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i64, i64* %10
%44 = getelementptr inbounds i8, i8* %16, i64 %43
store i8 0, i8* %44
%45 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%46 = call i32 @say_who_called(%struct.sockaddr_in* %11)
br label %37
47: ; preds = %37
store i32 0, i32* %3
%48 = load i8*, i8** %8
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = 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 = load i32, i32* @BUFSIZ
%28 = call i64 @recvfrom(i32 %21, i8* %7, i32 %27, i32 0, %struct.sockaddr_in* %3, i32* %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %26, %30
%31 = phi i64 [ %36, %30 ], [ %28, %26 ]
%32 = getelementptr inbounds i8, i8* %7, i64 %31
store i8 0, i8* %32
%33 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%34 = call i32 @say_who_called(%struct.sockaddr_in* %3)
%35 = load i32, i32* @BUFSIZ
%36 = call i64 @recvfrom(i32 %21, i8* %7, i32 %35, i32 0, %struct.sockaddr_in* %3, i32* %4)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %30
38: ; preds = %30, %26
ret i32 0
}