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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ diep(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(void) {
  struct sockaddr_in si_other;
  int s, i;
  socklen_t slen = sizeof(si_other);
  char buf[512];
  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    diep("socket");
  memset((char *)&si_other, 0, sizeof(si_other));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(9930);
  if (inet_aton("127.0.0.1", &si_other.sin_addr) == 0) {
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
  }
  for (i = 0; i < 10; i++) {
    printf("Sending packet %d\n", i);
    sprintf(buf, "This is packet %d\n", i);
    if (sendto(s, buf, 512, 0, (struct sockaddr *)&si_other, slen) == -1)
      diep("sendto()");
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xc,-0x1c(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1201 <main+0x51>
lea    0xe04(%rip),%rdi
call   1080 <diep@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x26ca,%edi
call   1030 <htons@plt>
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
lea    0xdd2(%rip),%rdi
call   10a0 <inet_aton@plt>
cmp    $0x0,%rax
jne    1263 <main+0xb3>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    12df <main+0x12f>
mov    -0x18(%rbp),%esi
lea    0xda7(%rip),%rdi
call   1040 <printf@plt>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xda4(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
lea    -0x10(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    $0x200,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
cmp    $0xffffffff,%eax
jne    12cc <main+0x11c>
lea    0xd84(%rip),%rdi
call   1080 <diep@plt>
jmp    12d1 <main+0x121>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    126a <main+0xba>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    0x2e20(%rip),%r14
mov    (%r14),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11f6 <main+0x46>
lea    0xe0f(%rip),%rdi
call   1080 <diep@plt>
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    $0x26ca,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xde1(%rip),%rdi
mov    %rbx,%rsi
call   10a0 <inet_aton@plt>
test   %rax,%rax
je     12b9 <main+0x109>
lea    0xde7(%rip),%r14
lea    0xdf3(%rip),%r15
lea    0x10(%rsp),%rbx
mov    %rsp,%r13
jmp    1258 <main+0xa8>
nop
add    $0x1,%ebp
cmp    $0xa,%ebp
je     129d <main+0xed>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   10b0 <sprintf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1250 <main+0xa0>
lea    0xdb5(%rip),%rdi
call   1080 <diep@plt>
jmp    1250 <main+0xa0>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd48(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"inet_aton() failed\0A\00"
@.str.3 = [19 x i8] c"Sending packet %d\0A\00"
@.str.4 = [19 x i8] c"This is packet %d\0A\00"
@.str.5 = [9 x i8] c"sendto()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
store i32 0, i32* %1
store i32 12, i32* %5
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = bitcast %struct.sockaddr_in* %2 to i8*
%16 = call i32 @memset(i8* %15, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 9930)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%22 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %14
store i32 0, i32* %4
br label %29
29: ; preds = %47, %28
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 10
br i1 %31, label %32, label %50
32: ; preds = %29
%33 = load i32, i32* %4
%34 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%36 = load i32, i32* %4
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %3
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%40 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%41 = load i32, i32* %5
%42 = call i32 @sendto(i32 %38, i8* %39, i32 512, i32 0, %struct.sockaddr* %40, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %32
%45 = call i32 @diep(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %46
46: ; preds = %44, %32
br label %47
47: ; preds = %46
%48 = load i32, i32* %4
%49 = add nsw i32 %48, 1
store i32 %49, i32* %4
br label %29
50: ; preds = %29
%51 = load i32, i32* %3
%52 = call i32 @close(i32 %51)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"inet_aton() failed\0A\00"
@.str.3 = [19 x i8] c"Sending packet %d\0A\00"
@.str.4 = [19 x i8] c"This is packet %d\0A\00"
@.str.5 = [9 x i8] c"sendto()\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [512 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @IPPROTO_UDP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @diep(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = call i32 @memset(i8* %3, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 9930)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%19 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %12
%22 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %27
23: ; preds = %12
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21, %35
%28 = phi i32 [ 0, %21 ], [ %36, %35 ]
%29 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%31 = call i32 @sendto(i32 %8, i8* %4, i32 512, i32 0, %struct.sockaddr* %22, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @diep(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %27, %33
%36 = add nuw nsw i32 %28, 1
%37 = icmp eq i32 %36, 10
br i1 %37, label %38, label %27
38: ; preds = %35
%39 = call i32 @close(i32 %8)
ret i32 0
}