int tgc_read(int sd, char *cmd) {
  int rc = 0;
  if (!cmd)
    return -3;
  if ((rc = read(sd, cmd, 1)) < 0) {
    print_log_msg((3), __FUNCTION__, "Failed reading command");
    return -4;
  }
  if (!rc)
    return -6;
  return 0;
}


x86 assembly (clang, -O0):
<tgc_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114d <tgc_read+0x2d>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11af <tgc_read+0x8f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1192 <tgc_read+0x72>
mov    $0x3,%edi
lea    0xe88(%rip),%rsi
lea    0xe8a(%rip),%rdx
mov    $0x0,%al
call   1040 <print_log_msg@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    11af <tgc_read+0x8f>
cmpl   $0x0,-0x14(%rbp)
jne    11a8 <tgc_read+0x88>
movl   $0xfffffffa,-0x4(%rbp)
jmp    11af <tgc_read+0x8f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tgc_read>:
push   %rbx
test   %rsi,%rsi
je     1142 <tgc_read+0x22>
xor    %ebx,%ebx
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <read@plt>
test   %eax,%eax
js     1149 <tgc_read+0x29>
mov    $0xfffffffa,%eax
cmove  %eax,%ebx
jmp    1168 <tgc_read+0x48>
mov    $0xfffffffd,%ebx
jmp    1168 <tgc_read+0x48>
lea    0xeb0(%rip),%rsi
lea    0xeb2(%rip),%rdx
mov    $0x3,%edi
xor    %eax,%eax
call   1040 <print_log_msg@plt>
mov    $0xfffffffc,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@__FUNCTION__.tgc_read = [9 x i8] c"tgc_read\00"
@.str = [23 x i8] c"Failed reading command\00"
define i32 @tgc_read(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%7 = load i8*, i8** %5
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i32 -3, i32* %3
br label %22
10: ; preds = %2
%11 = load i32, i32* %4
%12 = load i8*, i8** %5
%13 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %11, i8* %12, i32 1)
store i32 %13, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @print_log_msg to i32 (i32, i8*, i8*, ...)*)(i32 3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.tgc_read, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -4, i32* %3
br label %22
17: ; preds = %10
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %17
store i32 -6, i32* %3
br label %22
21: ; preds = %17
store i32 0, i32* %3
br label %22
22: ; preds = %21, %20, %15, %9
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
@__FUNCTION__.tgc_read = [9 x i8] c"tgc_read\00"
@.str = [23 x i8] c"Failed reading command\00"
define i32 @tgc_read(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %12, label %4
4: ; preds = %2
%5 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %0, i8* %1, i32 1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 (i32, i8*, i8*, ...) bitcast (i32 (...)* @print_log_msg to i32 (i32, i8*, i8*, ...)*)(i32 3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.tgc_read, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %12
9: ; preds = %4
%10 = icmp eq i32 %5, 0
%11 = select i1 %10, i32 -6, i32 0
br label %12
12: ; preds = %9, %2, %7
%13 = phi i32 [ -4, %7 ], [ -3, %2 ], [ %11, %9 ]
ret i32 %13
}