#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 /*<<< orphan*/ tmp;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ipc_read_line(int, char *, int);
int ipc_read_one(int);
int /*<<< orphan*/ ipc_sendf(int, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int ipc_send(int fd, char type, char *arg) {
  char *fmt;
  char *opt;
  char tmp[4096];
  int r;
  if (fd < 0)
    return 0;
  fmt = opt = NULL;
  switch (type) {
  case 'r':
    fmt = "R%s";
    opt = arg;
    break;
  case 'l':
    fmt = "L";
    opt = NULL;
    break;
  default:
    fprintf(stderr, "Unknown ipc command %c\n", type);
    return 0;
  }
  if (!ipc_sendf(fd, fmt, opt)) {
    fprintf(stderr, "Could not communicate with the daemon! Is ldm running?\n");
    return 0;
  }
  switch (type) {
  case 'r':
    r = ipc_read_one(fd);
    if (r == '+')
      printf("Operation completed successfully\n");
    else
      printf("The operation didn't complete successfully\n");
    return (r == '+');
  case 'l':
    while (1) {
      r = ipc_read_line(fd, tmp, sizeof(tmp));
      if (!r)
        break;
      printf("%s\n", tmp);
    }
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ipc_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    117e <ipc_send+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    132a <ipc_send+0x1da>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
movsbl -0x9(%rbp),%eax
mov    %eax,-0x1038(%rbp)
sub    $0x6c,%eax
je     11d2 <ipc_send+0x82>
jmp    11a6 <ipc_send+0x56>
mov    -0x1038(%rbp),%eax
sub    $0x72,%eax
jne    11ea <ipc_send+0x9a>
jmp    11ba <ipc_send+0x6a>
lea    0xe3f(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1211 <ipc_send+0xc1>
lea    0xe2b(%rip),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
jmp    1211 <ipc_send+0xc1>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
movsbl -0x9(%rbp),%edx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132a <ipc_send+0x1da>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <ipc_sendf@plt>
cmp    $0x0,%eax
jne    124d <ipc_send+0xfd>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132a <ipc_send+0x1da>
movsbl -0x9(%rbp),%eax
mov    %eax,-0x103c(%rbp)
sub    $0x6c,%eax
je     12cc <ipc_send+0x17c>
jmp    1265 <ipc_send+0x115>
mov    -0x103c(%rbp),%eax
sub    $0x72,%eax
jne    1323 <ipc_send+0x1d3>
jmp    1279 <ipc_send+0x129>
mov    -0x8(%rbp),%edi
call   1030 <ipc_read_one@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x2b,-0x1034(%rbp)
jne    12a7 <ipc_send+0x157>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b5 <ipc_send+0x165>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x2b,-0x1034(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    132a <ipc_send+0x1da>
jmp    12d1 <ipc_send+0x181>
mov    -0x8(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <ipc_read_line@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jne    12fd <ipc_send+0x1ad>
jmp    1317 <ipc_send+0x1c7>
lea    -0x1030(%rbp),%rsi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d1 <ipc_send+0x181>
movl   $0x1,-0x4(%rbp)
jmp    132a <ipc_send+0x1da>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_send>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
xor    %r14d,%r14d
test   %edi,%edi
js     124f <ipc_send+0xff>
mov    %edi,%ebp
movsbl %sil,%ebx
cmp    $0x72,%ebx
je     1181 <ipc_send+0x31>
cmp    $0x6c,%ebx
jne    11ed <ipc_send+0x9d>
lea    0xe87(%rip),%rsi
xor    %edx,%edx
jmp    1188 <ipc_send+0x38>
lea    0xe78(%rip),%rsi
mov    %ebp,%edi
call   1050 <ipc_sendf@plt>
test   %eax,%eax
je     120b <ipc_send+0xbb>
cmp    $0x72,%ebx
je     1227 <ipc_send+0xd7>
cmp    $0x6c,%ebx
jne    124f <ipc_send+0xff>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1000,%edx
call   1070 <ipc_read_line@plt>
mov    $0x1,%r14d
test   %eax,%eax
je     124f <ipc_send+0xff>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <ipc_read_line@plt>
test   %eax,%eax
jne    11d0 <ipc_send+0x80>
jmp    124f <ipc_send+0xff>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    124f <ipc_send+0xff>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    124f <ipc_send+0xff>
mov    %ebp,%edi
call   1040 <ipc_read_one@plt>
xor    %r14d,%r14d
cmp    $0x2b,%eax
sete   %r14b
lea    0xe42(%rip),%rax
lea    0xe10(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"R%s\00"
@.str.1 = [2 x i8] c"L\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Unknown ipc command %c\0A\00"
@.str.3 = [56 x i8] c"Could not communicate with the daemon! Is ldm running?\0A\00"
@.str.4 = [34 x i8] c"Operation completed successfully\0A\00"
@.str.5 = [44 x i8] c"The operation didn't complete successfully\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @ipc_send(i32 %0, i8 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [4096 x i8]
%11 = alloca i32
store i32 %0, i32* %5
store i8 %1, i8* %6
store i8* %2, i8** %7
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 0, i32* %4
br label %64
15: ; preds = %3
store i8* null, i8** %9
store i8* null, i8** %8
%16 = load i8, i8* %6
%17 = sext i8 %16 to i32
switch i32 %17, label %21 [
i32 114, label %18
i32 108, label %20
]
18: ; preds = %15
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %8
%19 = load i8*, i8** %7
store i8* %19, i8** %9
br label %26
20: ; preds = %15
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %8
store i8* null, i8** %9
br label %26
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = load i8, i8* %6
%24 = sext i8 %23 to i32
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %24)
store i32 0, i32* %4
br label %64
26: ; preds = %20, %18
%27 = load i32, i32* %5
%28 = load i8*, i8** %8
%29 = load i8*, i8** %9
%30 = call i32 @ipc_sendf(i32 %27, i8* %28, i8* %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %64
35: ; preds = %26
%36 = load i8, i8* %6
%37 = sext i8 %36 to i32
switch i32 %37, label %63 [
i32 114, label %38
i32 108, label %51
]
38: ; preds = %35
%39 = load i32, i32* %5
%40 = call i32 @ipc_read_one(i32 %39)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = icmp eq i32 %41, 43
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %47
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %47
47: ; preds = %45, %43
%48 = load i32, i32* %11
%49 = icmp eq i32 %48, 43
%50 = zext i1 %49 to i32
store i32 %50, i32* %4
br label %64
51: ; preds = %35
br label %52
52: ; preds = %51, %59
%53 = load i32, i32* %5
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%55 = call i32 @ipc_read_line(i32 %53, i8* %54, i32 4096)
store i32 %55, i32* %11
%56 = load i32, i32* %11
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %52
br label %62
59: ; preds = %52
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %60)
br label %52
62: ; preds = %58
store i32 1, i32* %4
br label %64
63: ; preds = %35
store i32 0, i32* %4
br label %64
64: ; preds = %63, %62, %47, %32, %21, %14
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@.str = [4 x i8] c"R%s\00"
@.str.1 = [2 x i8] c"L\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Unknown ipc command %c\0A\00"
@.str.3 = [56 x i8] c"Could not communicate with the daemon! Is ldm running?\0A\00"
@str = [43 x i8] c"The operation didn't complete successfully\00"
@str.7 = [33 x i8] c"Operation completed successfully\00"
define i32 @ipc_send(i32 %0, i8 signext %1, i8* %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = icmp slt i32 %0, 0
br i1 %6, label %35, label %7
7: ; preds = %3
%8 = sext i8 %1 to i32
switch i32 %8, label %10 [
i32 114, label %13
i32 108, label %9
]
9: ; preds = %7
br label %13
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %8)
br label %35
13: ; preds = %7, %9
%14 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %7 ]
%15 = phi i8* [ null, %9 ], [ %2, %7 ]
%16 = tail call i32 @ipc_sendf(i32 %0, i8* %14, i8* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
br label %35
21: ; preds = %13
switch i32 %8, label %35 [
i32 114, label %25
i32 108, label %22
]
22: ; preds = %21
%23 = call i32 @ipc_read_line(i32 %0, i8* %5, i32 4096)
%24 = icmp eq i32 %23, 0
br i1 %24, label %35, label %31
25: ; preds = %21
%26 = tail call i32 @ipc_read_one(i32 %0)
%27 = icmp eq i32 %26, 43
%28 = select i1 %27, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0)
%29 = tail call i32 @puts(i8* dereferenceable(1) %28)
%30 = zext i1 %27 to i32
br label %35
31: ; preds = %22, %31
%32 = call i32 @puts(i8* %5)
%33 = call i32 @ipc_read_line(i32 %0, i8* %5, i32 4096)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %31
35: ; preds = %31, %22, %21, %3, %25, %18, %10
%36 = phi i32 [ 0, %10 ], [ %30, %25 ], [ 0, %18 ], [ 0, %3 ], [ 0, %21 ], [ 1, %22 ], [ 1, %31 ]
ret i32 %36
}