#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 /*<<< orphan*/ debug_print(char *, char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int layer2_read(char *, int);
int layer2_write(char *, int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int main(int argc, char *argv[]) {
  char chunk[16];
  int len;
  if (argc != 2) {
    if ((len = layer2_read(chunk, 16)) == -1) {
      fprintf(stderr, "Reading error\n");
      exit(1);
    }
    debug_print("layer2_test", chunk, len);
  } else {
    if ((len = strlen(argv[1])) > 16) {
      fprintf(stderr, "Error - chunk is too long(%d)\n", len);
      return 0;
    }
    if (layer2_write(argv[1], len) == -1) {
      fprintf(stderr, "Error sending chunk\n");
      exit(1);
    }
  }
  return 1;
}


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     11d3 <main+0x73>
lea    -0x20(%rbp),%rdi
mov    $0x10,%esi
call   1030 <layer2_read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11bb <main+0x5b>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xe46(%rip),%rdi
call   1080 <debug_print@plt>
jmp    1251 <main+0xf1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x10,%eax
jle    1212 <main+0xb2>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1258 <main+0xf8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x24(%rbp),%esi
call   1060 <layer2_write@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0xec>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1251 <main+0xf1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1198 <main+0x38>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1040 <strlen@plt>
cmp    $0x11,%eax
jl     11c2 <main+0x62>
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%edi
lea    0xe90(%rip),%rsi
xor    %ebx,%ebx
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11d7 <main+0x77>
mov    %rsp,%rdi
mov    $0x10,%esi
call   1030 <layer2_read@plt>
cmp    $0xffffffff,%eax
je     11df <main+0x7f>
lea    0xe5e(%rip),%rdi
mov    %rsp,%rsi
mov    %eax,%edx
call   1080 <debug_print@plt>
mov    $0x1,%ebx
jmp    11d7 <main+0x77>
mov    0x8(%rbx),%rdi
mov    %eax,%esi
call   1060 <layer2_write@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
je     11f1 <main+0x91>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
jmp    1201 <main+0xa1>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"Reading error\0A\00"
@.str.1 = [12 x i8] c"layer2_test\00"
@.str.2 = [31 x i8] c"Error - chunk is too long(%d)\0A\00"
@.str.3 = [21 x i8] c"Error sending chunk\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [16 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %22
10: ; preds = %2
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%12 = call i32 @layer2_read(i8* %11, i32 16)
store i32 %12, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%20 = load i32, i32* %7
%21 = call i32 @debug_print(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %20)
br label %44
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @strlen(i8* %25)
store i32 %26, i32* %7
%27 = icmp sgt i32 %26, 16
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i32, i32* %7
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %30)
store i32 0, i32* %3
br label %45
32: ; preds = %22
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = load i32, i32* %7
%37 = call i32 @layer2_write(i8* %35, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
br label %44
44: ; preds = %43, %18
store i32 1, i32* %3
br label %45
45: ; preds = %44, %28
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"Reading error\0A\00"
@.str.1 = [12 x i8] c"layer2_test\00"
@.str.2 = [31 x i8] c"Error - chunk is too long(%d)\0A\00"
@.str.3 = [21 x i8] c"Error sending chunk\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = call i32 @layer2_read(i8* %4, i32 16)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %6
%14 = call i32 @debug_print(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 %7)
br label %31
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @strlen(i8* %17)
%19 = icmp sgt i32 %18, 16
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %31
23: ; preds = %15
%24 = load i8*, i8** %16
%25 = tail call i32 @layer2_write(i8* %24, i32 %18)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %13, %23, %20
%32 = phi i32 [ 0, %20 ], [ 1, %23 ], [ 1, %13 ]
ret i32 %32
}