#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 */
typedef int uint16_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sahn_cleanup();
int /*<<< orphan*/ sahn_init(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sahn_recv(int *, char *, int);
int /*<<< orphan*/ sahn_send(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("Usage: test1 <node-address>\n");
    exit(1);
  }
  uint16_t address = atoi(argv[1]), from;
  char *message = "Hello, world!";
  char buffer[64];
  sahn_init("examples/test3/nodes", address, NULL);
  switch (address) {
  case 19:
    sahn_recv(&from, buffer, 64);
    printf("Message from %d: %s\n", from, buffer);
    sahn_send(42, message, strlen(message) + 1);
    sahn_send(23, message, strlen(message) + 1);
    break;
  case 23:
    sahn_recv(&from, buffer, 64);
    printf("Message from %d: %s\n", from, buffer);
    break;
  case 42:
    sahn_send(19, message, strlen(message) + 1);
    sahn_recv(&from, buffer, 64);
    printf("Message from %d: %s\n", from, buffer);
    break;
  }
  sahn_cleanup();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bb <main+0x3b>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
lea    0xe4b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sahn_init@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x64(%rbp)
sub    $0x13,%eax
je     121f <main+0x9f>
jmp    11fd <main+0x7d>
mov    -0x64(%rbp),%eax
sub    $0x17,%eax
je     1293 <main+0x113>
jmp    120e <main+0x8e>
mov    -0x64(%rbp),%eax
sub    $0x2a,%eax
je     12bf <main+0x13f>
jmp    130a <main+0x18a>
lea    -0x60(%rbp),%rsi
lea    -0x18(%rbp),%rdi
mov    $0x40,%edx
call   1060 <sahn_recv@plt>
mov    -0x18(%rbp),%esi
lea    -0x60(%rbp),%rdx
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
mov    $0x2a,%edi
call   1040 <sahn_send@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
mov    $0x17,%edi
call   1040 <sahn_send@plt>
jmp    130a <main+0x18a>
lea    -0x60(%rbp),%rsi
lea    -0x18(%rbp),%rdi
mov    $0x40,%edx
call   1060 <sahn_recv@plt>
mov    -0x18(%rbp),%esi
lea    -0x60(%rbp),%rdx
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    130a <main+0x18a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
mov    $0x13,%edi
call   1040 <sahn_send@plt>
lea    -0x60(%rbp),%rsi
lea    -0x18(%rbp),%rdi
mov    $0x40,%edx
call   1060 <sahn_recv@plt>
mov    -0x18(%rbp),%esi
lea    -0x60(%rbp),%rdx
lea    0xd3d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1080 <sahn_cleanup@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x50,%rsp
cmp    $0x2,%edi
jne    128f <main+0xff>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1030 <sahn_init@plt>
cmp    $0x2a,%ebx
je     1235 <main+0xa5>
cmp    $0x17,%ebx
je     1254 <main+0xc4>
cmp    $0x13,%ebx
jne    1280 <main+0xf0>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x40,%edx
call   1070 <sahn_recv@plt>
mov    0xc(%rsp),%esi
lea    0xe31(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xdfd(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    $0x2a,%edi
mov    %rbx,%rsi
call   1040 <sahn_send@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    $0x17,%edi
mov    %rbx,%rsi
call   1040 <sahn_send@plt>
jmp    1280 <main+0xf0>
lea    0xdc4(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    $0x13,%edi
mov    %rbx,%rsi
call   1040 <sahn_send@plt>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x40,%edx
call   1070 <sahn_recv@plt>
mov    0xc(%rsp),%esi
lea    0xdad(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   1090 <sahn_cleanup@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xda2(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Usage: test1 <node-address>\0A\00"
@.str.1 = [14 x i8] c"Hello, world!\00"
@.str.2 = [21 x i8] c"examples/test3/nodes\00"
@.str.3 = [21 x i8] c"Message from %d: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [64 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8** %8
%20 = load i32, i32* %6
%21 = call i32 @sahn_init(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32* null)
%22 = load i32, i32* %6
switch i32 %22, label %56 [
i32 19, label %23
i32 23, label %39
i32 42, label %45
]
23: ; preds = %15
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%25 = call i32 @sahn_recv(i32* %7, i8* %24, i32 64)
%26 = load i32, i32* %7
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %26, i8* %27)
%29 = load i8*, i8** %8
%30 = load i8*, i8** %8
%31 = call i32 @strlen(i8* %30)
%32 = add nsw i32 %31, 1
%33 = call i32 @sahn_send(i32 42, i8* %29, i32 %32)
%34 = load i8*, i8** %8
%35 = load i8*, i8** %8
%36 = call i32 @strlen(i8* %35)
%37 = add nsw i32 %36, 1
%38 = call i32 @sahn_send(i32 23, i8* %34, i32 %37)
br label %56
39: ; preds = %15
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%41 = call i32 @sahn_recv(i32* %7, i8* %40, i32 64)
%42 = load i32, i32* %7
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %42, i8* %43)
br label %56
45: ; preds = %15
%46 = load i8*, i8** %8
%47 = load i8*, i8** %8
%48 = call i32 @strlen(i8* %47)
%49 = add nsw i32 %48, 1
%50 = call i32 @sahn_send(i32 19, i8* %46, i32 %49)
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%52 = call i32 @sahn_recv(i32* %7, i8* %51, i32 64)
%53 = load i32, i32* %7
%54 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %53, i8* %54)
br label %56
56: ; preds = %15, %45, %39, %23
%57 = call i32 (...) @sahn_cleanup()
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"Hello, world!\00"
@.str.2 = [21 x i8] c"examples/test3/nodes\00"
@.str.3 = [21 x i8] c"Message from %d: %s\0A\00"
@str = [28 x i8] c"Usage: test1 <node-address>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = bitcast i32* %3 to i8*
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%15 = tail call i32 @sahn_init(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32* null)
switch i32 %12, label %37 [
i32 19, label %16
i32 23, label %26
i32 42, label %30
]
16: ; preds = %9
%17 = call i32 @sahn_recv(i32* %3, i8* %14, i32 64)
%18 = load i32, i32* %3
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %18, i8* %14)
%20 = call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%21 = add nsw i32 %20, 1
%22 = call i32 @sahn_send(i32 42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%24 = add nsw i32 %23, 1
%25 = call i32 @sahn_send(i32 23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %37
26: ; preds = %9
%27 = call i32 @sahn_recv(i32* %3, i8* %14, i32 64)
%28 = load i32, i32* %3
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %28, i8* %14)
br label %37
30: ; preds = %9
%31 = tail call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%32 = add nsw i32 %31, 1
%33 = tail call i32 @sahn_send(i32 19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = call i32 @sahn_recv(i32* %3, i8* %14, i32 64)
%35 = load i32, i32* %3
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %35, i8* %14)
br label %37
37: ; preds = %9, %30, %26, %16
%38 = call i32 (...) @sahn_cleanup()
ret i32 0
}