#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 ssize_t;

/* Variables and functions */
int chdir(char const *);
int /*<<< orphan*/ close(int);
int cmd_cat(char const *, char *);
int connect_to_control_socket();
int /*<<< orphan*/ perror(char const *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int cmd_status_query(const char *jobid) {
  if (chdir(jobid) == -1) {
    perror(jobid);
    return 1;
  }
  int fd = connect_to_control_socket();
  if (fd != -1) {
    char buffer[1024];
    ssize_t len;
    write(fd, "STATUS\n", 7);
    while ((len = read(fd, buffer, 1023)) > 0) {
      write(1, buffer, len);
    }
    if (len == -1) {
      perror("write");
    }
    chdir("..");
    close(fd);
    return len == -1 ? 1 : 0;
  } else {
    chdir("..");
    return cmd_cat(jobid, "status");
  }
}


x86 assembly (clang, -O0):
<cmd_status_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11a6 <cmd_status_query+0x36>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <cmd_status_query+0x104>
mov    $0x0,%al
call   1030 <connect_to_control_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1255 <cmd_status_query+0xe5>
mov    -0x14(%rbp),%edi
lea    0xe3c(%rip),%rsi
mov    $0x7,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jle    120d <cmd_status_query+0x9d>
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
jmp    11ce <cmd_status_query+0x5e>
cmpl   $0xffffffff,-0x424(%rbp)
jne    1226 <cmd_status_query+0xb6>
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
lea    0xde1(%rip),%rdi
call   1050 <chdir@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x424(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0xffffffff,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1274 <cmd_status_query+0x104>
lea    0xdb2(%rip),%rdi
call   1050 <chdir@plt>
mov    -0x10(%rbp),%rdi
lea    0xda5(%rip),%rsi
call   1090 <cmd_cat@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_status_query>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbp
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
je     124a <cmd_status_query+0xda>
xor    %eax,%eax
call   1030 <connect_to_control_socket@plt>
cmp    $0xffffffff,%eax
je     1224 <cmd_status_query+0xb4>
mov    %eax,%r14d
lea    0xe5a(%rip),%rsi
mov    %eax,%edi
mov    $0x7,%edx
call   1040 <write@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11f5 <cmd_status_query+0x85>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11d0 <cmd_status_query+0x60>
cmp    $0xffffffff,%ebp
je     1259 <cmd_status_query+0xe9>
xor    %ebx,%ebx
cmp    $0xffffffff,%ebp
sete   %bl
lea    0xe05(%rip),%rdi
call   1050 <chdir@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde3(%rip),%rdi
call   1050 <chdir@plt>
lea    0xdda(%rip),%rsi
mov    %rbp,%rdi
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <cmd_cat@plt>
mov    %rbp,%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    1216 <cmd_status_query+0xa6>
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
jmp    11fa <cmd_status_query+0x8a>

LLVM IR (-O0):
@.str = [8 x i8] c"STATUS\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [7 x i8] c"status\00"
define i32 @cmd_status_query(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @chdir(i8* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @perror(i8* %11)
store i32 1, i32* %2
br label %46
13: ; preds = %1
%14 = call i32 (...) @connect_to_control_socket()
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %42
17: ; preds = %13
%18 = load i32, i32* %4
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
br label %20
20: ; preds = %25, %17
%21 = load i32, i32* %4
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 1023)
store i32 %23, i32* %6
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = load i32, i32* %6
%28 = call i32 @write(i32 1, i8* %26, i32 %27)
br label %20
29: ; preds = %20
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, -1
%40 = zext i1 %39 to i64
%41 = select i1 %39, i32 1, i32 0
store i32 %41, i32* %2
br label %46
42: ; preds = %13
%43 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%44 = load i8*, i8** %3
%45 = call i32 @cmd_cat(i8* %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 %45, i32* %2
br label %46
46: ; preds = %42, %34, %10
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@.str = [8 x i8] c"STATUS\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [7 x i8] c"status\00"
define i32 @cmd_status_query(i8* %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32 @chdir(i8* %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror(i8* %0)
br label %32
7: ; preds = %1
%8 = tail call i32 (...) @connect_to_control_socket()
%9 = icmp eq i32 %8, -1
br i1 %9, label %29, label %10
10: ; preds = %7
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%12 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%13 = call i32 @read(i32 %8, i8* %11, i32 1023)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10, %15
%16 = phi i32 [ %18, %15 ], [ %13, %10 ]
%17 = call i32 @write(i32 1, i8* %11, i32 %16)
%18 = call i32 @read(i32 %8, i8* %11, i32 1023)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %15, label %20
20: ; preds = %15, %10
%21 = phi i32 [ %13, %10 ], [ %18, %15 ]
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = tail call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @close(i32 %8)
%28 = zext i1 %22 to i32
br label %32
29: ; preds = %7
%30 = tail call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @cmd_cat(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %25, %29, %5
%33 = phi i32 [ 1, %5 ], [ %28, %25 ], [ %31, %29 ]
ret i32 %33
}