#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 scalar_t__ uint64_t;
struct sync_file_info {
  int num_fences;
  scalar_t__ sync_fence_info;
  scalar_t__ flags;
};
struct sync_fence_info {
  int num_fences;
  scalar_t__ sync_fence_info;
  scalar_t__ flags;
};

/* Variables and functions */
int /*<<< orphan*/ SYNC_IOC_FILE_INFO;
struct sync_file_info *calloc(int, int);
int /*<<< orphan*/ free(struct sync_file_info *);
int ioctl(int, int /*<<< orphan*/, struct sync_file_info *);
struct sync_file_info *sync_file_info(int fd) {
  struct sync_file_info *info;
  struct sync_fence_info *fence_info;
  int err, num_fences;
  info = calloc(1, sizeof(*info));
  if (info == NULL)
    return NULL;
  err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
  if (err < 0) {
    free(info);
    return NULL;
  }
  num_fences = info->num_fences;
  if (num_fences) {
    info->flags = 0;
    info->num_fences = num_fences;
    fence_info = calloc(num_fences, sizeof(*fence_info));
    if (!fence_info) {
      free(info);
      return NULL;
    }
    info->sync_fence_info = (uint64_t)fence_info;
    err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
    if (err < 0) {
      free(fence_info);
      free(info);
      return NULL;
    }
  }
  return info;
}


x86 assembly (clang, -O0):
<sync_file_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x1,%edi
mov    $0x18,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1166 <sync_file_info+0x36>
movq   $0x0,-0x8(%rbp)
jmp    1252 <sync_file_info+0x122>
mov    -0xc(%rbp),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119e <sync_file_info+0x6e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1252 <sync_file_info+0x122>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     124a <sync_file_info+0x11a>
mov    -0x18(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%edi
mov    $0x18,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f8 <sync_file_info+0xc8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1252 <sync_file_info+0x122>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0xc(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1245 <sync_file_info+0x115>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1252 <sync_file_info+0x122>
jmp    124a <sync_file_info+0x11a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sync_file_info>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x1,%edi
mov    $0x18,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11ad <sync_file_info+0x7d>
mov    %rax,%rbx
mov    0x2e89(%rip),%r15
mov    (%r15),%esi
mov    %ebp,%edi
mov    %rax,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
js     11a5 <sync_file_info+0x75>
mov    (%rbx),%edi
test   %edi,%edi
je     11af <sync_file_info+0x7f>
movq   $0x0,0x10(%rbx)
mov    $0x18,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11a5 <sync_file_info+0x75>
mov    %rax,%r14
mov    %rax,0x8(%rbx)
mov    (%r15),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
jns    11af <sync_file_info+0x7f>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sync_file_info = type { i32, i64, i64 }
%struct.sync_fence_info = type { i32, i64, i64 }
@SYNC_IOC_FILE_INFO = global i32 0
define %struct.sync_file_info* @sync_file_info(i32 %0) {
%2 = alloca %struct.sync_file_info*
%3 = alloca i32
%4 = alloca %struct.sync_file_info*
%5 = alloca %struct.sync_fence_info*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = call %struct.sync_file_info* @calloc(i32 1, i32 24)
store %struct.sync_file_info* %8, %struct.sync_file_info** %4
%9 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%10 = icmp eq %struct.sync_file_info* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store %struct.sync_file_info* null, %struct.sync_file_info** %2
br label %62
12: ; preds = %1
%13 = load i32, i32* %3
%14 = load i32, i32* @SYNC_IOC_FILE_INFO
%15 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%16 = call i32 @ioctl(i32 %13, i32 %14, %struct.sync_file_info* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%21 = call i32 @free(%struct.sync_file_info* %20)
store %struct.sync_file_info* null, %struct.sync_file_info** %2
br label %62
22: ; preds = %12
%23 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%24 = getelementptr inbounds %struct.sync_file_info, %struct.sync_file_info* %23, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %60
28: ; preds = %22
%29 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%30 = getelementptr inbounds %struct.sync_file_info, %struct.sync_file_info* %29, i32 0, i32 2
store i64 0, i64* %30
%31 = load i32, i32* %7
%32 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%33 = getelementptr inbounds %struct.sync_file_info, %struct.sync_file_info* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %7
%35 = call %struct.sync_file_info* @calloc(i32 %34, i32 24)
%36 = bitcast %struct.sync_file_info* %35 to %struct.sync_fence_info*
store %struct.sync_fence_info* %36, %struct.sync_fence_info** %5
%37 = load %struct.sync_fence_info*, %struct.sync_fence_info** %5
%38 = icmp ne %struct.sync_fence_info* %37, null
br i1 %38, label %42, label %39
39: ; preds = %28
%40 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%41 = call i32 @free(%struct.sync_file_info* %40)
store %struct.sync_file_info* null, %struct.sync_file_info** %2
br label %62
42: ; preds = %28
%43 = load %struct.sync_fence_info*, %struct.sync_fence_info** %5
%44 = ptrtoint %struct.sync_fence_info* %43 to i64
%45 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%46 = getelementptr inbounds %struct.sync_file_info, %struct.sync_file_info* %45, i32 0, i32 1
store i64 %44, i64* %46
%47 = load i32, i32* %3
%48 = load i32, i32* @SYNC_IOC_FILE_INFO
%49 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%50 = call i32 @ioctl(i32 %47, i32 %48, %struct.sync_file_info* %49)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %42
%54 = load %struct.sync_fence_info*, %struct.sync_fence_info** %5
%55 = bitcast %struct.sync_fence_info* %54 to %struct.sync_file_info*
%56 = call i32 @free(%struct.sync_file_info* %55)
%57 = load %struct.sync_file_info*, %struct.sync_file_info** %4
%58 = call i32 @free(%struct.sync_file_info* %57)
store %struct.sync_file_info* null, %struct.sync_file_info** %2
br label %62
59: ; preds = %42
br label %60
60: ; preds = %59, %22
%61 = load %struct.sync_file_info*, %struct.sync_file_info** %4
store %struct.sync_file_info* %61, %struct.sync_file_info** %2
br label %62
62: ; preds = %60, %53, %39, %19, %11
%63 = load %struct.sync_file_info*, %struct.sync_file_info** %2
ret %struct.sync_file_info* %63
}

LLVM IR (-O2):
%struct.sync_file_info = type { i32, i64, i64 }
@SYNC_IOC_FILE_INFO = global i32 0
define %struct.sync_file_info* @sync_file_info(i32 %0) {
%2 = tail call %struct.sync_file_info* @calloc(i32 1, i32 24)
%3 = icmp eq %struct.sync_file_info* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1
%5 = load i32, i32* @SYNC_IOC_FILE_INFO
%6 = tail call i32 @ioctl(i32 %0, i32 %5, %struct.sync_file_info* %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %4
%9 = getelementptr inbounds %struct.sync_file_info, %struct.sync_file_info* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp eq i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %8
%13 = getelementptr inbounds %struct.sync_file_info, %struct.sync_file_info* %2, i64 0, i32 2
store i64 0, i64* %13
%14 = tail call %struct.sync_file_info* @calloc(i32 %10, i32 24)
%15 = icmp eq %struct.sync_file_info* %14, null
br i1 %15, label %24, label %16
16: ; preds = %12
%17 = ptrtoint %struct.sync_file_info* %14 to i64
%18 = getelementptr inbounds %struct.sync_file_info, %struct.sync_file_info* %2, i64 0, i32 1
store i64 %17, i64* %18
%19 = load i32, i32* @SYNC_IOC_FILE_INFO
%20 = tail call i32 @ioctl(i32 %0, i32 %19, %struct.sync_file_info* %2)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = tail call i32 @free(%struct.sync_file_info* %14)
br label %24
24: ; preds = %12, %4, %22
%25 = tail call i32 @free(%struct.sync_file_info* %2)
br label %26
26: ; preds = %24, %8, %16, %1
%27 = phi %struct.sync_file_info* [ null, %1 ], [ %2, %16 ], [ %2, %8 ], [ null, %24 ]
ret %struct.sync_file_info* %27
}