#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 {
  int dummy;
};
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ errno;
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int sendto_(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int send_packet(int i, FILE *fp, int sd, struct sockaddr *remoteServAddr,
                int addr_size) {
  if (fseek(fp, (i * 1004), SEEK_SET) != 0) {
    printf("error seeking to position in file");
  }
  int nbytes, s;
  char buffer[1004], packet[1024], index[9], size[9];
  bzero(buffer, sizeof(buffer));
  if ((s = fread(buffer, 1, 1004, fp)) > 0) {
    sprintf(index, "%08x", i);
    sprintf(size, "%08x", s);
    sprintf(index, index, 8);
    sprintf(size, size, 8);
    strcpy(packet, index);
    strcat(packet, size);
    strcat(packet, " ");
    strcat(packet, buffer);
    if ((nbytes = sendto_(sd, packet, 1024, 0, remoteServAddr, addr_size)) ==
        0) {
      printf("error sending packet %s\n", strerror(errno));
    }
    if (s < 1004)
      return 0;
    else
      return 1;
  } else {
    printf("error reading file into buffer %s\n", strerror(errno));
  }
  return 1;
}


x86 assembly (clang, -O0):
<send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
imul   $0x3ec,-0x8(%rbp),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     11de <send_packet+0x4e>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    $0x3ec,%esi
call   1080 <bzero@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3ec,%edx
call   1040 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    132b <send_packet+0x19b>
lea    -0x829(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xdfc(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x832(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xde6(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x829(%rbp),%rdi
lea    -0x829(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <sprintf@plt>
lea    -0x832(%rbp),%rdi
lea    -0x832(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <sprintf@plt>
lea    -0x820(%rbp),%rdi
lea    -0x829(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x820(%rbp),%rdi
lea    -0x832(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0x820(%rbp),%rdi
lea    0xd82(%rip),%rsi
call   1090 <strcat@plt>
lea    -0x820(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x14(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    -0x20(%rbp),%r8
mov    -0x24(%rbp),%r9d
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <sendto_@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    1306 <send_packet+0x176>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x3ec,-0x2c(%rbp)
jge    131f <send_packet+0x18f>
movl   $0x0,-0x4(%rbp)
jmp    1351 <send_packet+0x1c1>
movl   $0x1,-0x4(%rbp)
jmp    1351 <send_packet+0x1c1>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %r8d,%r15d
mov    %rcx,%r12
mov    %edx,0xc(%rsp)
mov    %rsi,%rbp
mov    %edi,%r14d
imul   $0x3ec,%edi,%esi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
call   1070 <fseek@plt>
test   %rax,%rax
je     11db <send_packet+0x4b>
lea    0xe2c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x30(%rsp),%r13
mov    %r13,%rdi
mov    $0x3ec,%esi
call   1080 <bzero@plt>
mov    $0x1,%ebx
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x3ec,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    12f1 <send_packet+0x161>
mov    %eax,%r13d
mov    %r15d,0x8(%rsp)
lea    0xe04(%rip),%r15
mov    %r12,0x10(%rsp)
lea    0x27(%rsp),%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   10a0 <sprintf@plt>
lea    0x1e(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   10a0 <sprintf@plt>
mov    %r12,%rdi
mov    %r12,%rsi
mov    $0x8,%edx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   10a0 <sprintf@plt>
lea    0x420(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
lea    0xd99(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strcat@plt>
lea    0x30(%rsp),%rsi
mov    %rbp,%rdi
call   1090 <strcat@plt>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    0x10(%rsp),%r8
mov    0x8(%rsp),%r9d
call   1060 <sendto_@plt>
test   %eax,%eax
jne    12e3 <send_packet+0x153>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd50(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %ebx,%ebx
cmp    $0x3ec,%r13d
setae  %bl
jmp    1310 <send_packet+0x180>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd3c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SEEK_SET = global i32 0
@.str = [34 x i8] c"error seeking to position in file\00"
@.str.1 = [5 x i8] c"%08x\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [25 x i8] c"error sending packet %s\0A\00"
@errno = global i32 0
@.str.4 = [35 x i8] c"error reading file into buffer %s\0A\00"
define i32 @send_packet(i32 %0, i32* %1, i32 %2, %struct.sockaddr* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.sockaddr*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [1004 x i8]
%15 = alloca [1024 x i8]
%16 = alloca [9 x i8]
%17 = alloca [9 x i8]
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store %struct.sockaddr* %3, %struct.sockaddr** %10
store i32 %4, i32* %11
%18 = load i32*, i32** %8
%19 = load i32, i32* %7
%20 = mul nsw i32 %19, 1004
%21 = load i32, i32* @SEEK_SET
%22 = call i64 @fseek(i32* %18, i32 %20, i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %5
%27 = getelementptr inbounds [1004 x i8], [1004 x i8]* %14, i64 0, i64 0
%28 = call i32 @bzero(i8* %27, i32 1004)
%29 = getelementptr inbounds [1004 x i8], [1004 x i8]* %14, i64 0, i64 0
%30 = load i32*, i32** %8
%31 = call i32 @fread(i8* %29, i32 1, i32 1004, i32* %30)
store i32 %31, i32* %13
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %72
33: ; preds = %26
%34 = getelementptr inbounds [9 x i8], [9 x i8]* %16, i64 0, i64 0
%35 = load i32, i32* %7
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds [9 x i8], [9 x i8]* %17, i64 0, i64 0
%38 = load i32, i32* %13
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds [9 x i8], [9 x i8]* %16, i64 0, i64 0
%41 = getelementptr inbounds [9 x i8], [9 x i8]* %16, i64 0, i64 0
%42 = call i32 @sprintf(i8* %40, i8* %41, i32 8)
%43 = getelementptr inbounds [9 x i8], [9 x i8]* %17, i64 0, i64 0
%44 = getelementptr inbounds [9 x i8], [9 x i8]* %17, i64 0, i64 0
%45 = call i32 @sprintf(i8* %43, i8* %44, i32 8)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%47 = getelementptr inbounds [9 x i8], [9 x i8]* %16, i64 0, i64 0
%48 = call i32 @strcpy(i8* %46, i8* %47)
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%50 = getelementptr inbounds [9 x i8], [9 x i8]* %17, i64 0, i64 0
%51 = call i32 @strcat(i8* %49, i8* %50)
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%53 = call i32 @strcat(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%55 = getelementptr inbounds [1004 x i8], [1004 x i8]* %14, i64 0, i64 0
%56 = call i32 @strcat(i8* %54, i8* %55)
%57 = load i32, i32* %9
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%59 = load %struct.sockaddr*, %struct.sockaddr** %10
%60 = load i32, i32* %11
%61 = call i32 @sendto_(i32 %57, i8* %58, i32 1024, i32 0, %struct.sockaddr* %59, i32 %60)
store i32 %61, i32* %12
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %33
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %63, %33
%68 = load i32, i32* %13
%69 = icmp slt i32 %68, 1004
br i1 %69, label %70, label %71
70: ; preds = %67
store i32 0, i32* %6
br label %77
71: ; preds = %67
store i32 1, i32* %6
br label %77
72: ; preds = %26
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %72
store i32 1, i32* %6
br label %77
77: ; preds = %76, %71, %70
%78 = load i32, i32* %6
ret i32 %78
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SEEK_SET = global i32 0
@.str = [34 x i8] c"error seeking to position in file\00"
@.str.1 = [5 x i8] c"%08x\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [25 x i8] c"error sending packet %s\0A\00"
@errno = global i32 0
@.str.4 = [35 x i8] c"error reading file into buffer %s\0A\00"
define i32 @send_packet(i32 %0, i32* nocapture %1, i32 %2, %struct.sockaddr* %3, i32 %4) {
%6 = alloca [1004 x i8]
%7 = alloca [1024 x i8]
%8 = alloca [9 x i8]
%9 = alloca [9 x i8]
%10 = mul nsw i32 %0, 1004
%11 = load i32, i32* @SEEK_SET
%12 = tail call i64 @fseek(i32* %1, i32 %10, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %5
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %5
%17 = getelementptr inbounds [1004 x i8], [1004 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%19 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 0
%20 = getelementptr inbounds [9 x i8], [9 x i8]* %9, i64 0, i64 0
%21 = call i32 @bzero(i8* %17, i32 1004)
%22 = call i32 @fread(i8* %17, i32 1, i32 1004, i32* %1)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %42
24: ; preds = %16
%25 = call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%26 = call i32 @sprintf(i8* %20, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%27 = call i32 @sprintf(i8* %19, i8* %19, i32 8)
%28 = call i32 @sprintf(i8* %20, i8* %20, i32 8)
%29 = call i32 @strcpy(i8* %18, i8* %19)
%30 = call i32 @strcat(i8* %18, i8* %20)
%31 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @strcat(i8* %18, i8* %17)
%33 = call i32 @sendto_(i32 %2, i8* %18, i32 1024, i32 0, %struct.sockaddr* %3, i32 %4)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %24
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %35, %24
%40 = icmp ugt i32 %22, 1003
%41 = zext i1 %40 to i32
br label %46
42: ; preds = %16
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %39, %42
%47 = phi i32 [ 1, %42 ], [ %41, %39 ]
ret i32 %47
}