#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 stat {
  int st_size;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ print_address(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
int main(int argc, char *argv[]) {
  struct stat sbuf;
  char *packet;
  int fd;
  if (argc != 2) {
    printf("Usage: %s <packet file>\n", argv[0]);
    return EXIT_FAILURE;
  }
  if ((stat(argv[1], &sbuf)) < 0) {
    printf("Error opening packet file\n");
    return EXIT_FAILURE;
  }
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    printf("Error opening packet file\n");
    return EXIT_FAILURE;
  }
  if ((packet = (char *)malloc(sbuf.st_size * sizeof(char))) == NULL) {
    printf("Error allocating memory\n");
    return EXIT_FAILURE;
  }
  if (read(fd, packet, sbuf.st_size) < 0) {
    printf("Error reading packet from file\n");
    return EXIT_FAILURE;
  }
  close(fd);
  print_address(packet);
  free(packet);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c6 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12db <main+0x15b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    1200 <main+0x80>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12db <main+0x15b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1241 <main+0xc1>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12db <main+0x15b>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    127d <main+0xfd>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12db <main+0x15b>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    12b5 <main+0x135>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12db <main+0x15b>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1040 <print_address@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1207 <main+0x77>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1090 <stat@plt>
test   %rax,%rax
js     121a <main+0x8a>
mov    0x8(%rbx),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
test   %eax,%eax
js     121a <main+0x8a>
mov    %eax,%ebx
mov    (%rsp),%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1237 <main+0xa7>
mov    %rax,%r14
mov    (%rsp),%edx
mov    %ebx,%edi
mov    %rax,%rsi
call   1080 <read@plt>
test   %rax,%rax
js     1240 <main+0xb0>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14,%rdi
call   1050 <print_address@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2dcb(%rip),%rax
jmp    122d <main+0x9d>
mov    (%rbx),%rsi
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1226 <main+0x96>
lea    0xe2f(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdfa(%rip),%rdi
jmp    1221 <main+0x91>
lea    0xdd2(%rip),%rdi
jmp    1221 <main+0x91>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [25 x i8] c"Usage: %s <packet file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [27 x i8] c"Error opening packet file\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [25 x i8] c"Error allocating memory\0A\00"
@.str.3 = [32 x i8] c"Error reading packet from file\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %3
br label %66
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @stat(i8* %20, %struct.stat* %6)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %3
br label %66
26: ; preds = %17
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
store i32 %35, i32* %3
br label %66
36: ; preds = %26
%37 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%38 = load i32, i32* %37
%39 = sext i32 %38 to i64
%40 = mul i64 %39, 1
%41 = trunc i64 %40 to i32
%42 = call i64 @malloc(i32 %41)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %7
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %36
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
store i32 %47, i32* %3
br label %66
48: ; preds = %36
%49 = load i32, i32* %8
%50 = load i8*, i8** %7
%51 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @read(i32 %49, i8* %50, i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
store i32 %57, i32* %3
br label %66
58: ; preds = %48
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
%61 = load i8*, i8** %7
%62 = call i32 @print_address(i8* %61)
%63 = load i8*, i8** %7
%64 = call i32 @free(i8* %63)
%65 = load i32, i32* @EXIT_SUCCESS
store i32 %65, i32* %3
br label %66
66: ; preds = %58, %55, %45, %33, %23, %11
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [25 x i8] c"Usage: %s <packet file>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [31 x i8] c"Error reading packet from file\00"
@str.4 = [24 x i8] c"Error allocating memory\00"
@str.6 = [26 x i8] c"Error opening packet file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %41
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i64 @stat(i8* %11, %struct.stat* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0))
br label %41
16: ; preds = %9
%17 = load i8*, i8** %10
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0))
br label %41
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = icmp eq i64 %26, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.4, i64 0, i64 0))
br label %41
31: ; preds = %23
%32 = load i32, i32* %24
%33 = call i64 @read(i32 %19, i8* %27, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %41
37: ; preds = %31
%38 = call i32 @close(i32 %19)
%39 = call i32 @print_address(i8* %27)
%40 = call i32 @free(i8* %27)
br label %41
41: ; preds = %37, %35, %29, %21, %14, %6
%42 = phi i32* [ @EXIT_FAILURE, %6 ], [ @EXIT_FAILURE, %14 ], [ @EXIT_FAILURE, %21 ], [ @EXIT_FAILURE, %29 ], [ @EXIT_FAILURE, %35 ], [ @EXIT_SUCCESS, %37 ]
%43 = load i32, i32* %42
ret i32 %43
}