#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 connect_to_control_socket();
int /*<<< orphan*/ perror(char const *);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int cmd_signal(const char *jobid, int signum) {
  if (chdir(jobid) == -1) {
    perror(jobid);
    return 1;
  }
  int fd = connect_to_control_socket();
  if (fd != -1) {
    char buffer[1024];
    ssize_t len;
    snprintf(buffer, 1024, "SIGNAL %d\n", signum);
    write(fd, buffer, strlen(buffer));
    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 {
    write(2, "Process not running\n", 20);
    chdir("..");
    return 1;
  }
  return 1;
}


x86 assembly (clang, -O0):
<cmd_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11b9 <cmd_signal+0x39>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cf <cmd_signal+0x14f>
mov    $0x0,%al
call   1030 <connect_to_control_socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     12a6 <cmd_signal+0x126>
lea    -0x420(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x400,%esi
lea    0xe1d(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x434(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jle    125e <cmd_signal+0xde>
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
jmp    121f <cmd_signal+0x9f>
cmpl   $0xffffffff,-0x424(%rbp)
jne    1277 <cmd_signal+0xf7>
lea    0xd99(%rip),%rdi
call   10a0 <perror@plt>
lea    0xd93(%rip),%rdi
call   1060 <chdir@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x424(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0xffffffff,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12cf <cmd_signal+0x14f>
mov    $0x2,%edi
lea    0xd62(%rip),%rsi
mov    $0x14,%edx
call   1040 <write@plt>
lea    0xd4e(%rip),%rdi
call   1060 <chdir@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_signal>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1060 <chdir@plt>
cmp    $0xffffffff,%eax
je     127d <cmd_signal+0xfd>
xor    %eax,%eax
call   1030 <connect_to_control_socket@plt>
cmp    $0xffffffff,%eax
je     1248 <cmd_signal+0xc8>
mov    %eax,%r14d
lea    0xe48(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %ebp,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1225 <cmd_signal+0xa5>
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   1090 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1200 <cmd_signal+0x80>
cmp    $0xffffffff,%ebp
je     1287 <cmd_signal+0x107>
xor    %ebx,%ebx
cmp    $0xffffffff,%ebp
sete   %bl
lea    0xdd8(%rip),%rdi
call   1060 <chdir@plt>
mov    %r14d,%edi
call   1080 <close@plt>
jmp    126f <cmd_signal+0xef>
lea    0xdc5(%rip),%rsi
mov    $0x2,%edi
mov    $0x14,%edx
call   1040 <write@plt>
lea    0xdac(%rip),%rdi
call   1060 <chdir@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
call   10a0 <perror@plt>
jmp    126a <cmd_signal+0xea>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
jmp    122a <cmd_signal+0xaa>

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

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