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

/* Variables and functions */
int /*<<< orphan*/ SYS_modify_ldt;
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ syscall(int /*<<< orphan*/, int, struct user_desc const *, int);
void add_ldt(const struct user_desc *desc, bool *var, const char *name) {
  if (syscall(SYS_modify_ldt, 1, desc, sizeof(*desc)) == 0) {
    *var = true;
  } else {
    printf("[NOTE]\tFailed to create %s segment\n", name);
    *var = false;
  }
}


x86 assembly (clang, -O0):
<add_ldt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    $0x1,%esi
mov    $0x4,%ecx
call   1040 <syscall@plt>
cmp    $0x0,%rax
jne    1169 <add_ldt+0x49>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    1183 <add_ldt+0x63>
mov    -0x18(%rbp),%rsi
lea    0xe8c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_ldt>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rdx
mov    0x2ea4(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x4,%ecx
call   1040 <syscall@plt>
test   %rax,%rax
je     1160 <add_ldt+0x40>
lea    0xeaa(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
xor    %ebp,%ebp
mov    %ebp,(%rbx)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.user_desc = type { i32 }
@SYS_modify_ldt = global i32 0
@.str = [36 x i8] c"[NOTE]\09Failed to create %s segment\0A\00"
define void @add_ldt(%struct.user_desc* %0, i32* %1, i8* %2) {
%4 = alloca %struct.user_desc*
%5 = alloca i32*
%6 = alloca i8*
store %struct.user_desc* %0, %struct.user_desc** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%7 = load i32, i32* @SYS_modify_ldt
%8 = load %struct.user_desc*, %struct.user_desc** %4
%9 = call i64 @syscall(i32 %7, i32 1, %struct.user_desc* %8, i32 4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32*, i32** %5
store i32 1, i32* %12
br label %17
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32*, i32** %5
store i32 0, i32* %16
br label %17
17: ; preds = %13, %11
ret void
}

LLVM IR (-O2):
%struct.user_desc = type { i32 }
@SYS_modify_ldt = global i32 0
@.str = [36 x i8] c"[NOTE]\09Failed to create %s segment\0A\00"
define void @add_ldt(%struct.user_desc* %0, i32* nocapture writeonly %1, i8* %2) {
%4 = load i32, i32* @SYS_modify_ldt
%5 = tail call i64 @syscall(i32 %4, i32 1, %struct.user_desc* %0, i32 4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %9
9: ; preds = %3, %7
%10 = phi i32 [ 0, %7 ], [ 1, %3 ]
store i32 %10, i32* %1
ret void
}