#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 tracecmd_input {
  int dummy;
};

/* Variables and functions */
struct tracecmd_input *tracecmd_alloc_fd(int);
int /*<<< orphan*/ tracecmd_close(struct tracecmd_input *);
scalar_t__ tracecmd_init_data(struct tracecmd_input *);
scalar_t__ tracecmd_read_headers(struct tracecmd_input *);
struct tracecmd_input *tracecmd_open_fd(int fd) {
  struct tracecmd_input *handle;
  handle = tracecmd_alloc_fd(fd);
  if (!handle)
    return NULL;
  if (tracecmd_read_headers(handle) < 0)
    goto fail;
  if (tracecmd_init_data(handle) < 0)
    goto fail;
  return handle;
fail:
  tracecmd_close(handle);
  return NULL;
}


x86 assembly (clang, -O0):
<tracecmd_open_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <tracecmd_alloc_fd@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116f <tracecmd_open_fd+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11bd <tracecmd_open_fd+0x7d>
mov    -0x18(%rbp),%rdi
call   1050 <tracecmd_read_headers@plt>
cmp    $0x0,%rax
jge    1187 <tracecmd_open_fd+0x47>
jmp    11ac <tracecmd_open_fd+0x6c>
mov    -0x18(%rbp),%rdi
call   1060 <tracecmd_init_data@plt>
cmp    $0x0,%rax
jge    119f <tracecmd_open_fd+0x5f>
jmp    11ac <tracecmd_open_fd+0x6c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bd <tracecmd_open_fd+0x7d>
mov    -0x18(%rbp),%rdi
call   1040 <tracecmd_close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tracecmd_open_fd>:
push   %rbx
call   1030 <tracecmd_alloc_fd@plt>
test   %rax,%rax
je     1170 <tracecmd_open_fd+0x30>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <tracecmd_read_headers@plt>
test   %rax,%rax
js     1168 <tracecmd_open_fd+0x28>
mov    %rbx,%rdi
call   1060 <tracecmd_init_data@plt>
test   %rax,%rax
jns    1172 <tracecmd_open_fd+0x32>
mov    %rbx,%rdi
call   1040 <tracecmd_close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.tracecmd_input = type { i32 }
define %struct.tracecmd_input* @tracecmd_open_fd(i32 %0) {
%2 = alloca %struct.tracecmd_input*
%3 = alloca i32
%4 = alloca %struct.tracecmd_input*
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call %struct.tracecmd_input* @tracecmd_alloc_fd(i32 %5)
store %struct.tracecmd_input* %6, %struct.tracecmd_input** %4
%7 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%8 = icmp ne %struct.tracecmd_input* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store %struct.tracecmd_input* null, %struct.tracecmd_input** %2
br label %25
10: ; preds = %1
%11 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%12 = call i64 @tracecmd_read_headers(%struct.tracecmd_input* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
br label %22
15: ; preds = %10
%16 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%17 = call i64 @tracecmd_init_data(%struct.tracecmd_input* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
br label %22
20: ; preds = %15
%21 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
store %struct.tracecmd_input* %21, %struct.tracecmd_input** %2
br label %25
22: ; preds = %19, %14
%23 = load %struct.tracecmd_input*, %struct.tracecmd_input** %4
%24 = call i32 @tracecmd_close(%struct.tracecmd_input* %23)
store %struct.tracecmd_input* null, %struct.tracecmd_input** %2
br label %25
25: ; preds = %22, %20, %9
%26 = load %struct.tracecmd_input*, %struct.tracecmd_input** %2
ret %struct.tracecmd_input* %26
}

LLVM IR (-O2):
%struct.tracecmd_input = type { i32 }
define %struct.tracecmd_input* @tracecmd_open_fd(i32 %0) {
%2 = tail call %struct.tracecmd_input* @tracecmd_alloc_fd(i32 %0)
%3 = icmp eq %struct.tracecmd_input* %2, null
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = tail call i64 @tracecmd_read_headers(%struct.tracecmd_input* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i64 @tracecmd_init_data(%struct.tracecmd_input* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %4
%11 = tail call i32 @tracecmd_close(%struct.tracecmd_input* %2)
br label %12
12: ; preds = %7, %1, %10
%13 = phi %struct.tracecmd_input* [ null, %10 ], [ null, %1 ], [ %2, %7 ]
ret %struct.tracecmd_input* %13
}