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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e2 <main+0x82>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1050 <layer4_read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0xffffffff,%eax
jne    11c4 <main+0x64>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
lea    0xe37(%rip),%rdi
call   1080 <debug_print@plt>
jmp    126b <main+0x10b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x400,%eax
jle    1229 <main+0xc9>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x414(%rbp),%edx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1272 <main+0x112>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x414(%rbp),%esi
call   1060 <layer4_write@plt>
cmp    $0xffffffff,%eax
jne    1266 <main+0x106>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    126b <main+0x10b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x400,%rsp
cmp    $0x2,%edi
jne    119d <main+0x3d>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1030 <strlen@plt>
cmp    $0x401,%eax
jl     11c7 <main+0x67>
mov    0x2e59(%rip),%rcx
mov    (%rcx),%edi
lea    0xe8b(%rip),%rsi
xor    %ebx,%ebx
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11dc <main+0x7c>
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <layer4_read@plt>
cmp    $0xffffffff,%eax
je     11e7 <main+0x87>
lea    0xe59(%rip),%rdi
mov    %rsp,%rsi
mov    %eax,%edx
call   1080 <debug_print@plt>
mov    $0x1,%ebx
jmp    11dc <main+0x7c>
mov    0x8(%rbx),%rdi
mov    %eax,%esi
call   1060 <layer4_write@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
je     11f9 <main+0x99>
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
ret
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
jmp    1209 <main+0xa9>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
xor    %eax,%eax
call   1040 <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"layer4_test\00"
@.str.2 = [33 x i8] c"Error - message is too long(%d)\0A\00"
@.str.3 = [23 x i8] c"Error sending message\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 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 [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = call i32 @layer4_read(i8* %11, i32 1024)
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 [1024 x i8], [1024 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, 1024
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 ([33 x i8], [33 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 @layer4_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 ([23 x i8], [23 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"layer4_test\00"
@.str.2 = [33 x i8] c"Error - message is too long(%d)\0A\00"
@.str.3 = [23 x i8] c"Error sending message\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 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 @layer4_read(i8* %4, i32 1024)
%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, 1024
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 ([33 x i8], [33 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 @layer4_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 ([23 x i8], [23 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
}