#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 */

/* Variables and functions */
int INT_MAX;
scalar_t__ abs(int);
int /*<<< orphan*/ threeSumClosestQSort(int *, int /*<<< orphan*/, int);
int twoSumClosestDif(int *, int, int, int);
int threeSumClosest(int *nums, int numsSize, int target) {
  if (nums == NULL || numsSize < 3) {
    return 0;
  }
  threeSumClosestQSort(nums, 0, numsSize - 1);
  int dif = 0;
  int minDif = INT_MAX;
  int i = 0;
  for (i = 1; i < numsSize - 1;) {
    dif = twoSumClosestDif(nums, numsSize, i, target - nums[i - 1]);
    if (dif == 0) {
      return target;
    } else if (abs(dif) < abs(minDif)) {
      minDif = dif;
    }
    i++;
  }
  return target + minDif;
}


x86 assembly (clang, -O0):
<threeSumClosest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1157 <threeSumClosest+0x27>
cmpl   $0x3,-0x14(%rbp)
jge    1163 <threeSumClosest+0x33>
movl   $0x0,-0x4(%rbp)
jmp    1229 <threeSumClosest+0xf9>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1030 <threeSumClosestQSort@plt>
movl   $0x0,-0x1c(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1220 <threeSumClosest+0xf0>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%r8d
sub    $0x1,%r8d
movslq %r8d,%r8
sub    (%rax,%r8,4),%ecx
call   1050 <twoSumClosestDif@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11e3 <threeSumClosest+0xb3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1229 <threeSumClosest+0xf9>
mov    -0x1c(%rbp),%edi
call   1040 <abs@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <abs@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jge    120d <threeSumClosest+0xdd>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1212 <threeSumClosest+0xe2>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1195 <threeSumClosest+0x65>
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<threeSumClosest>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,0xc(%rsp)
xor    %ebx,%ebx
test   %rdi,%rdi
je     11d0 <threeSumClosest+0xa0>
mov    %esi,%r12d
cmp    $0x3,%esi
jl     11d0 <threeSumClosest+0xa0>
mov    %rdi,%r13
lea    -0x1(%r12),%edx
xor    %esi,%esi
mov    %rdx,0x10(%rsp)
call   1030 <threeSumClosestQSort@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%r14d
mov    $0x1,%ebp
jmp    118e <threeSumClosest+0x5e>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
mov    %ebx,%r14d
cmp    %rbp,0x10(%rsp)
je     11c6 <threeSumClosest+0x96>
mov    0xc(%rsp),%ecx
sub    -0x4(%r13,%rbp,4),%ecx
mov    %r13,%rdi
mov    %r12d,%esi
mov    %ebp,%edx
call   1050 <twoSumClosestDif@plt>
test   %eax,%eax
je     11cc <threeSumClosest+0x9c>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <abs@plt>
mov    %rax,%r15
mov    %r14d,%edi
call   1040 <abs@plt>
cmp    %rax,%r15
jl     1180 <threeSumClosest+0x50>
mov    %r14d,%ebx
jmp    1180 <threeSumClosest+0x50>
add    0xc(%rsp),%ebx
jmp    11d0 <threeSumClosest+0xa0>
mov    0xc(%rsp),%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@INT_MAX = global i32 0
define i32 @threeSumClosest(i32* %0, i32 %1, i32 %2) {
%4 = icmp eq i32* %0, null
%5 = icmp slt i32 %1, 3
%6 = or i1 %4, %5
br i1 %6, label %31, label %7
7: ; preds = %3
%8 = add nsw i32 %1, -1
%9 = tail call i32 @threeSumClosestQSort(i32* %0, i32 0, i32 %8)
%10 = load i32, i32* @INT_MAX
%11 = zext i32 %8 to i64
br label %12
12: ; preds = %7, %22
%13 = phi i64 [ 1, %7 ], [ %27, %22 ]
%14 = phi i32 [ %10, %7 ], [ %26, %22 ]
%15 = add nsw i64 %13, -1
%16 = getelementptr inbounds i32, i32* %0, i64 %15
%17 = load i32, i32* %16
%18 = sub nsw i32 %2, %17
%19 = trunc i64 %13 to i32
%20 = tail call i32 @twoSumClosestDif(i32* %0, i32 %1, i32 %19, i32 %18)
%21 = icmp eq i32 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %12
%23 = tail call i64 @abs(i32 %20)
%24 = tail call i64 @abs(i32 %14)
%25 = icmp slt i64 %23, %24
%26 = select i1 %25, i32 %20, i32 %14
%27 = add nuw nsw i64 %13, 1
%28 = icmp eq i64 %27, %11
br i1 %28, label %29, label %12
29: ; preds = %22
%30 = add nsw i32 %26, %2
br label %31
31: ; preds = %12, %29, %3
%32 = phi i32 [ 0, %3 ], [ %30, %29 ], [ %2, %12 ]
ret i32 %32
}