#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*/ sleep(int);
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/test4/nodes", address, NULL);
  switch (address) {
  case 19:
    while (1) {
      sahn_recv(&from, buffer, 64);
      printf("Message from %d: %s\n", from, buffer);
    }
    break;
  case 42:
    while (1) {
      sahn_send(19, message, strlen(message) + 1);
      sleep(1);
    }
    break;
  default:
    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    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c8 <main+0x38>
lea    0xe49(%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    0xe3e(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe3e(%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     121b <main+0x8b>
jmp    120a <main+0x7a>
mov    -0x64(%rbp),%eax
sub    $0x2a,%eax
je     124c <main+0xbc>
jmp    1287 <main+0xf7>
jmp    1220 <main+0x90>
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    0xe00(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1220 <main+0x90>
jmp    1251 <main+0xc1>
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    %edx,-0x68(%rbp)
mov    $0x13,%edi
call   1040 <sahn_send@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1251 <main+0xc1>
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    0xd99(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1080 <sahn_cleanup@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
cmp    $0x2,%edi
jne    1282 <main+0xe2>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
lea    0xe4a(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1030 <sahn_init@plt>
cmp    $0x13,%ebx
je     1204 <main+0x64>
cmp    $0x2a,%ebx
jne    1243 <main+0xa3>
lea    0xe22(%rip),%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    $0x13,%edi
mov    %rbx,%rsi
call   1040 <sahn_send@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    11e0 <main+0x40>
lea    0xc(%rsp),%r14
lea    0x10(%rsp),%rbx
lea    0xe0e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1070 <sahn_recv@plt>
mov    0xc(%rsp),%esi
mov    %r15,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1220 <main+0x80>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x40,%edx
call   1070 <sahn_recv@plt>
mov    0xc(%rsp),%esi
lea    0xdbe(%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
pop    %r14
pop    %r15
ret
lea    0xdaf(%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/test4/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 %38 [
i32 19, label %23
i32 42, label %30
]
23: ; preds = %15
br label %24
24: ; preds = %23, %24
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%26 = call i32 @sahn_recv(i32* %7, i8* %25, i32 64)
%27 = load i32, i32* %7
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %27, i8* %28)
br label %24
30: ; preds = %15
br label %31
31: ; preds = %30, %31
%32 = load i8*, i8** %8
%33 = load i8*, i8** %8
%34 = call i32 @strlen(i8* %33)
%35 = add nsw i32 %34, 1
%36 = call i32 @sahn_send(i32 19, i8* %32, i32 %35)
%37 = call i32 @sleep(i32 1)
br label %31
38: ; preds = %15
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%40 = call i32 @sahn_recv(i32* %7, i8* %39, i32 64)
%41 = load i32, i32* %7
%42 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %41, i8* %42)
br label %44
44: ; preds = %38
%45 = 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/test4/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 %25 [
i32 19, label %16
i32 42, label %20
]
16: ; preds = %9, %16
%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)
br label %16
20: ; preds = %9, %20
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = add nsw i32 %21, 1
%23 = tail call i32 @sahn_send(i32 19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = tail call i32 @sleep(i32 1)
br label %20
25: ; preds = %9
%26 = call i32 @sahn_recv(i32* %3, i8* %14, i32 64)
%27 = load i32, i32* %3
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %27, i8* %14)
%29 = call i32 (...) @sahn_cleanup()
ret i32 0
}