#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 */
struct katcl_line {
  int dummy;
};

/* Variables and functions */
scalar_t__ borph_read(struct katcl_line *, char *, unsigned char *, int, int);
scalar_t__ borph_write(struct katcl_line *, char *, unsigned char *, int, int);
struct katcl_line *create_katcl(int);
int /*<<< orphan*/ destroy_katcl(struct katcl_line *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getenv(char *);
int net_connect(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  char *server;
  int fd;
  struct katcl_line *l;
  unsigned char data[4];
  if (argc <= 0) {
    server = getenv("KATCP_SERVER");
  } else {
    server = argv[1];
  }
  if (server == NULL) {
    fprintf(
        stderr,
        "need a server as first argument or in the KATCP_SERVER variable\n");
    return 2;
  }
  fd = net_connect(server, 0, 0x01 | 0x02);
  if (fd < 0) {
    fprintf(stderr, "unable to connect to %s\n", server);
    return 2;
  }
  l = create_katcl(fd);
  if (l == NULL) {
    fprintf(stderr, "unable to allocate state\n");
    return 2;
  }
  if (borph_read(l, "junk", data, 4, 50000) < 0) {
    fprintf(stderr, "unable to read register\n");
    return 2;
  }
  data[0] = 0xde;
  data[1] = 0xad;
  data[2] = 0xbe;
  data[3] = 0xef;
  if (borph_write(l, "junk", data, 4, 50000) < 0) {
    fprintf(stderr, "unable to write to register\n");
    return 2;
  }
  destroy_katcl(l, 1);
  return 0;
}


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   $0x0,-0x8(%rbp)
jg     11a5 <main+0x35>
lea    0xe69(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
jmp    11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11df <main+0x6f>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <main+0x1aa>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1050 <net_connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1223 <main+0xb3>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <main+0x1aa>
mov    -0x1c(%rbp),%edi
call   1080 <create_katcl@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    125d <main+0xed>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <main+0x1aa>
mov    -0x28(%rbp),%rdi
lea    -0x2c(%rbp),%rdx
lea    0xe15(%rip),%rsi
mov    $0x4,%ecx
mov    $0xc350,%r8d
call   1040 <borph_read@plt>
cmp    $0x0,%rax
jge    12a9 <main+0x139>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <main+0x1aa>
movb   $0xde,-0x2c(%rbp)
movb   $0xad,-0x2b(%rbp)
movb   $0xbe,-0x2a(%rbp)
movb   $0xef,-0x29(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x2c(%rbp),%rdx
lea    0xdb9(%rip),%rsi
mov    $0x4,%ecx
mov    $0xc350,%r8d
call   1090 <borph_write@plt>
cmp    $0x0,%rax
jge    1305 <main+0x195>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    131a <main+0x1aa>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1070 <destroy_katcl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
jle    121c <main+0xac>
mov    0x8(%rsi),%rbx
test   %rbx,%rbx
je     1234 <main+0xc4>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1050 <net_connect@plt>
test   %eax,%eax
js     1246 <main+0xd6>
mov    %eax,%edi
call   1080 <create_katcl@plt>
test   %rax,%rax
je     1262 <main+0xf2>
mov    %rax,%rbx
lea    0xec6(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
mov    $0x4,%ecx
mov    $0xc350,%r8d
call   1040 <borph_read@plt>
test   %rax,%rax
js     1274 <main+0x104>
movl   $0xefbeadde,0xc(%rsp)
ef
lea    0xe96(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x4,%ecx
mov    $0xc350,%r8d
call   1090 <borph_write@plt>
test   %rax,%rax
js     1286 <main+0x116>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1070 <destroy_katcl@plt>
xor    %eax,%eax
jmp    12a2 <main+0x132>
lea    0xddd(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rbx,%rbx
jne    118a <main+0x1a>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
jmp    1296 <main+0x126>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    129d <main+0x12d>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
jmp    1296 <main+0x126>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
jmp    1296 <main+0x126>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.katcl_line = type { i32 }
@.str = [13 x i8] c"KATCP_SERVER\00"
@stderr = global i32 0
@.str.1 = [65 x i8] c"need a server as first argument or in the KATCP_SERVER variable\0A\00"
@.str.2 = [25 x i8] c"unable to connect to %s\0A\00"
@.str.3 = [26 x i8] c"unable to allocate state\0A\00"
@.str.4 = [5 x i8] c"junk\00"
@.str.5 = [25 x i8] c"unable to read register\0A\00"
@.str.6 = [29 x i8] c"unable to write to register\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.katcl_line*
%9 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %6
br label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
br label %18
18: ; preds = %14, %12
%19 = load i8*, i8** %6
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %64
24: ; preds = %18
%25 = load i8*, i8** %6
%26 = call i32 @net_connect(i8* %25, i32 0, i32 3)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %6
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %31)
store i32 2, i32* %3
br label %64
33: ; preds = %24
%34 = load i32, i32* %7
%35 = call %struct.katcl_line* @create_katcl(i32 %34)
store %struct.katcl_line* %35, %struct.katcl_line** %8
%36 = load %struct.katcl_line*, %struct.katcl_line** %8
%37 = icmp eq %struct.katcl_line* %36, null
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %3
br label %64
41: ; preds = %33
%42 = load %struct.katcl_line*, %struct.katcl_line** %8
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%44 = call i64 @borph_read(%struct.katcl_line* %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32 4, i32 50000)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %3
br label %64
49: ; preds = %41
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
store i8 -34, i8* %50
%51 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 -83, i8* %51
%52 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 -66, i8* %52
%53 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 -17, i8* %53
%54 = load %struct.katcl_line*, %struct.katcl_line** %8
%55 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%56 = call i64 @borph_write(%struct.katcl_line* %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %55, i32 4, i32 50000)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %49
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
store i32 2, i32* %3
br label %64
61: ; preds = %49
%62 = load %struct.katcl_line*, %struct.katcl_line** %8
%63 = call i32 @destroy_katcl(%struct.katcl_line* %62, i32 1)
store i32 0, i32* %3
br label %64
64: ; preds = %61, %58, %46, %38, %29, %21
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.katcl_line = type { i32 }
@.str = [13 x i8] c"KATCP_SERVER\00"
@stderr = global i32 0
@.str.1 = [65 x i8] c"need a server as first argument or in the KATCP_SERVER variable\0A\00"
@.str.2 = [25 x i8] c"unable to connect to %s\0A\00"
@.str.3 = [26 x i8] c"unable to allocate state\0A\00"
@.str.4 = [5 x i8] c"junk\00"
@.str.5 = [25 x i8] c"unable to read register\0A\00"
@.str.6 = [29 x i8] c"unable to write to register\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %11
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
br label %11
11: ; preds = %8, %6
%12 = phi i8* [ %7, %6 ], [ %10, %8 ]
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.1, i64 0, i64 0))
br label %46
17: ; preds = %11
%18 = tail call i32 @net_connect(i8* %12, i32 0, i32 3)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %12)
br label %46
23: ; preds = %17
%24 = tail call %struct.katcl_line* @create_katcl(i32 %18)
%25 = icmp eq %struct.katcl_line* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %46
29: ; preds = %23
%30 = call i64 @borph_read(%struct.katcl_line* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %4, i32 4, i32 50000)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %46
35: ; preds = %29
store i8 -34, i8* %4
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
store i8 -83, i8* %36
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
store i8 -66, i8* %37
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
store i8 -17, i8* %38
%39 = call i64 @borph_write(%struct.katcl_line* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %4, i32 4, i32 50000)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %46
44: ; preds = %35
%45 = call i32 @destroy_katcl(%struct.katcl_line* %24, i32 1)
br label %46
46: ; preds = %44, %41, %32, %26, %20, %14
%47 = phi i32 [ 2, %14 ], [ 2, %20 ], [ 2, %26 ], [ 2, %32 ], [ 2, %41 ], [ 0, %44 ]
ret i32 %47
}